pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Don't free a cached RI plan that may be in use
7+ messages / 1 participants
[nested] [flat]

* pgsql: Don't free a cached RI plan that may be in use
@ 2026-11-04 06:45 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Amit Langote @ 2026-11-04 06:45 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't free a cached RI plan that may be in use

Currently, ri_FetchPreparedPlan() immediately frees a cached SPI plan
if SPI_plan_is_valid() returns false, without tracking whether the
plan might be in use.  That can cause _SPI_execute_plan() to read
freed memory.  This happens, for example, when an RI check's or
action's query runs some user-defined code such as a cast function or
BEFORE ROW trigger function which may perform its own DML using its
own query that is not restricted from firing its own triggers, some
of which may be RI triggers for the same constraint as the outer RI
check and hence use the same cached plan.  If an invalidation arrives
that marks the cached plans referenced by the outer check's SPI plan
invalid between the outer check having called _SPI_execute_plan() and
the inner check's ri_FetchPreparedPlan(), the latter will free the
SPI plan as mentioned above.

Lucas Jeffrey reported an instance of this bug involving a
self-referencing ON DELETE CASCADE foreign key.  The RI action's
DELETE fires a BEFORE DELETE trigger on the rows it deletes, which
invalidates the action's plan with GRANT and REVOKE on the table and
then runs a DELETE of its own, and the RI action fired by that DELETE
is the inner check described above.  Separately, Tom Lane reported
that buildfarm member trilobite, which runs with CLOBBER_CACHE_ALWAYS,
hit this in the regression tests on master after a change there made
more FK checks go through SPI.

Fix by keeping a stack of the plans that every ri_PerformCheck() call
in progress is executing, pushed before SPI_execute_snapshot() and
popped whether it returns or throws an error, the latter using
PG_FINALLY().  ri_FetchPreparedPlan() now frees an invalid plan on the
spot when it is not on that stack, as before, since nothing can be
executing it.  Otherwise it only detaches the plan from its hash entry
and queues it on a list.  Whenever a check finishes, the queued plans
that are no longer on the stack are freed.  So a plan is held only
while a check is actually executing it, however many invalidations
happen meanwhile.  A new AtEOXact_RI() frees whatever is left.

Add the test from Lucas's original patch, which reproduces the crash
on an ordinary cassert build, and a test that raises errors from
inside a check's query, some of them inside subtransactions that are
then rolled back, to check that the stack stays exact.

Author: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Co-authored-by: Trakshan Mishra <trakshanmishra477@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Reported-by: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/3043944.1792163844@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGHzy7S0XfXdK3amd5c4QhpS1o9dySVzPr+-d9ohaE4iV_vNog@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_14_STABLE

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

Modified Files
--------------
src/backend/access/transam/xact.c         |   3 +
src/backend/utils/adt/ri_triggers.c       | 207 ++++++++++++++++++++++++++++--
src/include/commands/trigger.h            |   1 +
src/test/regress/expected/foreign_key.out | 156 ++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql      | 125 ++++++++++++++++++
5 files changed, 484 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Don't free a cached RI plan that may be in use
@ 2026-11-04 06:45 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Amit Langote @ 2026-11-04 06:45 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't free a cached RI plan that may be in use

Currently, ri_FetchPreparedPlan() immediately frees a cached SPI plan
if SPI_plan_is_valid() returns false, without tracking whether the
plan might be in use.  That can cause _SPI_execute_plan() to read
freed memory.  This happens, for example, when an RI check's or
action's query runs some user-defined code such as a cast function or
BEFORE ROW trigger function which may perform its own DML using its
own query that is not restricted from firing its own triggers, some
of which may be RI triggers for the same constraint as the outer RI
check and hence use the same cached plan.  If an invalidation arrives
that marks the cached plans referenced by the outer check's SPI plan
invalid between the outer check having called _SPI_execute_plan() and
the inner check's ri_FetchPreparedPlan(), the latter will free the
SPI plan as mentioned above.

Lucas Jeffrey reported an instance of this bug involving a
self-referencing ON DELETE CASCADE foreign key.  The RI action's
DELETE fires a BEFORE DELETE trigger on the rows it deletes, which
invalidates the action's plan with GRANT and REVOKE on the table and
then runs a DELETE of its own, and the RI action fired by that DELETE
is the inner check described above.  Separately, Tom Lane reported
that buildfarm member trilobite, which runs with CLOBBER_CACHE_ALWAYS,
hit this in the regression tests on master after a change there made
more FK checks go through SPI.

