Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple

From: Joao Rito <joaovictor(dot)rito(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Chee Wooson <chee(dot)wooson(at)gmail(dot)com>
Subject: Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple
Date: 2026-11-16 06:07:15
Message-ID: 20261116060715.799-1-joaovictor.rito@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Chee,

Thanks again for the patch. This is a second round of testing, to
cover cases my first message listed as not tested. I tried not to
repeat what I already wrote there.

Setup: master at 5ffc82103773 (2026-11-16), autoconf, gcc 14.2,
--enable-cassert --enable-debug --enable-tap-tests --without-icu,
Debian container. I used one source tree: first master as is, then
the patch, so "unpatched" means the same build without the heapam.c
change.

1. Rebase. The v2 patch no longer applies to this master. git apply
fails on the heapam.c hunk (the context around line 3628 no longer
matches). The other three files apply.
I added the new "else if" branch by hand, just before the
TransactionIdIsCurrentTransactionId(xwait) branch, with the same
text as v2. The tree built with 0 warnings. A rebased v3 would
help the next reviewer.

2. Regression and other tests, with the hand-rebased patch:
- make check: 243 of 243 passed (also 243 of 243 on unpatched
master, before the patch).
- make -C src/test/isolation check: 139 of 139 passed. I ran this
only on the patched tree.
- make check in contrib/pageinspect (8), amcheck (4, with its TAP
tests, 298 subtests), pgrowlocks (1) and pg_visibility (1): all
passed.

3. Tuple state with pageinspect, same script on both builds. Each case
is one transaction on one row (fillfactor 50, so the updates are
HOT). "Unpatched" shows the new version with xmax = own XID and
HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY. "Patched" shows
xmax = 0 and HEAP_XMAX_INVALID.
- SELECT ... FOR SHARE, then UPDATE: as above. This was not in my
first message.
- FOR NO KEY UPDATE, then UPDATE: same change.
- FOR KEY SHARE, then a non-key UPDATE: same change.
- FOR KEY SHARE, then an UPDATE of the primary key (not HOT): same
change.
- Lock and UPDATE inside a SAVEPOINT, then ROLLBACK TO: same change
on the new version. The row reads back with its old value.
- INSERT ... ON CONFLICT DO UPDATE twice in one transaction: the
last version changes the same way.
- Whole transaction ROLLBACK after lock and update, then a new
transaction with lock and update, then COMMIT: the new versions
have xmax = 0. The final value (6) is the same on both builds.
- Your PL/pgSQL loop with an EXCEPTION block, 5 iterations in one
transaction: unpatched, 5 of the 6 versions have a MultiXact
xmax (HEAP_XMAX_IS_MULTI). Patched, 0 of 6 do, and the last one
has xmax = 0.
Same result on both builds, as expected from the patch text:
- lock in the parent, UPDATE in a savepoint, RELEASE (new here;
my first message had the opposite order).

4. Concurrency, with sleeps to order the sessions (timing based, one
run each, same result on both builds):
- Another session holds FOR KEY SHARE on the row. Session A takes
FOR NO KEY UPDATE and does a non-key UPDATE. On both builds, the
old and the new tuple keep a MultiXact xmax with both lockers
(pgrowlocks shows "For Key Share" and "For No Key Update"). While
the other session is still open, a third session gets "canceling
statement due to lock timeout" (lock_timeout = 1s) on DELETE and
on a key UPDATE.
- Two sessions hold FOR SHARE (MultiXact), the other one commits,
then A updates. The old and new versions look the same on both
builds.
- Top-level FOR UPDATE, then UPDATE in a savepoint, then ROLLBACK
TO. A second session still times out on an UPDATE while A holds
the transaction open. So the top-level lock is not lost.
- A takes FOR UPDATE and updates. A second session runs an UPDATE
(v = v + 10) that waits for A. After A commits, the final value
is 12 on both builds. A second waiter with SELECT ... FOR SHARE
also returned the new value (2) after A committed.

5. Other checks, on both builds:
- FK: one transaction inserts a child row (key share on the parent)
and then updates the parent. After commit, DELETE and an UPDATE
of the parent key fail with the foreign key violation, on both
builds. This was the case I did not test last time.
- VACUUM (FREEZE) on all test tables, then verify_heapam,
pg_check_visible and pg_check_frozen: 0 rows each, on both.
- Commit, then pg_ctl stop -m immediate and restart: the row and
the tuple flags are the same as before the restart, and
verify_heapam reports 0 rows.
- The server log had no WARNING, PANIC or TRAP lines. The only
ERROR lines were my expected lock timeouts and foreign key
errors.

One side effect, which seems intended: pgrowlocks no longer lists the
new tuple version as "For Key Share" while the transaction is open.
Unpatched, it listed (0,2) as "For Key Share" after the UPDATE. Patched,
it returns 0 rows. Other lockers are not affected (see item 4).

What I did not test:
- check-world, the other TAP suites, meson, other platforms (one
Linux container only).
- Hot standby, streaming replication, logical decoding, pg_upgrade
with the new tuple state.
- MultiXact wraparound, or freezing with old MultiXacts.
- The README.tuplock text. I did not review it line by line.
- Timing. I only did one rough run in my first message, on a
cassert build. This is not a real benchmark.
- Isolation tests on the unpatched tree.
- The v1 pageinspect test, which v2 removed.

I can send the SQL scripts and the log if they are useful.

Regards,
Joao Rito

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Sivirilova Marija 2026-11-16 06:11:32 Re: [***DKIM violation***] Re: [PATCH] Fix heap-buffer-overflow in PGTYPEStimestamp_defmt_scan()
Previous Message Joao Rito 2026-11-16 06:07:08 Re: GiST multirange index scans can fail to return rows