Skip to content

Commit

Permalink
Remove chunks when truncating a CAgg
Browse files Browse the repository at this point in the history
When truncating a regular hypertable all chunks are removed but for
some reason it don't happen when truncating a continuous aggregate.

Improved it by removing the underlying materialization hypertable
chunks when truncating a continuous aggregate.
  • Loading branch information
fabriziomello committed May 17, 2024
1 parent 54830d1 commit f7255f0
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 48 deletions.
4 changes: 4 additions & 0 deletions src/process_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,10 @@ process_truncate(ProcessUtilityArgs *args)

/* list of materialization hypertables to reset the watermark */
mat_hypertables = lappend(mat_hypertables, mat_ht);

/* include the materialization hypertable to the list to be handled by the
* proper hypertable and chunk truncate code-path later */
hypertables = lappend(hypertables, mat_ht);
}

list_append = true;
Expand Down
16 changes: 15 additions & 1 deletion tsl/test/expected/cagg_ddl-14.out
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,22 @@ SELECT _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark
Fri Nov 02 17:00:00 2018 PDT
(1 row)

-- Exists chunks before truncate the cagg (> 0)
SELECT count(*) FROM show_chunks('conditions_daily');
count
-------
2
(1 row)

-- Truncate the given CAgg, it should reset the watermark to the empty state
TRUNCATE conditions_daily;
-- No chunks remains after truncate the cagg (= 0)
SELECT count(*) FROM show_chunks('conditions_daily');
count
-------
0
(1 row)

-- Watermark should be reseted
SELECT _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:mat_hypertable_id)) AS watermak_after;
watermak_after
Expand Down Expand Up @@ -2096,7 +2110,7 @@ SELECT count(compress_chunk(ch)) FROM show_chunks('cagg1') ch;
(1 row)

DROP MATERIALIZED VIEW cagg1;
NOTICE: drop cascades to table _timescaledb_internal._hyper_52_68_chunk
NOTICE: drop cascades to table _timescaledb_internal._hyper_52_70_chunk
SELECT * FROM _timescaledb_catalog.compression_settings;
relid | segmentby | orderby | orderby_desc | orderby_nullsfirst
-------+-----------+---------+--------------+--------------------
Expand Down
16 changes: 15 additions & 1 deletion tsl/test/expected/cagg_ddl-15.out
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,22 @@ SELECT _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark
Fri Nov 02 17:00:00 2018 PDT
(1 row)

-- Exists chunks before truncate the cagg (> 0)
SELECT count(*) FROM show_chunks('conditions_daily');
count
-------
2
(1 row)

-- Truncate the given CAgg, it should reset the watermark to the empty state
TRUNCATE conditions_daily;
-- No chunks remains after truncate the cagg (= 0)
SELECT count(*) FROM show_chunks('conditions_daily');
count
-------
0
(1 row)

-- Watermark should be reseted
SELECT _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:mat_hypertable_id)) AS watermak_after;
watermak_after
Expand Down Expand Up @@ -2096,7 +2110,7 @@ SELECT count(compress_chunk(ch)) FROM show_chunks('cagg1') ch;
(1 row)

DROP MATERIALIZED VIEW cagg1;
NOTICE: drop cascades to table _timescaledb_internal._hyper_52_68_chunk
NOTICE: drop cascades to table _timescaledb_internal._hyper_52_70_chunk
SELECT * FROM _timescaledb_catalog.compression_settings;
relid | segmentby | orderby | orderby_desc | orderby_nullsfirst
-------+-----------+---------+--------------+--------------------
Expand Down
16 changes: 15 additions & 1 deletion tsl/test/expected/cagg_ddl-16.out
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,22 @@ SELECT _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark
Fri Nov 02 17:00:00 2018 PDT
(1 row)

-- Exists chunks before truncate the cagg (> 0)
SELECT count(*) FROM show_chunks('conditions_daily');
count
-------
2
(1 row)

-- Truncate the given CAgg, it should reset the watermark to the empty state
TRUNCATE conditions_daily;
-- No chunks remains after truncate the cagg (= 0)
SELECT count(*) FROM show_chunks('conditions_daily');
count
-------
0
(1 row)

-- Watermark should be reseted
SELECT _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:mat_hypertable_id)) AS watermak_after;
watermak_after
Expand Down Expand Up @@ -2096,7 +2110,7 @@ SELECT count(compress_chunk(ch)) FROM show_chunks('cagg1') ch;
(1 row)