Fix by keeping a stack of the plans that every ri_PerformCheck() call
in progress is executing, pushed before SPI_execute_snapshot() and
popped whether it returns or throws an error, the latter using
PG_FINALLY().  ri_FetchPreparedPlan() now frees an invalid plan on the
spot when it is not on that stack, as before, since nothing can be
executing it.  Otherwise it only detaches the plan from its hash entry
and queues it on a list.  Whenever a check finishes, the queued plans
that are no longer on the stack are freed.  So a plan is held only
while a check is actually executing it, however many invalidations
happen meanwhile.  A new AtEOXact_RI() frees whatever is left.

Add the test from Lucas's original patch, which reproduces the crash
on an ordinary cassert build, and a test that raises errors from
inside a check's query, some of them inside subtransactions that are
then rolled back, to check that the stack stays exact.

Author: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Co-authored-by: Trakshan Mishra <trakshanmishra477@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Reported-by: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/3043944.1792163844@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGHzy7S0XfXdK3amd5c4QhpS1o9dySVzPr+-d9ohaE4iV_vNog@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/3cb89e214b428bca09eb8de9afd1401d392e2977

Modified Files
--------------
src/backend/access/transam/xact.c         |   3 +
src/backend/utils/adt/ri_triggers.c       | 207 ++++++++++++++++++++++++++++--
src/include/commands/trigger.h            |   1 +
src/test/regress/expected/foreign_key.out | 156 ++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql      | 125 ++++++++++++++++++
5 files changed, 484 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Don't free a cached RI plan that may be in use
@ 2026-11-04 06:46 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Amit Langote @ 2026-11-04 06:46 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't free a cached RI plan that may be in use

Currently, ri_FetchPreparedPlan() immediately frees a cached SPI plan
if SPI_plan_is_valid() returns false, without tracking whether the
plan might be in use.  That can cause _SPI_execute_plan() to read
freed memory.  This happens, for example, when an RI check's or
action's query runs some user-defined code such as a cast function or
BEFORE ROW trigger function which may perform its own DML using its
own query that is not restricted from firing its own triggers, some
of which may be RI triggers for the same constraint as the outer RI
check and hence use the same cached plan.  If an invalidation arrives
that marks the cached plans referenced by the outer check's SPI plan
invalid between the outer check having called _SPI_execute_plan() and
the inner check's ri_FetchPreparedPlan(), the latter will free the
SPI plan as mentioned above.

Lucas Jeffrey reported an instance of this bug involving a
self-referencing ON DELETE CASCADE foreign key.  The RI action's
DELETE fires a BEFORE DELETE trigger on the rows it deletes, which
invalidates the action's plan with GRANT and REVOKE on the table and
then runs a DELETE of its own, and the RI action fired by that DELETE
is the inner check described above.  Separately, Tom Lane reported
that buildfarm member trilobite, which runs with CLOBBER_CACHE_ALWAYS,
hit this in the regression tests on master after a change there made
more FK checks go through SPI.

Fix by keeping a stack of the plans that every ri_PerformCheck() call
in progress is executing, pushed before SPI_execute_snapshot() and
popped whether it returns or throws an error, the latter using
PG_FINALLY().  ri_FetchPreparedPlan() now frees an invalid plan on the
spot when it is not on that stack, as before, since nothing can be
executing it.  Otherwise it only detaches the plan from its hash entry
and queues it on a list.  Whenever a check finishes, the queued plans
that are no longer on the stack are freed.  So a plan is held only
while a check is actually executing it, however many invalidations
happen meanwhile.  A new AtEOXact_RI() frees whatever is left.

Add the test from Lucas's original patch, which reproduces the crash
on an ordinary cassert build, and a test that raises errors from
inside a check's query, some of them inside subtransactions that are
then rolled back, to check that the stack stays exact.

Author: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Co-authored-by: Trakshan Mishra <trakshanmishra477@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Reported-by: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/3043944.1792163844@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGHzy7S0XfXdK3amd5c4QhpS1o9dySVzPr+-d9ohaE4iV_vNog@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_16_STABLE

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

Modified Files
--------------
src/backend/access/transam/xact.c         |   3 +
src/backend/utils/adt/ri_triggers.c       | 207 ++++++++++++++++++++++++++++--
src/include/commands/trigger.h            |   1 +
src/test/regress/expected/foreign_key.out | 156 ++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql      | 125 ++++++++++++++++++
5 files changed, 484 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Don't free a cached RI plan that may be in use
@ 2026-11-04 06:46 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Amit Langote @ 2026-11-04 06:46 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't free a cached RI plan that may be in use

