From 3b4ceaab2d7c484f3b38bc43bb15ada63f918c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabr=C3=ADzio=20de=20Royes=20Mello?= Date: Fri, 2 Feb 2024 11:38:46 -0300 Subject: [PATCH] Cleanup invalid chunk metadata --- sql/updates/post-update.sql | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/sql/updates/post-update.sql b/sql/updates/post-update.sql index 0dd2d8a8fc7..d5e66dbc932 100644 --- a/sql/updates/post-update.sql +++ b/sql/updates/post-update.sql @@ -172,3 +172,52 @@ $$; -- Repair relations that have relacl entries for users that do not -- exist in pg_authid CALL _timescaledb_functions.repair_relation_acls(); + +-- Cleanup metadata for deleted chunks +CREATE UNLOGGED TABLE _timescaledb_catalog._chunks_remove AS +SELECT id FROM _timescaledb_catalog.chunk +WHERE dropped IS TRUE +AND NOT EXISTS ( + SELECT FROM information_schema.tables + WHERE tables.table_schema = chunk.schema_name + AND tables.table_name = chunk.table_name +) +AND NOT EXISTS ( + SELECT FROM _timescaledb_catalog.hypertable + JOIN _timescaledb_catalog.continuous_agg ON continuous_agg.raw_hypertable_id = hypertable.id + WHERE hypertable.id = chunk.hypertable_id + -- for the old caggs format we need to keep chunk metadata for dropped chunks + AND continuous_agg.finalized IS FALSE +); + +WITH _dimension_slice_remove AS ( + DELETE FROM _timescaledb_catalog.dimension_slice + USING _timescaledb_catalog.chunk_constraint, _timescaledb_catalog._chunks_remove + WHERE dimension_slice.id = chunk_constraint.dimension_slice_id + AND chunk_constraint.chunk_id = _chunks_remove.id + RETURNING _timescaledb_catalog.dimension_slice.id +) +DELETE FROM _timescaledb_catalog.chunk_constraint +USING _dimension_slice_remove +WHERE chunk_constraint.dimension_slice_id = _dimension_slice_remove.id; + +DELETE FROM _timescaledb_internal.bgw_policy_chunk_stats +USING _timescaledb_catalog._chunks_remove +WHERE bgw_policy_chunk_stats.chunk_id = _chunks_remove.id; + +DELETE FROM _timescaledb_catalog.chunk_index +USING _timescaledb_catalog._chunks_remove +WHERE chunk_index.chunk_id = _chunks_remove.id; + +DELETE FROM _timescaledb_catalog.compression_chunk_size +USING _timescaledb_catalog._chunks_remove +WHERE compression_chunk_size.chunk_id = _chunks_remove.id +OR compression_chunk_size.compressed_chunk_id = _chunks_remove.id; + +DELETE FROM _timescaledb_catalog.chunk +USING _timescaledb_catalog._chunks_remove +WHERE chunk.id = _chunks_remove.id +OR chunk.compressed_chunk_id = _chunks_remove.id; + +ALTER EXTENSION timescaledb DROP TABLE _timescaledb_catalog._chunks_remove; +DROP TABLE _timescaledb_catalog._chunks_remove;