DROP MATERIALIZED VIEW cagg1;
NOTICE: drop cascades to table _timescaledb_internal._hyper_52_68_chunk
NOTICE: drop cascades to table _timescaledb_internal._hyper_52_70_chunk
SELECT * FROM _timescaledb_catalog.compression_settings;
relid | segmentby | orderby | orderby_desc | orderby_nullsfirst
-------+-----------+---------+--------------+--------------------
Expand Down
4 changes: 2 additions & 2 deletions tsl/test/expected/cagg_invalidation.out
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,8 @@ SELECT show_chunks AS chunk_to_drop
FROM show_chunks('conditions');
chunk_to_drop
-----------------------------------------
_timescaledb_internal._hyper_1_34_chunk
_timescaledb_internal._hyper_1_40_chunk
_timescaledb_internal._hyper_1_35_chunk
_timescaledb_internal._hyper_1_41_chunk
(2 rows)

-- Pick the first one to drop
Expand Down
46 changes: 20 additions & 26 deletions tsl/test/expected/cagg_on_cagg.out
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,6 @@ SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;

-- DROP the 2TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:187: NOTICE: drop cascades to table _timescaledb_internal._hyper_3_3_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;
Expand All @@ -413,7 +412,7 @@ SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;

-- DROP the first CAGG should work
DROP MATERIALIZED VIEW :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_2_2_chunk
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_2_7_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -762,7 +761,7 @@ psql:include/cagg_on_cagg_common.sql:169: NOTICE: continuous aggregate "conditi
\set ON_ERROR_STOP 1
-- DROP the 3TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_8_9_chunk
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_8_11_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_3TH_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -790,7 +789,6 @@ SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;

-- DROP the 2TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:187: NOTICE: drop cascades to table _timescaledb_internal._hyper_7_8_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;
Expand All @@ -810,7 +808,7 @@ SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;

-- DROP the first CAGG should work
DROP MATERIALIZED VIEW :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_6_7_chunk
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_6_14_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -1485,7 +1483,7 @@ psql:include/cagg_on_cagg_common.sql:169: NOTICE: continuous aggregate "conditi
\set ON_ERROR_STOP 1
-- DROP the 3TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_16_14_chunk
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_16_18_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_3TH_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -1513,7 +1511,6 @@ SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;

-- DROP the 2TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:187: NOTICE: drop cascades to table _timescaledb_internal._hyper_15_13_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;
Expand All @@ -1533,7 +1530,7 @@ SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;

-- DROP the first CAGG should work
DROP MATERIALIZED VIEW :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_14_12_chunk
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_14_20_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -1878,7 +1875,7 @@ psql:include/cagg_on_cagg_common.sql:169: NOTICE: continuous aggregate "conditi
\set ON_ERROR_STOP 1
-- DROP the 3TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_20_18_chunk
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_20_24_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_3TH_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -1906,7 +1903,6 @@ SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;

-- DROP the 2TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:187: NOTICE: drop cascades to table _timescaledb_internal._hyper_19_17_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;
Expand All @@ -1926,7 +1922,7 @@ SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;

-- DROP the first CAGG should work
DROP MATERIALIZED VIEW :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_18_16_chunk
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_18_26_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -2701,7 +2697,7 @@ psql:include/cagg_on_cagg_common.sql:169: NOTICE: continuous aggregate "conditi
\set ON_ERROR_STOP 1
-- DROP the 3TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_29_22_chunk
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_29_30_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_3TH_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -2729,7 +2725,6 @@ SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;

-- DROP the 2TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:187: NOTICE: drop cascades to table _timescaledb_internal._hyper_28_21_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;
Expand All @@ -2749,7 +2744,7 @@ SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;

-- DROP the first CAGG should work
DROP MATERIALIZED VIEW :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_27_20_chunk
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_27_32_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -3093,7 +3088,7 @@ psql:include/cagg_on_cagg_common.sql:169: NOTICE: continuous aggregate "conditi
\set ON_ERROR_STOP 1
-- DROP the 3TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_33_26_chunk
psql:include/cagg_on_cagg_common.sql:173: NOTICE: drop cascades to table _timescaledb_internal._hyper_33_36_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_3TH_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -3121,7 +3116,6 @@ SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;

