Skip to content

Commit 562cab6

Browse files
committed
introduce function copy_foreign_keys(), generate fkeys on children
1 parent 1bcb666 commit 562cab6

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

hash.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ BEGIN
6363
EXECUTE format('ALTER TABLE %s ADD CONSTRAINT %s
6464
CHECK (@extschema@.get_hash_part_idx(%s(%s), %s) = %s)',
6565
v_child_relname,
66-
@extschema@.build_check_constraint_name(v_child_relname::regclass,
66+
@extschema@.build_check_constraint_name(v_child_relname::REGCLASS,
6767
attribute),
6868
v_hashfunc,
6969
attribute,
7070
partitions_count,
7171
partnum);
72+
73+
PERFORM @extschema@.copy_foreign_keys(parent_relid, v_child_relname::REGCLASS);
7274
END LOOP;
7375

7476
/* Notify backend about changes */

init.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,31 @@ $$ LANGUAGE plpgsql
546546
SET pg_pathman.enable_partitionfilter = off; /* ensures that PartitionFilter is OFF */
547547

548548

549+
/*
550+
* Copy all of parent's foreign keys.
551+
*/
552+
CREATE OR REPLACE FUNCTION @extschema@.copy_foreign_keys(
553+
parent_relid REGCLASS,
554+
partition REGCLASS)
555+
RETURNS VOID AS
556+
$$
557+
DECLARE
558+
rec RECORD;
559+
560+
BEGIN
561+
PERFORM @extschema@.validate_relname(parent_relid);
562+
PERFORM @extschema@.validate_relname(partition);
563+
564+
FOR rec IN (SELECT oid as conid FROM pg_catalog.pg_constraint
565+
WHERE conrelid = parent_relid AND contype = 'f')
566+
LOOP
567+
EXECUTE format('ALTER TABLE %s ADD %s',
568+
partition::TEXT,
569+
pg_get_constraintdef(rec.conid));
570+
END LOOP;
571+
END
572+
$$ LANGUAGE plpgsql;
573+
549574

550575
/*
551576
* Create DDL trigger to call pathman_ddl_trigger_func().

range.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ BEGIN
478478
p_start_value,
479479
p_end_value));
480480

481+
PERFORM @extschema@.copy_foreign_keys(parent_relid, v_child_relname::REGCLASS);
482+
481483
RETURN v_child_relname;
482484
END
483485
$$ LANGUAGE plpgsql

0 commit comments

Comments
 (0)