[PATCH] refint: Avoid reusing cascade UPDATE plans.

From: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Nathan Bossart <nathandbossart(at)gmail(dot)com>
Subject: [PATCH] refint: Avoid reusing cascade UPDATE plans.
Date: 2026-05-14 19:12:18
Message-ID: CAJTYsWXU+fhuzrEd_bnrxyGH3+ny8QRQC2QHf3ws6s9iki3c2Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

In refint's check_foreign_key(), a second cascade UPDATE through the
same trigger silently propagates the *first* UPDATE's new key value to
the referencing row. This is a pre-existing issue we noticed while
looking at BUG #19476 [1].

check_foreign_key() caches prepared plans across trigger invocations,
but the generated cascade UPDATE query embeds the current NEW key
values directly in the SET clause. As a result, the first set of new
key values gets reused for later cascade UPDATEs from the same trigger.

The attached regression test demonstrates this: updating two referenced
keys in sequence should cascade to two different referencing values.
Without the fix, the cached plan for the first UPDATE is reused for the
second one, so the second referencing row is updated to the first
UPDATE's new key value instead.

I had originally tried parameterizing the SET values, but as Nathan
pointed out in the BUG #19476 thread, that is not generally correct
because the referenced-relation key type is not necessarily the right
type for the referencing relation's SET target. This patch instead
uses the simpler approach suggested there: do not cache prepared plans
for cascade UPDATE actions. The existing cached-plan path remains in
place for restrict, cascade DELETE, and setnull actions.

Thoughts?

Regards,
Ayush

[1] PostgreSQL: BUG #19476: Segmentation fault in contrib/spi
<https://www.postgresql.org/message-id/flat/19476-bd04ea6241345303%40postgresql.org>

Attachment Content-Type Size
v1-0001-Avoid-reusing-refint-cascade-UPDATE-plans.patch application/octet-stream 8.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2026-05-14 19:51:19 Re: call for applications: hacker mentoring 2026
Previous Message Masahiko Sawada 2026-05-14 18:03:36 Re: Fix bug of COPY TO support partition table