Change integral drop_chunks() to use BIGINT

Previously drop_chunks() only took INTEGER, which prevented it
from being called with BIGINT values, e.g. for nanoseconds.
This commit is contained in:
Rob Kiefer 2017-10-17 12:34:53 -04:00 committed by RobAtticus
parent 9e0422ab7e
commit fbd4349234
5 changed files with 123 additions and 6 deletions

View File

@ -208,10 +208,10 @@ END
$BODY$;
CREATE OR REPLACE FUNCTION drop_chunks(
older_than INTEGER,
older_than BIGINT,
table_name NAME = NULL,
schema_name NAME = NULL,
cascade BOOLEAN = FALSE
cascade BOOLEAN = FALSE
)
RETURNS VOID LANGUAGE PLPGSQL VOLATILE AS
$BODY$

View File

@ -274,9 +274,10 @@ CREATE OR REPLACE FUNCTION _timescaledb_internal.drop_chunks_impl(
)
RETURNS VOID LANGUAGE PLPGSQL VOLATILE AS
$BODY$
DECLARE
DECLARE
chunk_row _timescaledb_catalog.chunk;
cascade_mod TEXT = '';
exist_count INT = 0;
BEGIN
IF older_than_time IS NULL AND table_name IS NULL AND schema_name IS NULL THEN
RAISE 'Cannot have all 3 arguments to drop_chunks_older_than be NULL';
@ -286,6 +287,19 @@ BEGIN
cascade_mod = 'CASCADE';
END IF;
IF table_name IS NOT NULL THEN
SELECT COUNT(*)
FROM _timescaledb_catalog.hypertable h
WHERE (drop_chunks_impl.schema_name IS NULL OR h.schema_name = drop_chunks_impl.schema_name)
AND drop_chunks_impl.table_name = h.table_name
INTO STRICT exist_count;
IF exist_count = 0 THEN
RAISE 'hypertable % does not exist', drop_chunks_impl.table_name
USING ERRCODE = 'IO001';
END IF;
END IF;
FOR chunk_row IN SELECT *
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.hypertable h ON (h.id = c.hypertable_id)

View File

@ -0,0 +1 @@
DROP FUNCTION IF EXISTS drop_chunks(INTEGER, NAME, NAME, BOOLEAN);

View File