Currently, ri_FetchPreparedPlan() immediately frees a cached SPI plan
if SPI_plan_is_valid() returns false, without tracking whether the
plan might be in use.  That can cause _SPI_execute_plan() to read
freed memory.  This happens, for example, when an RI check's or
action's query runs some user-defined code such as a cast function or
BEFORE ROW trigger function which may perform its own DML using its
own query that is not restricted from firing its own triggers, some
of which may be RI triggers for the same constraint as the outer RI
check and hence use the same cached plan.  If an invalidation arrives
that marks the cached plans referenced by the outer check's SPI plan
invalid between the outer check having called _SPI_execute_plan() and
the inner check's ri_FetchPreparedPlan(), the latter will free the
SPI plan as mentioned above.

Lucas Jeffrey reported an instance of this bug involving a
self-referencing ON DELETE CASCADE foreign key.  The RI action's
DELETE fires a BEFORE DELETE trigger on the rows it deletes, which
invalidates the action's plan with GRANT and REVOKE on the table and
then runs a DELETE of its own, and the RI action fired by that DELETE
is the inner check described above.  Separately, Tom Lane reported
that buildfarm member trilobite, which runs with CLOBBER_CACHE_ALWAYS,
hit this in the regression tests on master after a change there made
more FK checks go through SPI.

Fix by keeping a stack of the plans that every ri_PerformCheck() call
in progress is executing, pushed before SPI_execute_snapshot() and
popped whether it returns or throws an error, the latter using
PG_FINALLY().  ri_FetchPreparedPlan() now frees an invalid plan on the
spot when it is not on that stack, as before, since nothing can be
executing it.  Otherwise it only detaches the plan from its hash entry
and queues it on a list.  Whenever a check finishes, the queued plans
that are no longer on the stack are freed.  So a plan is held only
while a check is actually executing it, however many invalidations
happen meanwhile.  A new AtEOXact_RI() frees whatever is left.

Add the test from Lucas's original patch, which reproduces the crash
on an ordinary cassert build, and a test that raises errors from
inside a check's query, some of them inside subtransactions that are
then rolled back, to check that the stack stays exact.

Author: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Co-authored-by: Trakshan Mishra <trakshanmishra477@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Reported-by: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/3043944.1792163844@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGHzy7S0XfXdK3amd5c4QhpS1o9dySVzPr+-d9ohaE4iV_vNog@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_17_STABLE

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

Modified Files
--------------
src/backend/access/transam/xact.c         |   3 +
src/backend/utils/adt/ri_triggers.c       | 207 ++++++++++++++++++++++++++++--
src/include/commands/trigger.h            |   1 +
src/test/regress/expected/foreign_key.out | 156 ++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql      | 125 ++++++++++++++++++
5 files changed, 484 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Don't free a cached RI plan that may be in use
@ 2026-11-04 06:46 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Amit Langote @ 2026-11-04 06:46 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't free a cached RI plan that may be in use

Currently, ri_FetchPreparedPlan() immediately frees a cached SPI plan
if SPI_plan_is_valid() returns false, without tracking whether the
plan might be in use.  That can cause _SPI_execute_plan() to read
freed memory.  This happens, for example, when an RI check's or
action's query runs some user-defined code such as a cast function or
BEFORE ROW trigger function which may perform its own DML using its
own query that is not restricted from firing its own triggers, some
of which may be RI triggers for the same constraint as the outer RI
check and hence use the same cached plan.  If an invalidation arrives
that marks the cached plans referenced by the outer check's SPI plan
invalid between the outer check having called _SPI_execute_plan() and
the inner check's ri_FetchPreparedPlan(), the latter will free the
SPI plan as mentioned above.

Lucas Jeffrey reported an instance of this bug involving a
self-referencing ON DELETE CASCADE foreign key.  The RI action's
DELETE fires a BEFORE DELETE trigger on the rows it deletes, which
invalidates the action's plan with GRANT and REVOKE on the table and
then runs a DELETE of its own, and the RI action fired by that DELETE
is the inner check described above.  Separately, Tom Lane reported
that buildfarm member trilobite, which runs with CLOBBER_CACHE_ALWAYS,
hit this in the regression tests on master after a change there made
more FK checks go through SPI.

Fix by keeping a stack of the plans that every ri_PerformCheck() call
in progress is executing, pushed before SPI_execute_snapshot() and
popped whether it returns or throws an error, the latter using
PG_FINALLY().  ri_FetchPreparedPlan() now frees an invalid plan on the
spot when it is not on that stack, as before, since nothing can be
executing it.  Otherwise it only detaches the plan from its hash entry
and queues it on a list.  Whenever a check finishes, the queued plans
that are no longer on the stack are freed.  So a plan is held only
while a check is actually executing it, however many invalidations
happen meanwhile.  A new AtEOXact_RI() frees whatever is left.

