Add api _timescaledb_internal.drop_chunk

Add an internal api to drop a single chunk.
This function drops the storage and metadata
associated with the chunk.
Note that chunk dependencies are not affected.
e.g. Continuous aggs are not updated when this chunk
is dropped.
This commit is contained in:
gayyappan 2022-04-06 11:57:33 -04:00 committed by gayyappan
parent b32ccd0363
commit 5d56b1cdbc
8 changed files with 198 additions and 0 deletions

View File

@ -84,3 +84,9 @@ RETURNS BOOL AS '@MODULE_PATHNAME@', 'ts_chunk_create_empty_table' LANGUAGE C VO
CREATE OR REPLACE FUNCTION _timescaledb_internal.freeze_chunk(
chunk REGCLASS)
RETURNS BOOL AS '@MODULE_PATHNAME@', 'ts_chunk_freeze_chunk' LANGUAGE C VOLATILE;
--wrapper for ts_chunk_drop
--drops the chunk table and its entry in the chunk catalog
CREATE OR REPLACE FUNCTION _timescaledb_internal.drop_chunk(
chunk REGCLASS)
RETURNS BOOL AS '@MODULE_PATHNAME@', 'ts_chunk_drop_single_chunk' LANGUAGE C VOLATILE;

View File

@ -3,6 +3,7 @@ DROP FUNCTION _timescaledb_internal.relation_size(relation REGCLASS);
DROP INDEX _timescaledb_catalog.chunk_constraint_dimension_slice_id_idx;
CREATE INDEX chunk_constraint_chunk_id_dimension_slice_id_idx ON _timescaledb_catalog.chunk_constraint (chunk_id, dimension_slice_id);
DROP FUNCTION _timescaledb_internal.freeze_chunk(chunk REGCLASS);
DROP FUNCTION _timescaledb_internal.drop_chunk(chunk REGCLASS);
DO
$$

View File

