diff --git a/sql/views.sql b/sql/views.sql index 3d3f5ae51..2067012e0 100644 --- a/sql/views.sql +++ b/sql/views.sql @@ -206,7 +206,7 @@ WITH mapq as mapq.compressed_toast_bytes, mapq.compressed_total_bytes FROM _timescaledb_catalog.hypertable as srcht JOIN _timescaledb_catalog.chunk as srcch - ON srcht.id = srcch.hypertable_id and srcht.compressed_hypertable_id IS NOT NULL + ON srcht.id = srcch.hypertable_id and srcht.compressed_hypertable_id IS NOT NULL and srcch.dropped = false LEFT JOIN mapq ON srcch.id = mapq.chunk_id ; diff --git a/test/sql/views.sql b/test/sql/views.sql index fc1d24d02..5a43c22b2 100644 --- a/test/sql/views.sql +++ b/test/sql/views.sql @@ -46,4 +46,3 @@ SELECT * FROM timescaledb_information.hypertable WHERE table_name = 'ht1' ORDER -- filter by owner SELECT * FROM timescaledb_information.hypertable WHERE table_owner = 'super_user' ORDER BY table_schema,table_name; - diff --git a/tsl/test/expected/compression.out b/tsl/test/expected/compression.out index 82c904101..12056fb68 100644 --- a/tsl/test/expected/compression.out +++ b/tsl/test/expected/compression.out @@ -966,7 +966,7 @@ ERROR: cannot update/delete rows from chunk "_hyper_16_36_chunk" as it is compr -- Test FK constraint drop and recreate during compression and decompression on a chunk CREATE TABLE meta (device_id INT PRIMARY KEY); CREATE TABLE hyper( - time INT NOT NULL, + time INT NOT NULL, device_id INT REFERENCES meta(device_id) ON DELETE CASCADE ON UPDATE CASCADE, val INT); SELECT * FROM create_hypertable('hyper', 'time', chunk_time_interval => 10); @@ -976,18 +976,18 @@ SELECT * FROM create_hypertable('hyper', 'time', chunk_time_interval => 10); (1 row) ALTER TABLE hyper SET ( - timescaledb.compress, + timescaledb.compress, timescaledb.compress_orderby = 'time', timescaledb.compress_segmentby = 'device_id'); NOTICE: adding index _compressed_hypertable_19_device_id__ts_meta_sequence_num_idx ON _timescaledb_internal._compressed_hypertable_19 USING BTREE(device_id, _ts_meta_sequence_num) INSERT INTO meta VALUES (1), (2), (3), (4), (5); INSERT INTO hyper VALUES (1, 1, 1), (2, 2, 1), (3, 3, 1), (10, 3, 2), (11, 4, 2), (11, 5, 2); SELECT ch1.table_name AS "CHUNK_NAME", ch1.schema_name|| '.' || ch1.table_name AS "CHUNK_FULL_NAME" -FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht -WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper' +FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht +WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper' ORDER BY ch1.id LIMIT 1 \gset SELECT constraint_schema, constraint_name, table_schema, table_name, constraint_type -FROM information_schema.table_constraints +FROM information_schema.table_constraints WHERE table_name = :'CHUNK_NAME' AND constraint_type = 'FOREIGN KEY' ORDER BY constraint_name; constraint_schema | constraint_name | table_schema | table_name | constraint_type @@ -1002,7 +1002,7 @@ SELECT compress_chunk(:'CHUNK_FULL_NAME'); (1 row) SELECT constraint_schema, constraint_name, table_schema, table_name, constraint_type -FROM information_schema.table_constraints +FROM information_schema.table_constraints WHERE table_name = :'CHUNK_NAME' AND constraint_type = 'FOREIGN KEY' ORDER BY constraint_name; constraint_schema | constraint_name | table_schema | table_name | constraint_type @@ -1043,7 +1043,7 @@ SELECT decompress_chunk(:'CHUNK_FULL_NAME'); (1 row) SELECT constraint_schema, constraint_name, table_schema, table_name, constraint_type -FROM information_schema.table_constraints +FROM information_schema.table_constraints WHERE table_name = :'CHUNK_NAME' AND constraint_type = 'FOREIGN KEY' ORDER BY constraint_name; constraint_schema | constraint_name | table_schema | table_name | constraint_type @@ -1051,3 +1051,36 @@ ORDER BY constraint_name; _timescaledb_internal | 42_9_hyper_device_id_fkey | _timescaledb_internal | _hyper_18_42_chunk | FOREIGN KEY (1 row) +-- create hypertable with 2 chunks +CREATE TABLE ht5(time TIMESTAMPTZ NOT NULL); +SELECT create_hypertable('ht5','time'); + create_hypertable +------------------- + (20,public,ht5,t) +(1 row) + +INSERT INTO ht5 SELECT '2000-01-01'::TIMESTAMPTZ; +INSERT INTO ht5 SELECT '2001-01-01'::TIMESTAMPTZ; +-- compressed_chunk_stats should not show dropped chunks +ALTER TABLE ht5 SET (timescaledb.compress); +SELECT compress_chunk(i) FROM show_chunks('ht5') i; + compress_chunk +------------------------------------------ + _timescaledb_internal._hyper_20_45_chunk + _timescaledb_internal._hyper_20_46_chunk +(2 rows) + +SELECT drop_chunks(table_name => 'ht5', newer_than => '2000-01-01'::TIMESTAMPTZ); + drop_chunks +------------------------------------------ + _timescaledb_internal._hyper_20_46_chunk +(1 row) + +SELECT chunk_name +FROM timescaledb_information.compressed_chunk_stats +WHERE hypertable_name = 'ht5'::regclass; + chunk_name +------------------------------------------ + _timescaledb_internal._hyper_20_45_chunk +(1 row) + diff --git a/tsl/test/sql/compression.sql b/tsl/test/sql/compression.sql index 8f200b981..7aa4f9072 100644 --- a/tsl/test/sql/compression.sql +++ b/tsl/test/sql/compression.sql @@ -374,31 +374,31 @@ WHERE rescan_test.id = tmp.id AND rescan_test.t = tmp.t; CREATE TABLE meta (device_id INT PRIMARY KEY); CREATE TABLE hyper( - time INT NOT NULL, + time INT NOT NULL, device_id INT REFERENCES meta(device_id) ON DELETE CASCADE ON UPDATE CASCADE, val INT); SELECT * FROM create_hypertable('hyper', 'time', chunk_time_interval => 10); ALTER TABLE hyper SET ( - timescaledb.compress, + timescaledb.compress, timescaledb.compress_orderby = 'time', timescaledb.compress_segmentby = 'device_id'); INSERT INTO meta VALUES (1), (2), (3), (4), (5); INSERT INTO hyper VALUES (1, 1, 1), (2, 2, 1), (3, 3, 1), (10, 3, 2), (11, 4, 2), (11, 5, 2); SELECT ch1.table_name AS "CHUNK_NAME", ch1.schema_name|| '.' || ch1.table_name AS "CHUNK_FULL_NAME" -FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht -WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper' +FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht +WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper' ORDER BY ch1.id LIMIT 1 \gset SELECT constraint_schema, constraint_name, table_schema, table_name, constraint_type -FROM information_schema.table_constraints +FROM information_schema.table_constraints WHERE table_name = :'CHUNK_NAME' AND constraint_type = 'FOREIGN KEY' ORDER BY constraint_name; SELECT compress_chunk(:'CHUNK_FULL_NAME'); SELECT constraint_schema, constraint_name, table_schema, table_name, constraint_type -FROM information_schema.table_constraints +FROM information_schema.table_constraints WHERE table_name = :'CHUNK_NAME' AND constraint_type = 'FOREIGN KEY' ORDER BY constraint_name; @@ -415,6 +415,20 @@ SELECT * FROM hyper ORDER BY time, device_id; SELECT decompress_chunk(:'CHUNK_FULL_NAME'); SELECT constraint_schema, constraint_name, table_schema, table_name, constraint_type -FROM information_schema.table_constraints +FROM information_schema.table_constraints WHERE table_name = :'CHUNK_NAME' AND constraint_type = 'FOREIGN KEY' ORDER BY constraint_name; + +-- create hypertable with 2 chunks +CREATE TABLE ht5(time TIMESTAMPTZ NOT NULL); +SELECT create_hypertable('ht5','time'); +INSERT INTO ht5 SELECT '2000-01-01'::TIMESTAMPTZ; +INSERT INTO ht5 SELECT '2001-01-01'::TIMESTAMPTZ; + +-- compressed_chunk_stats should not show dropped chunks +ALTER TABLE ht5 SET (timescaledb.compress); +SELECT compress_chunk(i) FROM show_chunks('ht5') i; +SELECT drop_chunks(table_name => 'ht5', newer_than => '2000-01-01'::TIMESTAMPTZ); +SELECT chunk_name +FROM timescaledb_information.compressed_chunk_stats +WHERE hypertable_name = 'ht5'::regclass;