timescaledb/test/expected/truncate.out
Matvey Arye 2c594ec6f9 Keep catalog rows for some dropped chunks
If a chunk is dropped but it has a continuous aggregate that is
not dropped we want to preserve the chunk catalog row instead of
deleting the row. This is to prevent dangling identifiers in the
materialization hypertable. It also preserves the dimension slice
and chunk constraints rows for the chunk since those will be necessary
when enabling this with multinode and is necessary to recreate the
chunk too. The postgres objects associated with the chunk are all
dropped (table, constraints, indexes).

If data is ever reinserted to the same data region, the chunk is
recreated with the same dimension definitions as before. The postgres
objects are simply recreated.
2019-12-30 09:10:44 -05:00

182 lines
9.0 KiB
Plaintext

-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
\o /dev/null
\ir include/insert_two_partitions.sql
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
CREATE TABLE PUBLIC."two_Partitions" (
"timeCustom" BIGINT NOT NULL,
device_id TEXT NOT NULL,
series_0 DOUBLE PRECISION NULL,
series_1 DOUBLE PRECISION NULL,
series_2 DOUBLE PRECISION NULL,
series_bool BOOLEAN NULL
);
CREATE INDEX ON PUBLIC."two_Partitions" (device_id, "timeCustom" DESC NULLS LAST) WHERE device_id IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_0) WHERE series_0 IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_1) WHERE series_1 IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_2) WHERE series_2 IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, series_bool) WHERE series_bool IS NOT NULL;
CREATE INDEX ON PUBLIC."two_Partitions" ("timeCustom" DESC NULLS LAST, device_id);
SELECT * FROM create_hypertable('"public"."two_Partitions"'::regclass, 'timeCustom'::name, 'device_id'::name, associated_schema_name=>'_timescaledb_internal'::text, number_partitions => 2, chunk_time_interval=>_timescaledb_internal.interval_to_usec('1 month'));
\set QUIET off
BEGIN;
\COPY public."two_Partitions" FROM 'data/ds1_dev1_1.tsv' NULL AS '';
COMMIT;
INSERT INTO public."two_Partitions"("timeCustom", device_id, series_0, series_1) VALUES
(1257987600000000000, 'dev1', 1.5, 1),
(1257987600000000000, 'dev1', 1.5, 2),
(1257894000000000000, 'dev2', 1.5, 1),
(1257894002000000000, 'dev1', 2.5, 3);
INSERT INTO "two_Partitions"("timeCustom", device_id, series_0, series_1) VALUES
(1257894000000000000, 'dev2', 1.5, 2);
\set QUIET on
\o
SELECT * FROM _timescaledb_catalog.hypertable;
id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema | chunk_sizing_func_name | chunk_target_size | compressed | compressed_hypertable_id
----+-------------+----------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+------------+--------------------------
1 | public | two_Partitions | _timescaledb_internal | _hyper_1 | 2 | _timescaledb_internal | calculate_chunk_interval | 0 | f |
(1 row)
SELECT * FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | dropped
----+---------------+-----------------------+------------------+---------------------+---------
1 | 1 | _timescaledb_internal | _hyper_1_1_chunk | | f
2 | 1 | _timescaledb_internal | _hyper_1_2_chunk | | f
3 | 1 | _timescaledb_internal | _hyper_1_3_chunk | | f
4 | 1 | _timescaledb_internal | _hyper_1_4_chunk | | f
(4 rows)
SELECT * FROM test.show_subtables('"two_Partitions"');
Child | Tablespace
----------------------------------------+------------
_timescaledb_internal._hyper_1_1_chunk |
_timescaledb_internal._hyper_1_2_chunk |
_timescaledb_internal._hyper_1_3_chunk |
_timescaledb_internal._hyper_1_4_chunk |
(4 rows)
SELECT * FROM "two_Partitions";
timeCustom | device_id | series_0 | series_1 | series_2 | series_bool
---------------------+-----------+----------+----------+----------+-------------
1257894000000000000 | dev1 | 1.5 | 1 | 2 | t
1257894000000000000 | dev1 | 1.5 | 2 | |
1257894000000001000 | dev1 | 2.5 | 3 | |
1257894001000000000 | dev1 | 3.5 | 4 | |
1257894002000000000 | dev1 | 5.5 | 6 | | t
1257894002000000000 | dev1 | 5.5 | 7 | | f
1257894002000000000 | dev1 | 2.5 | 3 | |
1257897600000000000 | dev1 | 4.5 | 5 | | f
1257987600000000000 | dev1 | 1.5 | 1 | |
1257987600000000000 | dev1 | 1.5 | 2 | |
1257894000000000000 | dev2 | 1.5 | 1 | |
1257894000000000000 | dev2 | 1.5 | 2 | |
(12 rows)
SET client_min_messages = WARNING;
TRUNCATE "two_Partitions";
SELECT * FROM _timescaledb_catalog.hypertable;
id | schema_name | table_name | associated_schema_name | associated_table_prefix | num_dimensions | chunk_sizing_func_schema | chunk_sizing_func_name | chunk_target_size | compressed | compressed_hypertable_id
----+-------------+----------------+------------------------+-------------------------+----------------+--------------------------+--------------------------+-------------------+------------+--------------------------
1 | public | two_Partitions | _timescaledb_internal | _hyper_1 | 2 | _timescaledb_internal | calculate_chunk_interval | 0 | f |
(1 row)
SELECT * FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | dropped
----+---------------+-------------+------------+---------------------+---------
(0 rows)
-- should be empty
SELECT * FROM test.show_subtables('"two_Partitions"');
Child | Tablespace
-------+------------
(0 rows)
SELECT * FROM "two_Partitions";
timeCustom | device_id | series_0 | series_1 | series_2 | series_bool
------------+-----------+----------+----------+----------+-------------
(0 rows)
INSERT INTO public."two_Partitions"("timeCustom", device_id, series_0, series_1) VALUES
(1257987600000000000, 'dev1', 1.5, 1),
(1257987600000000000, 'dev1', 1.5, 2),
(1257894000000000000, 'dev2', 1.5, 1),
(1257894002000000000, 'dev1', 2.5, 3);
SELECT * FROM _timescaledb_catalog.chunk;
id | hypertable_id | schema_name | table_name | compressed_chunk_id | dropped
----+---------------+-----------------------+------------------+---------------------+---------
5 | 1 | _timescaledb_internal | _hyper_1_5_chunk | | f
6 | 1 | _timescaledb_internal | _hyper_1_6_chunk | | f
7 | 1 | _timescaledb_internal | _hyper_1_7_chunk | | f
(3 rows)
CREATE VIEW dependent_view AS SELECT * FROM _timescaledb_internal._hyper_1_5_chunk;
CREATE OR REPLACE FUNCTION test_trigger()
RETURNS TRIGGER LANGUAGE PLPGSQL AS
$BODY$
DECLARE
cnt INTEGER;
BEGIN
RAISE WARNING 'FIRING trigger when: % level: % op: % cnt: % trigger_name %',
tg_when, tg_level, tg_op, cnt, tg_name;
IF TG_OP = 'DELETE' THEN
RETURN OLD;
END IF;
RETURN NEW;
END
$BODY$;
-- test truncate on a chunk
CREATE TRIGGER _test_truncate_before
BEFORE TRUNCATE ON _timescaledb_internal._hyper_1_5_chunk
FOR EACH STATEMENT EXECUTE PROCEDURE test_trigger();
CREATE TRIGGER _test_truncate_after
AFTER TRUNCATE ON _timescaledb_internal._hyper_1_5_chunk
FOR EACH STATEMENT EXECUTE PROCEDURE test_trigger();
\set ON_ERROR_STOP 0
TRUNCATE "two_Partitions";
WARNING: FIRING trigger when: BEFORE level: STATEMENT op: TRUNCATE cnt: <NULL> trigger_name _test_truncate_before
WARNING: FIRING trigger when: AFTER level: STATEMENT op: TRUNCATE cnt: <NULL> trigger_name _test_truncate_after
ERROR: cannot drop table _timescaledb_internal._hyper_1_5_chunk because other objects depend on it
-- cannot TRUNCATE ONLY a hypertable
TRUNCATE ONLY "two_Partitions" CASCADE;
ERROR: cannot truncate only a hypertable
\set ON_ERROR_STOP 1
-- create a regular table to make sure we can truncate it in the same call
CREATE TABLE truncate_normal (color int);
INSERT INTO truncate_normal VALUES (1);
SELECT * FROM truncate_normal;
color
-------
1
(1 row)
SELECT * FROM test.show_subtables('"two_Partitions"');
Child | Tablespace
----------------------------------------+------------
_timescaledb_internal._hyper_1_5_chunk |
_timescaledb_internal._hyper_1_6_chunk |
_timescaledb_internal._hyper_1_7_chunk |
(3 rows)
TRUNCATE "two_Partitions", truncate_normal CASCADE;
WARNING: FIRING trigger when: BEFORE level: STATEMENT op: TRUNCATE cnt: <NULL> trigger_name _test_truncate_before
WARNING: FIRING trigger when: AFTER level: STATEMENT op: TRUNCATE cnt: <NULL> trigger_name _test_truncate_after
-- should be empty
SELECT * FROM test.show_subtables('"two_Partitions"');
Child | Tablespace
-------+------------
(0 rows)
SELECT * FROM "two_Partitions";
timeCustom | device_id | series_0 | series_1 | series_2 | series_bool
------------+-----------+----------+----------+----------+-------------
(0 rows)
SELECT * FROM truncate_normal;
color
-------
(0 rows)