pgsql: Enforce foreign key correctly during cross-partition updates - Mailing list pgsql-committers

From Alvaro Herrera
Subject pgsql: Enforce foreign key correctly during cross-partition updates
Date
Msg-id E1nVzfM-000bAp-9u@gemulon.postgresql.org
Whole thread Raw
Responses Re: pgsql: Enforce foreign key correctly during cross-partition updates  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-committers
Enforce foreign key correctly during cross-partition updates

When an update on a partitioned table referenced in foreign key
constraints causes a row to move from one partition to another,
the fact that the move is implemented as a delete followed by an insert
on the target partition causes the foreign key triggers to have
surprising behavior.  For example, a given foreign key's delete trigger
which implements the ON DELETE CASCADE clause of that key will delete
any referencing rows when triggered for that internal DELETE, although
it should not, because the referenced row is simply being moved from one
partition of the referenced root partitioned table into another, not
being deleted from it.

This commit teaches trigger.c to skip queuing such delete trigger events
on the leaf partitions in favor of an UPDATE event fired on the root
target relation.  Doing so is sensible because both the old and the new
tuple "logically" belong to the root relation.

The after trigger event queuing interface now allows passing the source
and the target partitions of a particular cross-partition update when
registering the update event for the root partitioned table.  Along with
the two ctids of the old and the new tuple, the after trigger event now
also stores the OIDs of those partitions. The tuples fetched from the
source and the target partitions are converted into the root table
format, if necessary, before they are passed to the trigger function.

The implementation currently has a limitation that only the foreign keys
pointing into the query's target relation are considered, not those of
its sub-partitioned partitions.  That seems like a reasonable
limitation, because it sounds rare to have distinct foreign keys
pointing to sub-partitioned partitions instead of to the root table.

This misbehavior stems from commit f56f8f8da6af (which added support for
foreign keys to reference partitioned tables) not paying sufficient
attention to commit 2f178441044b (which had introduced cross-partition
updates a year earlier).  Even though the former commit goes back to
Postgres 12, we're not backpatching this fix at this time for fear of
destabilizing things too much, and because there are a few ABI breaks in
it that we'd have to work around in older branches.  It also depends on
commit f4566345cf40, which had its own share of backpatchability issues
as well.

Author: Amit Langote <amitlangote09@gmail.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org>
Reported-by: Eduard Català <eduard.catala@gmail.com>
Discussion: https://postgr.es/m/CA+HiwqFvkBCmfwkQX_yBqv2Wz8ugUGiBDxum8=WvVbfU1TXaNg@mail.gmail.com
Discussion: https://postgr.es/m/CAL54xNZsLwEM1XCk5yW9EqaRzsZYHuWsHQkA2L5MOSKXAwviCQ@mail.gmail.com

Branch
------
master

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

Modified Files
--------------
doc/src/sgml/ref/update.sgml              |   7 +
src/backend/commands/trigger.c            | 394 +++++++++++++++++++++++++-----
src/backend/executor/execMain.c           |  86 ++++++-
src/backend/executor/execReplication.c    |   5 +-
src/backend/executor/nodeModifyTable.c    | 151 +++++++++++-
src/backend/utils/adt/ri_triggers.c       |   6 +
src/include/commands/trigger.h            |   8 +-
src/include/executor/executor.h           |   4 +-
src/include/nodes/execnodes.h             |   6 +
src/test/regress/expected/foreign_key.out | 204 +++++++++++++++-
src/test/regress/sql/foreign_key.sql      | 135 +++++++++-
11 files changed, 926 insertions(+), 80 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Doc: fix our example systemd script.
Next
From: Tom Lane
Date:
Subject: pgsql: psql: handle tab completion of timezone names after "SET TIMEZON