mirror of
https://github.com/timescale/timescaledb.git
synced 2025-05-28 01:30:29 +08:00
This change removes, simplifies, and unifies code related to `drop_chunks` and `show_chunks`. As a result of prior changes to `drop_chunks`, e.g., making table relid mandatory and removing cascading options, there's an opportunity to clean up and simplify the rather complex code for dropping and showing chunks. In particular, `show_chunks` is now consistent with `drop_chunks`; the relid argument is mandatory, a continuous aggregate can be used in place of a hypertable, and the input time ranges are checked and handled in the same way. Unused code is also removed, for instance, code that cascaded drop chunks to continuous aggregates remained in the code base while the option no longer exists.
138 lines
5.3 KiB
Plaintext
138 lines
5.3 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.
|
|
--------------------------------------------------------------------------
|
|
-- show_chunks and drop_chunks functions on a compressed table
|
|
-- (issue https://github.com/timescale/timescaledb/issues/1535)
|
|
-- create a table that will not be compressed
|
|
CREATE TABLE public.uncompressed_table(time date NOT NULL, temp float8, device_id text);
|
|
CREATE INDEX ON public.uncompressed_table(time DESC);
|
|
SELECT create_hypertable('public.uncompressed_table', 'time', chunk_time_interval => interval '1 day');
|
|
create_hypertable
|
|
---------------------------------
|
|
(1,public,uncompressed_table,t)
|
|
(1 row)
|
|
|
|
INSERT INTO public.uncompressed_table VALUES('2020-03-01', 1.0, 'dev1');
|
|
INSERT INTO public.uncompressed_table VALUES('2020-03-05', 2.0, 'dev1');
|
|
INSERT INTO public.uncompressed_table VALUES('2020-03-07', 3.0, 'dev1');
|
|
INSERT INTO public.uncompressed_table VALUES('2020-03-08', 4.0, 'dev7');
|
|
INSERT INTO public.uncompressed_table VALUES('2020-03-09', 5.0, 'dev7');
|
|
INSERT INTO public.uncompressed_table VALUES('2020-03-10', 6.0, 'dev7');
|
|
-- create next table that is going to be compressed:
|
|
CREATE TABLE public.table_to_compress (time date NOT NULL, acq_id bigint, value bigint);
|
|
CREATE INDEX idx_table_to_compress_acq_id ON public.table_to_compress(acq_id);
|
|
SELECT create_hypertable('public.table_to_compress', 'time', chunk_time_interval => interval '1 day');
|
|
create_hypertable
|
|
--------------------------------
|
|
(2,public,table_to_compress,t)
|
|
(1 row)
|
|
|
|
ALTER TABLE public.table_to_compress SET (timescaledb.compress, timescaledb.compress_segmentby = 'acq_id');
|
|
NOTICE: adding index _compressed_hypertable_3_acq_id__ts_meta_sequence_num_idx ON _timescaledb_internal._compressed_hypertable_3 USING BTREE(acq_id, _ts_meta_sequence_num)
|
|
INSERT INTO public.table_to_compress VALUES ('2020-01-01', 1234567, 777888);
|
|
INSERT INTO public.table_to_compress VALUES ('2020-02-01', 567567, 890890);
|
|
INSERT INTO public.table_to_compress VALUES ('2020-02-10', 1234, 5678);
|
|
SELECT show_chunks('public.uncompressed_table');
|
|
show_chunks
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_1_1_chunk
|
|
_timescaledb_internal._hyper_1_2_chunk
|
|
_timescaledb_internal._hyper_1_3_chunk
|
|
_timescaledb_internal._hyper_1_4_chunk
|
|
_timescaledb_internal._hyper_1_5_chunk
|
|
_timescaledb_internal._hyper_1_6_chunk
|
|
(6 rows)
|
|
|
|
SELECT show_chunks('public.table_to_compress');
|
|
show_chunks
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_2_7_chunk
|
|
_timescaledb_internal._hyper_2_8_chunk
|
|
_timescaledb_internal._hyper_2_9_chunk
|
|
(3 rows)
|
|
|
|
SELECT show_chunks('public.table_to_compress', older_than=>'1 day'::interval);
|
|
show_chunks
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_2_7_chunk
|
|
_timescaledb_internal._hyper_2_8_chunk
|
|
_timescaledb_internal._hyper_2_9_chunk
|
|
(3 rows)
|
|
|
|
SELECT show_chunks('public.table_to_compress', newer_than=>'1 day'::interval);
|
|
show_chunks
|
|
-------------
|
|
(0 rows)
|
|
|
|
-- compress all chunks of the table:
|
|
SELECT compress_chunk(show_chunks('public.table_to_compress'));
|
|
compress_chunk
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_2_7_chunk
|
|
_timescaledb_internal._hyper_2_8_chunk
|
|
_timescaledb_internal._hyper_2_9_chunk
|
|
(3 rows)
|
|
|
|
-- and run the queries again to make sure results are the same
|
|
SELECT show_chunks('public.uncompressed_table');
|
|
show_chunks
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_1_1_chunk
|
|
_timescaledb_internal._hyper_1_2_chunk
|
|
_timescaledb_internal._hyper_1_3_chunk
|
|
_timescaledb_internal._hyper_1_4_chunk
|
|
_timescaledb_internal._hyper_1_5_chunk
|
|
_timescaledb_internal._hyper_1_6_chunk
|
|
(6 rows)
|
|
|
|
SELECT show_chunks('public.table_to_compress');
|
|
show_chunks
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_2_7_chunk
|
|
_timescaledb_internal._hyper_2_8_chunk
|
|
_timescaledb_internal._hyper_2_9_chunk
|
|
(3 rows)
|
|
|
|
SELECT show_chunks('public.table_to_compress', older_than=>'1 day'::interval);
|
|
show_chunks
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_2_7_chunk
|
|
_timescaledb_internal._hyper_2_8_chunk
|
|
_timescaledb_internal._hyper_2_9_chunk
|
|
(3 rows)
|
|
|
|
SELECT show_chunks('public.table_to_compress', newer_than=>'1 day'::interval);
|
|
show_chunks
|
|
-------------
|
|
(0 rows)
|
|
|
|
-- drop all hypertables' old chunks
|
|
SELECT drop_chunks(table_name::regclass, older_than=>'1 day'::interval)
|
|
FROM _timescaledb_catalog.hypertable
|
|
WHERE schema_name = current_schema()
|
|
ORDER BY table_name DESC;
|
|
drop_chunks
|
|
----------------------------------------
|
|
_timescaledb_internal._hyper_1_1_chunk
|
|
_timescaledb_internal._hyper_1_2_chunk
|
|
_timescaledb_internal._hyper_1_3_chunk
|
|
_timescaledb_internal._hyper_1_4_chunk
|
|
_timescaledb_internal._hyper_1_5_chunk
|
|
_timescaledb_internal._hyper_1_6_chunk
|
|
_timescaledb_internal._hyper_2_7_chunk
|
|
_timescaledb_internal._hyper_2_8_chunk
|
|
_timescaledb_internal._hyper_2_9_chunk
|
|
(9 rows)
|
|
|
|
SELECT show_chunks('public.uncompressed_table');
|
|
show_chunks
|
|
-------------
|
|
(0 rows)
|
|
|
|
SELECT show_chunks('public.table_to_compress');
|
|
show_chunks
|
|
-------------
|
|
(0 rows)
|
|
|