pgsql: Refuse ATTACH of a table referenced by a foreign key

From: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Refuse ATTACH of a table referenced by a foreign key
Date: 2024-08-08 23:36:46
Message-ID: E1scCgY-003Kt0-MI@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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 <exclusion(at)gmail(dot)com>
Reviewed-by: Tender Wang <tndrwang(at)gmail(dot)com>
Discussion: https://postgr.es/m/18541-628a61bc267cd2d3@postgresql.org

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a90bdd7a44d088a10b2faf5d7cdb85b8e4f0c662

Modified Files
--------------
src/backend/commands/tablecmds.c | 17 +++++++++++++++++
src/test/regress/expected/foreign_key.out | 17 +++++++++++++++++
src/test/regress/sql/foreign_key.sql | 17 +++++++++++++++++
3 files changed, 51 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Alvaro Herrera 2024-08-09 00:38:42 pgsql: libpq: Add suppress argument to pqTraceOutputNchar
Previous Message Alvaro Herrera 2024-08-08 19:18:52 pgsql: Refactor error messages to reduce duplication