Skip to content

Commit

Permalink
Fix removal of metadata function and update script
Browse files Browse the repository at this point in the history
Changing the code to remove the assumption of 1:1
mapping between chunks and chunk constraints. Instead,
we look for orphaned metadata and remove if there is
no reference to the chunk or other metadata identifiers.
  • Loading branch information
antekresic committed Jun 4, 2024
1 parent f34c293 commit aeb56fe
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 57 deletions.
59 changes: 31 additions & 28 deletions sql/maintenance_utils.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ DECLARE
_chunk_id INTEGER;
_removed INTEGER := 0;
BEGIN
FOR _chunk_id IN
SELECT id FROM _timescaledb_catalog.chunk
DELETE FROM _timescaledb_catalog.chunk
WHERE hypertable_id = _hypertable_id
AND dropped IS TRUE
AND NOT EXISTS (
Expand All @@ -134,39 +133,43 @@ BEGIN
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
)
LOOP
_removed := _removed + 1;
RAISE INFO 'Removing metadata of chunk % from hypertable %', _chunk_id, _hypertable_id;

WITH _dimension_slice_remove AS (
DELETE FROM _timescaledb_catalog.dimension_slice
USING _timescaledb_catalog.chunk_constraint
WHERE dimension_slice.id = chunk_constraint.dimension_slice_id
AND chunk_constraint.chunk_id = _chunk_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;
);
GET DIAGNOSTICS _removed = ROW_COUNT;

-- delete orphaned chunk constraints
DELETE FROM _timescaledb_catalog.chunk_constraint
WHERE chunk_constraint.chunk_id = _chunk_id;

WHERE chunk_constraint.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

-- delete orphaned dimension slices
DELETE FROM _timescaledb_catalog.dimension_slice
WHERE dimension_slice.id NOT IN (
SELECT dimension_slice_id
FROM _timescaledb_catalog.chunk_constraint
);

-- delete orphaned bgw policy stats
DELETE FROM _timescaledb_internal.bgw_policy_chunk_stats
WHERE bgw_policy_chunk_stats.chunk_id = _chunk_id;
WHERE bgw_policy_chunk_stats.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

-- delete orphaned chunk indexes
DELETE FROM _timescaledb_catalog.chunk_index
WHERE chunk_index.chunk_id = _chunk_id;
WHERE chunk_index.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

-- delete orphaned compression chunk sizes
DELETE FROM _timescaledb_catalog.compression_chunk_size
WHERE compression_chunk_size.chunk_id = _chunk_id
OR compression_chunk_size.compressed_chunk_id = _chunk_id;

DELETE FROM _timescaledb_catalog.chunk
WHERE chunk.id = _chunk_id
OR chunk.compressed_chunk_id = _chunk_id;
END LOOP;
WHERE compression_chunk_size.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

RETURN _removed;
END;
Expand Down
60 changes: 31 additions & 29 deletions sql/updates/2.14.2--2.15.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ DROP FUNCTION IF EXISTS _timescaledb_functions.hypertable_invalidation_log_delet
CREATE FUNCTION _timescaledb_functions.remove_dropped_chunk_metadata(_hypertable_id INTEGER)
RETURNS INTEGER LANGUAGE plpgsql AS $$
DECLARE
_chunk_id INTEGER;
_removed INTEGER := 0;
BEGIN
FOR _chunk_id IN
SELECT id FROM _timescaledb_catalog.chunk
DELETE FROM _timescaledb_catalog.chunk
WHERE hypertable_id = _hypertable_id
AND dropped IS TRUE
AND NOT EXISTS (
Expand All @@ -39,39 +37,43 @@ BEGIN
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
)
LOOP
_removed := _removed + 1;
RAISE INFO 'Removing metadata of chunk % from hypertable %', _chunk_id, _hypertable_id;

WITH _dimension_slice_remove AS (
DELETE FROM _timescaledb_catalog.dimension_slice
USING _timescaledb_catalog.chunk_constraint
WHERE dimension_slice.id = chunk_constraint.dimension_slice_id
AND chunk_constraint.chunk_id = _chunk_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;
);
GET DIAGNOSTICS _removed = ROW_COUNT;

-- delete orphaned chunk constraints
DELETE FROM _timescaledb_catalog.chunk_constraint
WHERE chunk_constraint.chunk_id = _chunk_id;

WHERE chunk_constraint.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

-- delete orphaned dimension slices
DELETE FROM _timescaledb_catalog.dimension_slice
WHERE dimension_slice.id NOT IN (
SELECT dimension_slice_id
FROM _timescaledb_catalog.chunk_constraint
);

-- delete orphaned bgw policy stats
DELETE FROM _timescaledb_internal.bgw_policy_chunk_stats
WHERE bgw_policy_chunk_stats.chunk_id = _chunk_id;
WHERE bgw_policy_chunk_stats.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

-- delete orphaned chunk indexes
DELETE FROM _timescaledb_catalog.chunk_index
WHERE chunk_index.chunk_id = _chunk_id;
WHERE chunk_index.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

-- delete orphaned compression chunk sizes
DELETE FROM _timescaledb_catalog.compression_chunk_size
WHERE compression_chunk_size.chunk_id = _chunk_id
OR compression_chunk_size.compressed_chunk_id = _chunk_id;

DELETE FROM _timescaledb_catalog.chunk
WHERE chunk.id = _chunk_id
OR chunk.compressed_chunk_id = _chunk_id;
END LOOP;
WHERE compression_chunk_size.chunk_id NOT IN (
SELECT id
FROM _timescaledb_catalog.chunk
);

RETURN _removed;
END;
Expand Down

0 comments on commit aeb56fe

Please sign in to comment.