Refuse ATTACH of a table referenced by a foreign key
authorAlvaro Herrera <[email protected]>
Thu, 8 Aug 2024 23:35:13 +0000 (19:35 -0400)
committerAlvaro Herrera <[email protected]>
Thu, 8 Aug 2024 23:35:13 +0000 (19:35 -0400)
commite97121d90e91032e9ff5d963202e835c96f298dd
tree76f445c34535534cc241e2042c119b32a093146a
parentbb5592caceded690451869ac098b55dbb2142dfc
Refuse ATTACH of a table referenced by a foreign key

Trying to attach a table as a partition which is already on the
referenced side of a foreign key on the partitioned table that it is
being attached to, leads to strange behavior: we try to clone the
foreign key from the parent to the partition, but this new FK points to
the partition itself, and the mix of pg_constraint rows and triggers
doesn't behave well.

Rather than trying to untangle the mess (which might be possible given
sufficient time), I opted to forbid the ATTACH.  This doesn't seem a
problematic restriction, given that we already fail to create the
foreign key if you do it the other way around, that is, having the
partition first and the FK second.

Backpatch to all supported branches.

Reported-by: Alexander Lakhin <[email protected]>
Reviewed-by: Tender Wang <[email protected]>
Discussion: https://postgr.es/m/18541-628a61bc267cd2d3@postgresql.org
src/backend/commands/tablecmds.c
src/test/regress/expected/foreign_key.out
src/test/regress/sql/foreign_key.sql