Skip to content

Commit 415b3ce

Browse files
author
Antonin Houska
committed
Support creation of FK in NOT VALID state for partitioned tables.
If the PG server version is at least 18, the foreign key in NOT VALID state can be created. (If the partitioned table is on the PK side, the FK constraint in NOT VALID state can be created even in earlier PG versions.)
1 parent df82d45 commit 415b3ce

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,16 @@ table. It's recommended to handle constraints creation this way:
166166

167167
5. FOREIGN KEY constraints are also created automatically (according to the
168168
source table) and need to be validated using the `ALTER TABLE ... VALIDATE
169-
CONSTRAINT ...` command, unless the referencing table is partitioned: the
170-
NOT VALID option is currently not supported for partitioned tables.
171-
172-
Therefore, if the referencing table is partitioned, you need to use the
173-
`ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY ...` command after
174-
`rewrite_table()` has finished. Please run the command as soon as possible
175-
to minimize the risk that applications modify the database in a way that
176-
violates the constraints.
169+
CONSTRAINT ...` command, unless the referencing table is partitioned and
170+
the version of PostgreSQL server is 17 or lower: those versions do not
171+
support the NOT VALID option for partitioned tables.
172+
173+
Therefore, if the referencing table is partitioned and if the server
174+
version is 17 or lower, you need to use the `ALTER TABLE ... ADD
175+
CONSTRAINT ... FOREIGN KEY ...` command after `rewrite_table()` has
176+
finished. Please run the command as soon as possible to minimize the risk
177+
that applications modify the database in a way that violates the
178+
constraints.
177179

178180
6. Drop all foreign keys involving the source table.
179181

expected/pg_rewrite.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ TABLE tab4;
227227

228228
-- One more test for "manual" validation of FKs, this time we rewrite the PK
229229
-- table. The NOT VALID constraint cannot be used if the FK table is
230-
-- partitioned, so we need a separate test.
230+
-- partitioned and if PG version is < 18, so we need a separate test.
231231
CREATE TABLE tab1_pk(i int primary key);
232232
INSERT INTO tab1_pk(i) VALUES (1);
233233
CREATE TABLE tab1_pk_new(i bigint primary key);

pg_rewrite.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,9 +3260,10 @@ dump_fk_constraint(HeapTuple tup, Oid relid_dst, const char *relname_dst,
32603260

32613261
if (con->conrelid == relid_src)
32623262
{
3263+
#if PG_VERSION_NUM < 180000
32633264
/*
3264-
* ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY ... NOT VALID is
3265-
* currently not supported for partitioned FK table.
3265+
* ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY ... NOT VALID is not
3266+
* supported for partitioned FK table in PG < 18.
32663267
*/
32673268
if (get_rel_relkind(relid_dst) == RELKIND_PARTITIONED_TABLE)
32683269
{
@@ -3272,6 +3273,7 @@ dump_fk_constraint(HeapTuple tup, Oid relid_dst, const char *relname_dst,
32723273
NULL);
32733274
return;
32743275
}
3276+
#endif
32753277

32763278
fknsp = get_namespace_name(relid_dst);
32773279
fkrelname = relname_dst;

sql/pg_rewrite.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ TABLE tab4;
9292

9393
-- One more test for "manual" validation of FKs, this time we rewrite the PK
9494
-- table. The NOT VALID constraint cannot be used if the FK table is
95-
-- partitioned, so we need a separate test.
95+
-- partitioned and if PG version is < 18, so we need a separate test.
9696
CREATE TABLE tab1_pk(i int primary key);
9797
INSERT INTO tab1_pk(i) VALUES (1);
9898
CREATE TABLE tab1_pk_new(i bigint primary key);

0 commit comments

Comments
 (0)