@@ -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
1314DO $$
1415DECLARE
1516 r RECORD;
1617BEGIN
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;
3539END
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- --
5650ALTER TABLE _timescaledb_catalog .hypertable SET (user_catalog_table = true);
5751ALTER TABLE _timescaledb_catalog .chunk SET (user_catalog_table = true);
52+
0 commit comments