forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitba9a7e3
committed
Enforce foreign key correctly during cross-partition updates
When an update on a partitioned table referenced in foreign keyconstraints causes a row to move from one partition to another,the fact that the move is implemented as a delete followed by an inserton the target partition causes the foreign key triggers to havesurprising behavior. For example, a given foreign key's delete triggerwhich implements the ON DELETE CASCADE clause of that key will deleteany referencing rows when triggered for that internal DELETE, althoughit should not, because the referenced row is simply being moved from onepartition of the referenced root partitioned table into another, notbeing deleted from it.This commit teaches trigger.c to skip queuing such delete trigger eventson the leaf partitions in favor of an UPDATE event fired on the roottarget relation. Doing so is sensible because both the old and the newtuple "logically" belong to the root relation.The after trigger event queuing interface now allows passing the sourceand the target partitions of a particular cross-partition update whenregistering the update event for the root partitioned table. Along withthe two ctids of the old and the new tuple, the after trigger event nowalso stores the OIDs of those partitions. The tuples fetched from thesource and the target partitions are converted into the root tableformat, if necessary, before they are passed to the trigger function.The implementation currently has a limitation that only the foreign keyspointing into the query's target relation are considered, not those ofits sub-partitioned partitions. That seems like a reasonablelimitation, because it sounds rare to have distinct foreign keyspointing to sub-partitioned partitions instead of to the root table.This misbehavior stems from commitf56f8f8 (which added support forforeign keys to reference partitioned tables) not paying sufficientattention to commit2f17844 (which had introduced cross-partitionupdates a year earlier). Even though the former commit goes back toPostgres 12, we're not backpatching this fix at this time for fear ofdestabilizing things too much, and because there are a few ABI breaks init that we'd have to work around in older branches. It also depends oncommitf456634, which had its own share of backpatchability issuesas 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.comDiscussion:https://postgr.es/m/CAL54xNZsLwEM1XCk5yW9EqaRzsZYHuWsHQkA2L5MOSKXAwviCQ@mail.gmail.com1 parent3f513ac commitba9a7e3
File tree
11 files changed
+926
-80
lines changed- doc/src/sgml/ref
- src
- backend
- commands
- executor
- utils/adt
- include
- commands
- executor
- nodes
- test/regress
- expected
- sql
11 files changed
+926
-80
lines changedLines changed: 7 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
316 | 316 |
| |
317 | 317 |
| |
318 | 318 |
| |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
319 | 326 |
| |
320 | 327 |
| |
321 | 328 |
| |
|
0 commit comments
Comments
(0)