Add the test from Lucas's original patch, which reproduces the crash
on an ordinary cassert build, and a test that raises errors from
inside a check's query, some of them inside subtransactions that are
then rolled back, to check that the stack stays exact.

Author: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Co-authored-by: Trakshan Mishra <trakshanmishra477@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Reported-by: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/3043944.1792163844@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGHzy7S0XfXdK3amd5c4QhpS1o9dySVzPr+-d9ohaE4iV_vNog@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/9e9d24c33ada6fb2fbbc364969ff7aa57506ef32

Modified Files
--------------
src/backend/access/transam/xact.c         |   3 +
src/backend/utils/adt/ri_triggers.c       | 207 ++++++++++++++++++++++++++++--
src/include/commands/trigger.h            |   1 +
src/test/regress/expected/foreign_key.out | 156 ++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql      | 125 ++++++++++++++++++
5 files changed, 484 insertions(+), 8 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Don't free a cached RI plan that may be in use
@ 2026-11-04 06:46 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Amit Langote @ 2026-11-04 06:46 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't free a cached RI plan that may be in use

Currently, ri_FetchPreparedPlan() immediately frees a cached SPI plan
if SPI_plan_is_valid() returns false, without tracking whether the
plan might be in use.  That can cause _SPI_execute_plan() to read
freed memory.  This happens, for example, when an RI check's or
action's query runs some user-defined code such as a cast function or
BEFORE ROW trigger function which may perform its own DML using its
own query that is not restricted from firing its own triggers, some
of which may be RI triggers for the same constraint as the outer RI
check and hence use the same cached plan.  If an invalidation arrives
that marks the cached plans referenced by the outer check's SPI plan
invalid between the outer check having called _SPI_execute_plan() and
the inner check's ri_FetchPreparedPlan(), the latter will free the
SPI plan as mentioned above.

Lucas Jeffrey reported an instance of this bug involving a
self-referencing ON DELETE CASCADE foreign key.  The RI action's
DELETE fires a BEFORE DELETE trigger on the rows it deletes, which
invalidates the action's plan with GRANT and REVOKE on the table and
then runs a DELETE of its own, and the RI action fired by that DELETE
is the inner check described above.  Separately, as Tom Lane
reported, buildfarm member trilobite hit the cast function case in
the regression tests, because CLOBBER_CACHE_ALWAYS makes the
invalidation certain.  This trilobite failure came after 6aecabcd5ed
made the RI checks that require cast functions go through the SPI
path instead of the fast path, which didn't have this bug as it
didn't use SPI plans.

Fix by keeping a stack of the plans that every ri_PerformCheck() call
in progress is executing, pushed before SPI_execute_snapshot() and
popped whether it returns or throws an error, the latter using
PG_FINALLY().  ri_FetchPreparedPlan() now frees an invalid plan on the
spot when it is not on that stack, as before, since nothing can be
executing it.  Otherwise it only detaches the plan from its hash entry
and queues it on a list.  Whenever a check finishes, the queued plans
that are no longer on the stack are freed.  So a plan is held only
while a check is actually executing it, however many invalidations
happen meanwhile.  AtEOXact_RI() frees whatever is left, the same
treatment the fast-path metadata already gets since e73e0196.

Re-add the test case that f52d4051 removed because it turned trilobite
red, now that the bug is fixed, along with a comment noting that it
takes CLOBBER_CACHE_ALWAYS to hit the bug with it.  Also, add the
test from Lucas's original patch, which reproduces the crash on an
ordinary cassert build, and a test that raises errors from inside a
check's query, some of them inside subtransactions that are then
rolled back, to check that the stack stays exact.

Author: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Co-authored-by: Trakshan Mishra <trakshanmishra477@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Reported-by: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/3043944.1792163844@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGHzy7S0XfXdK3amd5c4QhpS1o9dySVzPr+-d9ohaE4iV_vNog@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_19_STABLE

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

Modified Files
--------------
src/backend/utils/adt/ri_triggers.c       | 213 +++++++++++++++++++++++++++---
src/test/regress/expected/foreign_key.out | 186 ++++++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql      | 150 +++++++++++++++++++++
3 files changed, 530 insertions(+), 19 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* pgsql: Don't free a cached RI plan that may be in use
@ 2026-11-04 06:46 Amit Langote <amitlan@postgresql.org>
  0 siblings, 0 replies; 7+ messages in thread