@ -68,6 +68,7 @@
TS_FUNCTION_INFO_V1(ts_chunk_show_chunks);
TS_FUNCTION_INFO_V1(ts_chunk_drop_chunks);
TS_FUNCTION_INFO_V1(ts_chunk_drop_single_chunk);
TS_FUNCTION_INFO_V1(ts_chunk_freeze_chunk);
TS_FUNCTION_INFO_V1(ts_chunks_in);
TS_FUNCTION_INFO_V1(ts_chunk_id_from_relid);
@ -3987,6 +3988,23 @@ ts_chunk_freeze_chunk(PG_FUNCTION_ARGS)
PG_RETURN_BOOL(ret);
}
Datum
ts_chunk_drop_single_chunk(PG_FUNCTION_ARGS)
{
Oid chunk_relid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
char *chunk_table_name = get_rel_name(chunk_relid);
char *chunk_schema_name = get_namespace_name(get_rel_namespace(chunk_relid));
const Chunk *ch = ts_chunk_get_by_name_with_memory_context(chunk_schema_name,
chunk_table_name,
CurrentMemoryContext,
true);
Assert(ch != NULL);
/* do not drop any chunk dependencies */
ts_chunk_drop(ch, DROP_RESTRICT, LOG);
PG_RETURN_BOOL(true);
}
Datum
ts_chunk_drop_chunks(PG_FUNCTION_ARGS)
{

View File

@ -1415,3 +1415,38 @@ CREATE TABLE PUBLIC.drop_chunk_test4(time bigint, temp float8, device_id text);
CREATE TABLE drop_chunks_table_id AS SELECT hypertable_id
FROM create_hypertable('public.drop_chunk_test4', 'time', chunk_time_interval => 1);
NOTICE: adding not-null constraint to column "time"
-- TEST for internal api that drops a single chunk
-- this drops the table and removes entry from the catalog.
-- does not affect any materialized cagg data
INSERT INTO test1.hyper1 VALUES (20, 0.5);
SELECT chunk_schema as "CHSCHEMA", chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name ;
CHSCHEMA | CHNAME
-----------------------+--------------------
_timescaledb_internal | _hyper_12_37_chunk
_timescaledb_internal | _hyper_12_41_chunk
(2 rows)
--drop one of the chunks
SELECT chunk_schema || '.' || chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name LIMIT 1
\gset
SELECT _timescaledb_internal.drop_chunk(:'CHNAME');
drop_chunk
------------
t
(1 row)
SELECT chunk_schema as "CHSCHEMA", chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name ;
CHSCHEMA | CHNAME
-----------------------+--------------------
_timescaledb_internal | _hyper_12_41_chunk
(1 row)

View File

@ -599,3 +599,24 @@ SELECT show_chunks('test3.hyperx');
CREATE TABLE PUBLIC.drop_chunk_test4(time bigint, temp float8, device_id text);
CREATE TABLE drop_chunks_table_id AS SELECT hypertable_id
FROM create_hypertable('public.drop_chunk_test4', 'time', chunk_time_interval => 1);
-- TEST for internal api that drops a single chunk
-- this drops the table and removes entry from the catalog.
-- does not affect any materialized cagg data
INSERT INTO test1.hyper1 VALUES (20, 0.5);
SELECT chunk_schema as "CHSCHEMA", chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name ;
--drop one of the chunks
SELECT chunk_schema || '.' || chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name LIMIT 1
\gset
SELECT _timescaledb_internal.drop_chunk(:'CHNAME');
SELECT chunk_schema as "CHSCHEMA", chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name ;

View File

@ -2,6 +2,8 @@
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
-- These tests work for PG14 or greater
-- Remember to corordinate any changes to freeze_chunk functionality with the Cloud
-- Storage team.
\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE SCHEMA test1;
GRANT CREATE ON SCHEMA test1 TO :ROLE_DEFAULT_PERM_USER;
@ -84,6 +86,12 @@ SELECT * from test1.hyper1 ORDER BY 1;
(3 rows)
\set ON_ERROR_STOP 1
SELECT _timescaledb_internal.drop_chunk( :'CHNAME');
drop_chunk
------------
t
(1 row)
-- TEST freeze_chunk api on a chunk that is 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);
@ -193,9 +201,82 @@ SELECT _timescaledb_internal.freeze_chunk( :'CHNAME');
SELECT compress_chunk( :'CHNAME');
ERROR: compress_chunk not permitted on frozen chunk "_hyper_2_7_chunk"
\set ON_ERROR_STOP 1
--TEST dropping a frozen chunk
--DO NOT CHANGE this behavior ---
-- frozen chunks can be dropped.
SELECT _timescaledb_internal.drop_chunk(:'CHNAME');
drop_chunk
------------
t
(1 row)
SELECT count(*)
FROM timescaledb_information.chunks
WHERE hypertable_name = 'table_to_compress' and hypertable_schema = 'public';
count
-------
0
(1 row)
--TEST error freeze a non-chunk
CREATE TABLE nochunk_tab( a timestamp, b integer);
\set ON_ERROR_STOP 0
SELECT _timescaledb_internal.freeze_chunk('nochunk_tab');
ERROR: chunk not found
\set ON_ERROR_STOP 1
--TEST dropping frozen chunk in the presence of caggs
SELECT * FROM test1.hyper1 ORDER BY 1;
time | temp
------+------
30 | 0.5
31 | 31
(2 rows)
CREATE OR REPLACE FUNCTION hyper_dummy_now() RETURNS BIGINT
LANGUAGE SQL IMMUTABLE AS 'SELECT 100::BIGINT';
SELECT set_integer_now_func('test1.hyper1', 'hyper_dummy_now');
set_integer_now_func
----------------------
(1 row)
CREATE MATERIALIZED VIEW hyper1_cagg WITH (timescaledb.continuous)
AS SELECT time_bucket( 5, "time") as bucket, count(*)
FROM test1.hyper1 GROUP BY 1;
NOTICE: refreshing continuous aggregate "hyper1_cagg"
SELECT * FROM hyper1_cagg ORDER BY 1;
bucket | count
--------+-------
30 | 2
(1 row)
--now freeze chunk and drop it
--cagg data is unaffected
SELECT chunk_schema || '.' || chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name LIMIT 1
\gset
SELECT _timescaledb_internal.freeze_chunk( :'CHNAME');
freeze_chunk
--------------
t
(1 row)
SELECT _timescaledb_internal.drop_chunk( :'CHNAME');
drop_chunk
------------
t
(1 row)
SELECT * from test1.hyper1 ORDER BY 1;
time | temp
------+------
(0 rows)
SELECT * FROM hyper1_cagg ORDER BY 1;
bucket | count
--------+-------
30 | 2
(1 row)

