Commit bfb18f4
committed
Restore firing state at subtransaction end
AfterTriggerEndQuery(), AfterTriggerFireDeferred(), and
AfterTriggerSetState() bracket their firing loops with
firing_depth++/--, and FireAfterTriggerBatchCallbacks() brackets its
loop with firing_batch_callbacks. The closing step runs after the loop
and is not protected by PG_FINALLY, so if a trigger or batch callback
throws and the error is caught by a subtransaction (e.g. a PL/pgSQL
EXCEPTION block), firing_depth is left too high and/or
firing_batch_callbacks is left set for the rest of the transaction.
firing_depth feeds AfterTriggerIsActive(), which the RI fast path uses
to decide whether an FK check is running inside trigger firing (and
may batch). firing_depth left too high makes AfterTriggerIsActive()
wrongly report firing as active afterwards. This is reachable and
results in silent data corruption: after a caught FK-check error, an
ALTER TABLE ... ADD FOREIGN KEY whose validation runs per-row
(RI_Initial_Check() having bailed, e.g. because RLS is enabled on the
referenced table) calls RI_FKey_check() with AfterTriggerIsActive()
wrongly true, so the check is routed into the batched fast path. A
utility command has no AfterTriggerEndQuery() to fire the flush
callback, so the batch is never flushed: the violating row is not
reported, the constraint is marked validated, and the cached PK
relation and index leak.
Fix by restoring firing_depth and firing_batch_callbacks in
AfterTriggerEndSubXact() to the values saved at subtransaction start,
next to the existing query_depth handling. Restoring (rather than
zeroing/clearing) is required because a subtransaction can begin and
end while an outer query is firing, where firing_depth is legitimately
positive and firing_batch_callbacks legitimately set; forcing them to
0/false there breaks the outer firing (FireAfterTriggerBatchCallbacks()
asserts firing_depth > 0).
Reported-by: Noah Misch <noah@leadboat.com>
Discussion: https://postgr.es/m/20260705222115.be.noahmisch@microsoft.com
Backpatch-through: 191 parent 242899b commit bfb18f4
3 files changed
Lines changed: 106 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3946 | 3946 | | |
3947 | 3947 | | |
3948 | 3948 | | |
| 3949 | + | |
| 3950 | + | |
3949 | 3951 | | |
3950 | 3952 | | |
3951 | 3953 | | |
| |||
5507 | 5509 | | |
5508 | 5510 | | |
5509 | 5511 | | |
| 5512 | + | |
| 5513 | + | |
| 5514 | + | |
5510 | 5515 | | |
5511 | 5516 | | |
5512 | 5517 | | |
| |||
5607 | 5612 | | |
5608 | 5613 | | |
5609 | 5614 | | |
5610 | | - | |
5611 | | - | |
| 5615 | + | |
| 5616 | + | |
| 5617 | + | |
| 5618 | + | |
| 5619 | + | |
| 5620 | + | |
| 5621 | + | |
| 5622 | + | |
| 5623 | + | |
| 5624 | + | |
| 5625 | + | |
| 5626 | + | |
| 5627 | + | |
| 5628 | + | |
| 5629 | + | |
| 5630 | + | |
| 5631 | + | |
| 5632 | + | |
| 5633 | + | |
| 5634 | + | |
| 5635 | + | |
5612 | 5636 | | |
5613 | 5637 | | |
5614 | 5638 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3864 | 3864 | | |
3865 | 3865 | | |
3866 | 3866 | | |
| 3867 | + | |
| 3868 | + | |
| 3869 | + | |
| 3870 | + | |
| 3871 | + | |
| 3872 | + | |
| 3873 | + | |
| 3874 | + | |
| 3875 | + | |
| 3876 | + | |
| 3877 | + | |
| 3878 | + | |
| 3879 | + | |
| 3880 | + | |
| 3881 | + | |
| 3882 | + | |
| 3883 | + | |
| 3884 | + | |
| 3885 | + | |
| 3886 | + | |
| 3887 | + | |
| 3888 | + | |
| 3889 | + | |
| 3890 | + | |
| 3891 | + | |
| 3892 | + | |
| 3893 | + | |
| 3894 | + | |
| 3895 | + | |
| 3896 | + | |
| 3897 | + | |
| 3898 | + | |
| 3899 | + | |
| 3900 | + | |
| 3901 | + | |
| 3902 | + | |
| 3903 | + | |
| 3904 | + | |
| 3905 | + | |
| 3906 | + | |
| 3907 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2807 | 2807 | | |
2808 | 2808 | | |
2809 | 2809 | | |
| 2810 | + | |
| 2811 | + | |
| 2812 | + | |
| 2813 | + | |
| 2814 | + | |
| 2815 | + | |
| 2816 | + | |
| 2817 | + | |
| 2818 | + | |
| 2819 | + | |
| 2820 | + | |
| 2821 | + | |
| 2822 | + | |
| 2823 | + | |
| 2824 | + | |
| 2825 | + | |
| 2826 | + | |
| 2827 | + | |
| 2828 | + | |
| 2829 | + | |
| 2830 | + | |
| 2831 | + | |
| 2832 | + | |
| 2833 | + | |
| 2834 | + | |
| 2835 | + | |
| 2836 | + | |
| 2837 | + | |
| 2838 | + | |
| 2839 | + | |
| 2840 | + | |
| 2841 | + | |
| 2842 | + | |
| 2843 | + | |
| 2844 | + | |
| 2845 | + | |
| 2846 | + | |
| 2847 | + | |
| 2848 | + | |
0 commit comments