From: Amit Langote @ 2026-11-04 06:46 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Don't free a cached RI plan that may be in use

Currently, ri_FetchPreparedPlan() immediately frees a cached SPI plan
if SPI_plan_is_valid() returns false, without tracking whether the
plan might be in use.  That can cause _SPI_execute_plan() to read
freed memory.  This happens, for example, when an RI check's or
action's query runs some user-defined code such as a cast function or
BEFORE ROW trigger function which may perform its own DML using its
own query that is not restricted from firing its own triggers, some
of which may be RI triggers for the same constraint as the outer RI
check and hence use the same cached plan.  If an invalidation arrives
that marks the cached plans referenced by the outer check's SPI plan
invalid between the outer check having called _SPI_execute_plan() and
the inner check's ri_FetchPreparedPlan(), the latter will free the
SPI plan as mentioned above.

Lucas Jeffrey reported an instance of this bug involving a
self-referencing ON DELETE CASCADE foreign key.  The RI action's
DELETE fires a BEFORE DELETE trigger on the rows it deletes, which
invalidates the action's plan with GRANT and REVOKE on the table and
then runs a DELETE of its own, and the RI action fired by that DELETE
is the inner check described above.  Separately, as Tom Lane
reported, buildfarm member trilobite hit the cast function case in
the regression tests, because CLOBBER_CACHE_ALWAYS makes the
invalidation certain.  This trilobite failure came after 6aecabcd5ed
made the RI checks that require cast functions go through the SPI
path instead of the fast path, which didn't have this bug as it
didn't use SPI plans.

Fix by keeping a stack of the plans that every ri_PerformCheck() call
in progress is executing, pushed before SPI_execute_snapshot() and
popped whether it returns or throws an error, the latter using
PG_FINALLY().  ri_FetchPreparedPlan() now frees an invalid plan on the
spot when it is not on that stack, as before, since nothing can be
executing it.  Otherwise it only detaches the plan from its hash entry
and queues it on a list.  Whenever a check finishes, the queued plans
that are no longer on the stack are freed.  So a plan is held only
while a check is actually executing it, however many invalidations
happen meanwhile.  AtEOXact_RI() frees whatever is left, the same
treatment the fast-path metadata already gets since e73e0196.

Re-add the test case that f52d4051 removed because it turned trilobite
red, now that the bug is fixed, along with a comment noting that it
takes CLOBBER_CACHE_ALWAYS to hit the bug with it.  Also, add the
test from Lucas's original patch, which reproduces the crash on an
ordinary cassert build, and a test that raises errors from inside a
check's query, some of them inside subtransactions that are then
rolled back, to check that the stack stays exact.

Author: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Co-authored-by: Trakshan Mishra <trakshanmishra477@gmail.com>
Co-authored-by: Amit Langote <amitlangote09@gmail.com>
Reported-by: Lucas Jeffrey <lucas.jeffrey@anachronics.com>
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/3043944.1792163844@sss.pgh.pa.us
Discussion: https://postgr.es/m/CAGHzy7S0XfXdK3amd5c4QhpS1o9dySVzPr+-d9ohaE4iV_vNog@mail.gmail.com
Backpatch-through: 14

Branch
------
master

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

Modified Files
--------------
src/backend/utils/adt/ri_triggers.c       | 213 +++++++++++++++++++++++++++---
src/test/regress/expected/foreign_key.out | 186 ++++++++++++++++++++++++++
src/test/regress/sql/foreign_key.sql      | 150 +++++++++++++++++++++
3 files changed, 530 insertions(+), 19 deletions(-)



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2026-11-04 06:46 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-04 06:45 pgsql: Don't free a cached RI plan that may be in use Amit Langote <amitlan@postgresql.org>
2026-11-04 06:45 pgsql: Don't free a cached RI plan that may be in use Amit Langote <amitlan@postgresql.org>
2026-11-04 06:46 pgsql: Don't free a cached RI plan that may be in use Amit Langote <amitlan@postgresql.org>
2026-11-04 06:46 pgsql: Don't free a cached RI plan that may be in use Amit Langote <amitlan@postgresql.org>
2026-11-04 06:46 pgsql: Don't free a cached RI plan that may be in use Amit Langote <amitlan@postgresql.org>
2026-11-04 06:46 pgsql: Don't free a cached RI plan that may be in use Amit Langote <amitlan@postgresql.org>
2026-11-04 06:46 pgsql: Don't free a cached RI plan that may be in use Amit Langote <amitlan@postgresql.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox