]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix deferred FK check batching introduced by commit b7b27eb41a5
authorAmit Langote <amitlan@postgresql.org>
Mon, 6 Apr 2026 23:36:49 +0000 (08:36 +0900)
committerAmit Langote <amitlan@postgresql.org>
Tue, 7 Apr 2026 01:45:59 +0000 (10:45 +0900)
commit5c54c3ed1b934a35a8e9eacff679699ccb3a5070
treefc7107fdc6690be89692191e96015c12b27eabb7
parent98979578055ffba3bd520adcec00881f72fc2861
Fix deferred FK check batching introduced by commit b7b27eb41a5

That commit introduced AfterTriggerIsActive() to detect whether
we are inside the after-trigger firing machinery, so that RI trigger
functions can take the batched fast path.  It was implemented using
query_depth >= 0, which correctly identified immediate trigger firing
but missed the deferred case where query_depth is -1 at COMMIT via
AfterTriggerFireDeferred().  This caused deferred FK checks to fall
back to the per-row fast path instead of the batched path.

The correct check is whether we are inside an after-trigger firing
loop specifically.  Introduce afterTriggerFiringDepth, a counter
incremented around the trigger-firing loops in AfterTriggerEndQuery,
AfterTriggerFireDeferred, and AfterTriggerSetState, and decremented
after FireAfterTriggerBatchCallbacks() returns.  AfterTriggerIsActive()
now returns afterTriggerFiringDepth > 0.

Reported-by: Chao Li <li.evan.chao@gmail.com>
Author: Chao Li <li.evan.chao@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Discussion: https://postgr.es/m/C2133B47-79CD-40FF-B088-02D20D654806@gmail.com
src/backend/commands/trigger.c