-- DROP the 2TH level CAGG don't affect others
DROP MATERIALIZED VIEW :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_common.sql:187: NOTICE: drop cascades to table _timescaledb_internal._hyper_32_25_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_2TH_LEVEL ORDER BY bucket;
Expand All @@ -3141,7 +3135,7 @@ SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;

-- DROP the first CAGG should work
DROP MATERIALIZED VIEW :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_31_24_chunk
psql:include/cagg_on_cagg_common.sql:197: NOTICE: drop cascades to table _timescaledb_internal._hyper_31_38_chunk
\set ON_ERROR_STOP 0
-- should error because it was dropped
SELECT * FROM :CAGG_NAME_1ST_LEVEL ORDER BY bucket;
Expand Down Expand Up @@ -4022,16 +4016,16 @@ UNION ALL
(1 row)

DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_validations.sql:79: NOTICE: drop cascades to table _timescaledb_internal._hyper_46_31_chunk
psql:include/cagg_on_cagg_validations.sql:79: NOTICE: drop cascades to table _timescaledb_internal._hyper_46_43_chunk
DELETE FROM conditions WHERE device_id = 4;
\endif
--
-- Cleanup
--
DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_validations.sql:86: NOTICE: drop cascades to table _timescaledb_internal._hyper_45_30_chunk
psql:include/cagg_on_cagg_validations.sql:86: NOTICE: drop cascades to table _timescaledb_internal._hyper_45_42_chunk
DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_validations.sql:87: NOTICE: drop cascades to table _timescaledb_internal._hyper_44_29_chunk
psql:include/cagg_on_cagg_validations.sql:87: NOTICE: drop cascades to table _timescaledb_internal._hyper_44_41_chunk
\set INTERVAL_TEST FALSE
--
-- Variable bucket size with different timezones
Expand Down Expand Up @@ -4275,16 +4269,16 @@ UNION ALL
(1 row)

DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_validations.sql:79: NOTICE: drop cascades to table _timescaledb_internal._hyper_51_34_chunk
psql:include/cagg_on_cagg_validations.sql:79: NOTICE: drop cascades to table _timescaledb_internal._hyper_51_46_chunk
DELETE FROM conditions WHERE device_id = 4;
\endif
--
-- Cleanup
--
DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_validations.sql:86: NOTICE: drop cascades to table _timescaledb_internal._hyper_50_33_chunk
psql:include/cagg_on_cagg_validations.sql:86: NOTICE: drop cascades to table _timescaledb_internal._hyper_50_45_chunk
DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_validations.sql:87: NOTICE: drop cascades to table _timescaledb_internal._hyper_49_32_chunk
psql:include/cagg_on_cagg_validations.sql:87: NOTICE: drop cascades to table _timescaledb_internal._hyper_49_44_chunk
\set INTERVAL_TEST FALSE
--
-- TZ bucket on top of non-TZ bucket
Expand Down Expand Up @@ -4529,16 +4523,16 @@ UNION ALL
(1 row)

DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_3TH_LEVEL;
psql:include/cagg_on_cagg_validations.sql:79: NOTICE: drop cascades to table _timescaledb_internal._hyper_56_37_chunk
psql:include/cagg_on_cagg_validations.sql:79: NOTICE: drop cascades to table _timescaledb_internal._hyper_56_49_chunk
DELETE FROM conditions WHERE device_id = 4;
\endif
--
-- Cleanup
--
DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_2TH_LEVEL;
psql:include/cagg_on_cagg_validations.sql:86: NOTICE: drop cascades to table _timescaledb_internal._hyper_55_36_chunk
psql:include/cagg_on_cagg_validations.sql:86: NOTICE: drop cascades to table _timescaledb_internal._hyper_55_48_chunk
DROP MATERIALIZED VIEW IF EXISTS :CAGG_NAME_1ST_LEVEL;
psql:include/cagg_on_cagg_validations.sql:87: NOTICE: drop cascades to table _timescaledb_internal._hyper_54_35_chunk
psql:include/cagg_on_cagg_validations.sql:87: NOTICE: drop cascades to table _timescaledb_internal._hyper_54_47_chunk
\set INTERVAL_TEST FALSE
--
-- non-TZ bucket on top of TZ bucket
Expand Down
Loading

0 comments on commit f7255f0

Please sign in to comment.