View File

@ -49,6 +49,7 @@ ORDER BY pronamespace::regnamespace::text COLLATE "C", p.oid::regprocedure::text
_timescaledb_internal.data_node_index_size(name,name,name)
_timescaledb_internal.dimension_is_finite(bigint)
_timescaledb_internal.dimension_slice_get_constraint_sql(integer)
_timescaledb_internal.drop_chunk(regclass)
_timescaledb_internal.drop_dist_ht_invalidation_trigger(integer)
_timescaledb_internal.finalize_agg(text,name,name,name[],bytea,anyelement)
_timescaledb_internal.finalize_agg_ffunc(internal,text,name,name,name[],bytea,anyelement)

View File

@ -3,6 +3,8 @@
-- LICENSE-TIMESCALE for a copy of the license.
-- These tests work for PG14 or greater
-- Remember to corordinate any changes to freeze_chunk functionality with the Cloud
-- Storage team.
\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE SCHEMA test1;
@ -51,6 +53,7 @@ INSERT INTO test1.hyper1 VALUES ( 11, 11);
INSERT INTO test1.hyper1 VALUES ( 31, 31);
SELECT * from test1.hyper1 ORDER BY 1;
\set ON_ERROR_STOP 1
SELECT _timescaledb_internal.drop_chunk( :'CHNAME');
-- TEST freeze_chunk api on a chunk that is compressed
CREATE TABLE public.table_to_compress (time date NOT NULL, acq_id bigint, value bigint);
@ -111,9 +114,41 @@ SELECT _timescaledb_internal.freeze_chunk( :'CHNAME');
SELECT compress_chunk( :'CHNAME');
\set ON_ERROR_STOP 1
--TEST dropping a frozen chunk
--DO NOT CHANGE this behavior ---
-- frozen chunks can be dropped.
SELECT _timescaledb_internal.drop_chunk(:'CHNAME');
SELECT count(*)
FROM timescaledb_information.chunks
WHERE hypertable_name = 'table_to_compress' and hypertable_schema = 'public';
--TEST error freeze a non-chunk
CREATE TABLE nochunk_tab( a timestamp, b integer);
\set ON_ERROR_STOP 0
SELECT _timescaledb_internal.freeze_chunk('nochunk_tab');
\set ON_ERROR_STOP 1
--TEST dropping frozen chunk in the presence of caggs
SELECT * FROM test1.hyper1 ORDER BY 1;
CREATE OR REPLACE FUNCTION hyper_dummy_now() RETURNS BIGINT
LANGUAGE SQL IMMUTABLE AS 'SELECT 100::BIGINT';
SELECT set_integer_now_func('test1.hyper1', 'hyper_dummy_now');
CREATE MATERIALIZED VIEW hyper1_cagg WITH (timescaledb.continuous)
AS SELECT time_bucket( 5, "time") as bucket, count(*)
FROM test1.hyper1 GROUP BY 1;
SELECT * FROM hyper1_cagg ORDER BY 1;
--now freeze chunk and drop it
--cagg data is unaffected
SELECT chunk_schema || '.' || chunk_name as "CHNAME"
FROM timescaledb_information.chunks
WHERE hypertable_name = 'hyper1' and hypertable_schema = 'test1'
ORDER BY chunk_name LIMIT 1
\gset
SELECT _timescaledb_internal.freeze_chunk( :'CHNAME');
SELECT _timescaledb_internal.drop_chunk( :'CHNAME');
SELECT * from test1.hyper1 ORDER BY 1;
SELECT * FROM hyper1_cagg ORDER BY 1;