@ -1,5 +1,6 @@
CREATE TABLE PUBLIC.drop_chunk_test1(time bigint, temp float8, device_id text);
CREATE TABLE PUBLIC.drop_chunk_test2(time bigint, temp float8, device_id text);
CREATE TABLE PUBLIC.drop_chunk_test3(time bigint, temp float8, device_id text);
CREATE INDEX ON drop_chunk_test1(time DESC);
SELECT create_hypertable('public.drop_chunk_test1', 'time', chunk_time_interval => 1, create_default_indexes=>false);
create_hypertable
@ -13,6 +14,12 @@ SELECT create_hypertable('public.drop_chunk_test2', 'time', chunk_time_interval
(1 row)
SELECT create_hypertable('public.drop_chunk_test3', 'time', chunk_time_interval => 1, create_default_indexes=>false);
create_hypertable
-------------------
(1 row)
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.hypertable h ON (c.hypertable_id = h.id)
@ -54,6 +61,12 @@ INSERT INTO PUBLIC.drop_chunk_test2 VALUES(3, 3.0, 'dev1');
INSERT INTO PUBLIC.drop_chunk_test2 VALUES(4, 4.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test2 VALUES(5, 5.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test2 VALUES(6, 6.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(1, 1.0, 'dev1');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(2, 2.0, 'dev1');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(3, 3.0, 'dev1');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(4, 4.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(5, 5.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(6, 6.0, 'dev7');
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.hypertable h ON (c.hypertable_id = h.id)
@ -93,7 +106,13 @@ WHERE h.schema_name = 'public' AND (h.table_name = 'drop_chunk_test1' OR h.table
_timescaledb_internal | _hyper_2_7_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_8_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_9_chunk | table | default_perm_user
(12 rows)
_timescaledb_internal | _hyper_3_13_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_14_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_15_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_16_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_17_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_18_chunk | table | default_perm_user
(18 rows)
CREATE VIEW dependent_view AS SELECT * FROM _timescaledb_internal._hyper_1_1_chunk;
\set ON_ERROR_STOP 0
@ -142,7 +161,12 @@ WHERE h.schema_name = 'public' AND (h.table_name = 'drop_chunk_test1' OR h.table
_timescaledb_internal | _hyper_2_12_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_8_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_9_chunk | table | default_perm_user
(10 rows)
_timescaledb_internal | _hyper_3_14_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_15_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_16_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_17_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_18_chunk | table | default_perm_user
(15 rows)
SELECT drop_chunks(3, 'drop_chunk_test1');
drop_chunks
@ -183,8 +207,60 @@ WHERE h.schema_name = 'public' AND (h.table_name = 'drop_chunk_test1' OR h.table
_timescaledb_internal | _hyper_2_12_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_8_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_9_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_14_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_15_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_16_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_17_chunk | table | default_perm_user
_timescaledb_internal | _hyper_3_18_chunk | table | default_perm_user
(14 rows)
-- 2,147,483,647 is the largest int so this tests that BIGINTs work
SELECT drop_chunks(2147483648, 'drop_chunk_test3');
drop_chunks
-------------
(1 row)
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.hypertable h ON (c.hypertable_id = h.id)
INNER JOIN _timescaledb_internal.dimension_get_time(h.id) time_dimension ON(true)
INNER JOIN _timescaledb_catalog.dimension_slice ds ON (ds.dimension_id = time_dimension.id)
INNER JOIN _timescaledb_catalog.chunk_constraint cc ON (cc.dimension_slice_id = ds.id AND cc.chunk_id = c.id)
WHERE h.schema_name = 'public' AND (h.table_name = 'drop_chunk_test1' OR h.table_name = 'drop_chunk_test2' OR h.table_name = 'drop_chunk_test3');
chunk_id | hypertable_id | chunk_schema | chunk_table | range_start | range_end
----------+---------------+-----------------------+-------------------+-------------+-----------
3 | 1 | _timescaledb_internal | _hyper_1_3_chunk | 3 | 4
4 | 1 | _timescaledb_internal | _hyper_1_4_chunk | 4 | 5
5 | 1 | _timescaledb_internal | _hyper_1_5_chunk | 5 | 6
6 | 1 | _timescaledb_internal | _hyper_1_6_chunk | 6 | 7
8 | 2 | _timescaledb_internal | _hyper_2_8_chunk | 2 | 3
9 | 2 | _timescaledb_internal | _hyper_2_9_chunk | 3 | 4
10 | 2 | _timescaledb_internal | _hyper_2_10_chunk | 4 | 5
11 | 2 | _timescaledb_internal | _hyper_2_11_chunk | 5 | 6
12 | 2 | _timescaledb_internal | _hyper_2_12_chunk | 6 | 7
(9 rows)
\dt "_timescaledb_internal".*
List of relations
Schema | Name | Type | Owner
-----------------------+-------------------+-------+-------------------
_timescaledb_internal | _hyper_1_3_chunk | table | default_perm_user
_timescaledb_internal | _hyper_1_4_chunk | table | default_perm_user
_timescaledb_internal | _hyper_1_5_chunk | table | default_perm_user
_timescaledb_internal | _hyper_1_6_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_10_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_11_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_12_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_8_chunk | table | default_perm_user
_timescaledb_internal | _hyper_2_9_chunk | table | default_perm_user
(9 rows)
-- should error because no hypertable
\set ON_ERROR_STOP 0
SELECT drop_chunks(5, 'drop_chunk_test4');
ERROR: hypertable drop_chunk_test4 does not exist
\set ON_ERROR_STOP 1
DROP TABLE _timescaledb_internal._hyper_1_6_chunk;
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
FROM _timescaledb_catalog.chunk c

View File

@ -1,8 +1,10 @@
CREATE TABLE PUBLIC.drop_chunk_test1(time bigint, temp float8, device_id text);
CREATE TABLE PUBLIC.drop_chunk_test2(time bigint, temp float8, device_id text);
CREATE TABLE PUBLIC.drop_chunk_test3(time bigint, temp float8, device_id text);
CREATE INDEX ON drop_chunk_test1(time DESC);
SELECT create_hypertable('public.drop_chunk_test1', 'time', chunk_time_interval => 1, create_default_indexes=>false);
SELECT create_hypertable('public.drop_chunk_test2', 'time', chunk_time_interval => 1, create_default_indexes=>false);
SELECT create_hypertable('public.drop_chunk_test3', 'time', chunk_time_interval => 1, create_default_indexes=>false);
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
FROM _timescaledb_catalog.chunk c
@ -31,6 +33,13 @@ INSERT INTO PUBLIC.drop_chunk_test2 VALUES(4, 4.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test2 VALUES(5, 5.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test2 VALUES(6, 6.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(1, 1.0, 'dev1');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(2, 2.0, 'dev1');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(3, 3.0, 'dev1');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(4, 4.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(5, 5.0, 'dev7');
INSERT INTO PUBLIC.drop_chunk_test3 VALUES(6, 6.0, 'dev7');
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.hypertable h ON (c.hypertable_id = h.id)
@ -70,6 +79,24 @@ WHERE h.schema_name = 'public' AND (h.table_name = 'drop_chunk_test1' OR h.table
\dt "_timescaledb_internal".*
-- 2,147,483,647 is the largest int so this tests that BIGINTs work
SELECT drop_chunks(2147483648, 'drop_chunk_test3');
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
FROM _timescaledb_catalog.chunk c
INNER JOIN _timescaledb_catalog.hypertable h ON (c.hypertable_id = h.id)
INNER JOIN _timescaledb_internal.dimension_get_time(h.id) time_dimension ON(true)
INNER JOIN _timescaledb_catalog.dimension_slice ds ON (ds.dimension_id = time_dimension.id)
INNER JOIN _timescaledb_catalog.chunk_constraint cc ON (cc.dimension_slice_id = ds.id AND cc.chunk_id = c.id)
WHERE h.schema_name = 'public' AND (h.table_name = 'drop_chunk_test1' OR h.table_name = 'drop_chunk_test2' OR h.table_name = 'drop_chunk_test3');
\dt "_timescaledb_internal".*
-- should error because no hypertable
\set ON_ERROR_STOP 0
SELECT drop_chunks(5, 'drop_chunk_test4');
\set ON_ERROR_STOP 1
DROP TABLE _timescaledb_internal._hyper_1_6_chunk;
SELECT c.id AS chunk_id, c.hypertable_id, c.schema_name AS chunk_schema, c.table_name AS chunk_table, ds.range_start, ds.range_end
@ -81,4 +108,3 @@ INNER JOIN _timescaledb_catalog.chunk_constraint cc ON (cc.dimension_slice_id =
WHERE h.schema_name = 'public' AND (h.table_name = 'drop_chunk_test1' OR h.table_name = 'drop_chunk_test2');
\dt "_timescaledb_internal".*