Skip to content

Commit d4747b6

Browse files
committed
Remove chunk_constraint catalog tracking for non-dimensional constraints
Rely on postgres for tracking non-dimensional constraints
1 parent b4d5f46 commit d4747b6

46 files changed

Lines changed: 477 additions & 563 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.unreleased/pr_9893

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implements: #9893 Remove chunk_constraint catalog tracking for non-dimensional constraints

sql/updates/latest-dev.sql

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,45 @@ WHERE cc.chunk_id = c.id
88
AND cc.hypertable_constraint_name IS NOT NULL
99
AND cc.constraint_name = cc.hypertable_constraint_name;
1010

11-
-- Rename legacy <chunk_id>_<seq>_<parent> chunk-side FKs to the parent name
12-
-- so the event-trigger hooks can locate them by name on DROP/RENAME.
11+
-- Rename legacy chunk-side constraints to the names the new code recomputes:
12+
-- FKs use the parent's name; unique/PK/exclusion/trigger use the deterministic
13+
-- "<chunk_id>_<parent>" form
1314
DO $$
1415
DECLARE
1516
r RECORD;
1617
BEGIN
1718
FOR r IN
1819
SELECT pg_catalog.format('%I.%I', c.schema_name, c.table_name) AS chunk_table,
1920
cc.constraint_name AS old_name,
20-
cc.hypertable_constraint_name AS new_name
21+
CASE WHEN parent.contype = 'f' THEN cc.hypertable_constraint_name
22+
ELSE format('%s_%s', c.id, cc.hypertable_constraint_name)
23+
END AS new_name
2124
FROM _timescaledb_catalog.chunk_constraint cc
2225
JOIN _timescaledb_catalog.chunk c ON c.id = cc.chunk_id
2326
JOIN _timescaledb_catalog.hypertable ht ON ht.id = c.hypertable_id
2427
JOIN pg_constraint parent
2528
ON parent.conrelid = pg_catalog.format('%I.%I', ht.schema_name, ht.table_name)::regclass
2629
AND parent.conname = cc.hypertable_constraint_name
27-
AND parent.contype = 'f'
30+
AND parent.contype IN ('f', 'u', 'p', 'x', 't')
2831
WHERE cc.dimension_slice_id IS NULL
2932
AND cc.hypertable_constraint_name IS NOT NULL
30-
AND cc.constraint_name <> cc.hypertable_constraint_name
3133
LOOP
32-
EXECUTE pg_catalog.format('ALTER TABLE %s RENAME CONSTRAINT %I TO %I',
33-
r.chunk_table, r.old_name, r.new_name);
34+
IF r.old_name <> r.new_name THEN
35+
EXECUTE pg_catalog.format('ALTER TABLE %s RENAME CONSTRAINT %I TO %I',
36+
r.chunk_table, r.old_name, r.new_name);
37+
END IF;
3438
END LOOP;
3539
END
3640
$$;
3741

38-
-- Drop the obsolete chunk_constraint rows for outbound FKs; the event-trigger
39-
-- hooks locate chunk-side FKs by name and no longer need this catalog.
40-
DELETE FROM _timescaledb_catalog.chunk_constraint cc
41-
USING _timescaledb_catalog.chunk c,
42-
_timescaledb_catalog.hypertable ht,
43-
pg_constraint parent
44-
WHERE cc.chunk_id = c.id
45-
AND ht.id = c.hypertable_id
46-
AND parent.conrelid = pg_catalog.format('%I.%I', ht.schema_name, ht.table_name)::regclass
47-
AND parent.conname = cc.hypertable_constraint_name
48-
AND parent.contype = 'f'
49-
AND cc.dimension_slice_id IS NULL
50-
AND cc.hypertable_constraint_name IS NOT NULL;
42+
-- Remove the chunk_constraint rows that mirrored non-dimensional constraints.
43+
-- FK chunk-side constraints are now located by name through the event-trigger
44+
-- hooks; unique/PK/exclusion/trigger constraints through the deterministic
45+
-- "<chunk_id>_<parent>" name pattern.
46+
DELETE FROM _timescaledb_catalog.chunk_constraint
47+
WHERE dimension_slice_id IS NULL
48+
AND hypertable_constraint_name IS NOT NULL;
5149

52-
--
53-
-- Include the `hypertable` and the `chunk` catalog tables in the historical
54-
-- snapshot during logical replication
55-
--
5650
ALTER TABLE _timescaledb_catalog.hypertable SET (user_catalog_table = true);
5751
ALTER TABLE _timescaledb_catalog.chunk SET (user_catalog_table = true);
52+

sql/updates/reverse-dev.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,21 @@ JOIN pg_constraint child
2626
AND child.conname = parent.conname
2727
ON CONFLICT DO NOTHING;
2828

29+
-- Restore chunk_constraint rows for unique/PK/exclusion/trigger constraints.
30+
-- The chunk-side conname is the deterministic "<chunk_id>_<parent>" form
31+
INSERT INTO _timescaledb_catalog.chunk_constraint
32+
(chunk_id, dimension_slice_id, constraint_name, hypertable_constraint_name)
33+
SELECT c.id, NULL, child.conname, parent.conname
34+
FROM _timescaledb_catalog.chunk c
35+
JOIN _timescaledb_catalog.hypertable ht ON ht.id = c.hypertable_id
36+
JOIN pg_constraint parent
37+
ON parent.conrelid = pg_catalog.format('%I.%I', ht.schema_name, ht.table_name)::regclass
38+
AND parent.contype IN ('u', 'p', 'x', 't')
39+
JOIN pg_constraint child
40+
ON child.conrelid = pg_catalog.format('%I.%I', c.schema_name, c.table_name)::regclass
41+
AND child.conname = format('%s_%s', c.id, parent.conname)
42+
ON CONFLICT DO NOTHING;
43+
2944
ALTER TABLE _timescaledb_catalog.hypertable RESET (user_catalog_table);
3045
ALTER TABLE _timescaledb_catalog.chunk RESET (user_catalog_table);
46+

src/chunk.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3218,11 +3218,11 @@ chunk_tuple_delete(TupleInfo *ti, Oid relid, DropBehavior behavior, bool detach)
32183218
}
32193219

32203220
/*
3221-
* Even tough we keep foreign key constraints on the chunk, we still
3222-
* need to drop the referencing foreign keys since such keys are possibly
3223-
* intended to reference the hypertable, not the chunk.
3221+
* Drop FKs that reference this chunk. They target the chunk's PK index
3222+
* directly, so without explicit removal the index would block the chunk
3223+
* drop. Skip when the relation is already gone (cascade from hypertable).
32243224
*/
3225-
if (detach)
3225+
if (detach && OidIsValid(relid) && SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relid)))
32263226
{
32273227
ts_chunk_drop_referencing_fk_by_chunk_id(form.id);
32283228
}

0 commit comments

Comments
 (0)