postgresql/src
Dean Rasheed 7048e50f8f Fix RETURNING OLD with BEFORE UPDATE trigger and concurrent update.
When executing an UPDATE with a RETURNING clause on a table with a
BEFORE UPDATE row trigger, the computation of the OLD values in the
RETURNING list was incorrect if the target tuple was concurrently
updated by another session, at isolation level READ COMMITTED.

The problem was that the trigger code would lock the target tuple,
waiting for the other session to commit, and then fetch the updated
target tuple, but ExecUpdate() would not realise that the target tuple
had changed, and use the outdated target tuple for computing OLD
values. Fix by having ExecUpdate() check the TM_FailureData from
trigger execution and re-fetch the target tuple if necessary.

Re-fetching the target tuple like this is a little inefficient, but
probably negligible compared to the trigger execution and update. A
better long-term fix might be to move the EPQ code out of trigger.c,
and let ExecUpdate() handle it, like ExecMergeMatched() does, but that
would likely mean changing the trigger API, which seems a bit much for
back-patching.

Backpatch to v18, where support for RETURNING OLD/NEW was added.

Bug: #19536
Reported-by: Jonas Boberg <bobergj@gmail.com>
Diagnosed-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Author: Dean Rasheed <dean.a.rasheed@gmail.com>
Reviewed-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Discussion: https://postgr.es/m/19536-73ce5847e6c0e7b1@postgresql.org
Backpatch-through: 18
2026-07-08 20:46:25 +01:00
..
backend Fix RETURNING OLD with BEFORE UPDATE trigger and concurrent update. 2026-07-08 20:46:25 +01:00
bin Fix pg_dump ACL minimization for PROPERTY GRAPH. 2026-07-07 09:51:38 -07:00
common pg_unicode_fast: fix final sigma logic. 2026-07-07 14:29:21 -07:00
fe_utils Fix psql's pager selection for wrapped expanded output. 2026-07-05 18:11:40 -04:00
include Fix pg_dump ACL minimization for PROPERTY GRAPH. 2026-07-07 09:51:38 -07:00
interfaces libpq: Drain all pending bytes from SSL/GSS during pqReadData() 2026-07-07 18:46:14 +03:00
makefiles Add CONCURRENTLY option to REPACK 2026-04-06 21:55:08 +02:00
pl Make PLy_elog() use pg_integer_constant_p(). 2026-07-06 13:48:42 -04:00
port Use C11 alignas instead of pg_attribute_aligned 2026-07-01 08:53:52 +02:00
template Switch the semaphore API on Solaris to unnamed POSIX. 2026-03-14 14:10:32 -04:00
test Fix RETURNING OLD with BEFORE UPDATE trigger and concurrent update. 2026-07-08 20:46:25 +01:00
timezone Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
tools Fix qual pushdown past grouping with mismatched equivalence 2026-07-06 16:14:13 +09:00
tutorial Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
.gitignore
DEVELOPERS
Makefile Add CONCURRENTLY option to REPACK 2026-04-06 21:55:08 +02:00
Makefile.global.in Update Unicode data to CLDR 48.2 2026-04-13 11:13:36 +02:00
Makefile.shlib Restore AIX support. 2026-02-23 13:34:22 -05:00
meson.build Update copyright for 2026 2026-01-01 13:24:10 -05:00
nls-global.mk Fix update-po for the PGXS case 2025-10-16 20:21:05 +02:00