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

Поиск
Список
Период
Сортировка
Искать

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

От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi,

Thank you for the patch. I tested v2 (CF entry 7253) and here are my results.

== Setup ==

- I used the cfbot branch cf/7253, commit
62f9339057248e2e323e075bb2a6cb9630afd024. It is a merge of the v2
patch commit 597d58f into 58aecdd.
- Configure options: --enable-cassert --enable-debug
--enable-tap-tests --without-icu, on Linux (WSL Ubuntu).
- For comparison, I built a second server from the same tree with only
src/backend/access/heap/heapam.c reverted to the base version, so
"unpatched" below means the same code without the heapam.c change.

== Tests that passed ==

- The build finished with 0 compiler warnings.
- "make check" passed: All 243 tests passed. This includes the changed
expected output in update.out.

== Manual tests ==

I ran these on a temporary cluster, on both the patched and the
unpatched server, and looked at the tuples with pageinspect.

1. Your PL/pgSQL loop (SELECT ... FOR UPDATE, then UPDATE, inside a
BEGIN ... EXCEPTION block), run for 3 iterations in one transaction.
   - Unpatched: the tuple chain had 4 versions, and 3 of them had a
MultiXact xmax (HEAP_XMAX_IS_MULTI).
   - Patched: 4 versions, none with a MultiXact xmax, and the last
version has xmax = 0 with HEAP_XMAX_INVALID.
2. A single transaction doing SELECT ... FOR UPDATE and then UPDATE on
the same row.
   - Unpatched: the new version carried xmax = the same XID, with
HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY.
   - Patched: the new version has xmax = 0 and HEAP_XMAX_INVALID. The
row value was correct after COMMIT (2).
3. INSERT ... ON CONFLICT DO UPDATE on an existing row, in one transaction.
   - Unpatched: the new version carried a self lock-only xmax.
   - Patched: the new version has xmax = 0.
4. A SAVEPOINT that takes FOR UPDATE, then RELEASE, then UPDATE in the
parent (different XID, so it should keep the old behavior). Patched
and unpatched gave the same flags on the new version
(HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY, xmax 678). The old
version showed a MultiXact xmax in both.
5. Foreign key key-share lock from another session. Session B inserted
a child row (FOR KEY SHARE on the parent row) and stayed open. Session
A did a non-key UPDATE of the parent row. Then a key UPDATE and a
DELETE of the parent row, each with lock_timeout = 2s, both failed
with "canceling statement due to lock timeout", waiting on the open
FK-insert transaction. The result was the same on the patched and
unpatched servers, so the remote key-share lock is still protected in
this case.
6. A rough timing of your loop, 3000 iterations, fsync=off, single run
each: about 1.6 s patched and about 6.2 s unpatched. The final counter
value was 3001 in both cases. This is only a quick check on an
assert-enabled build, but it is in line with the speedup you
described.

The patched server's log had no WARNING, ERROR, PANIC or TRAP lines
apart from the expected lock timeouts.

== Not tested ==

- Only "make check" was run. I did not run check-world, the TAP tests,
isolation tests or contrib tests.
- No concurrency stress test, no crash recovery or replication test,
and no pg_upgrade or logical decoding test with the new tuple state.
- No test of VACUUM, freezing or MultiXact wraparound behavior.
- No code review beyond the heapam.c change and the update.out change
shown in run.log.
- I did not test other lock modes or combinations beyond the cases
above (for example FOR SHARE or FOR NO KEY UPDATE taken first), or the
foreign key case where the same transaction both locks and updates.
- Timing was a single short run per build on a debug build, so it is
not a reliable benchmark.
- Only one platform (Linux under WSL) was used.

Regards,
João Rito


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

От:
Chee Wooson <chee.wooson@gmail.com>
Дата:
Hi João,

Thanks for taking the time to test the patch and for sharing the
detailed results. I really appreciate your help!

Best regards,
Chee Wooson


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

От:
Joao Rito <joaovictor.rito@gmail.com>
Дата:
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


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

От:
Joao Rito <joaovictor.rito@gmail.com>
Дата:
Hi Chee,

Thanks for v3. I tested it on master at 7fc2f0423320 (Debian container,
gcc 14.2, --enable-cassert --enable-debug --enable-tap-tests). Your v3
mail reached me directly and is not in the archive thread yet, so I
reply to my last message here.

- Both patches apply with git apply, and git diff --check reports
  nothing. The build has 0 warnings.
- make check: 243 of 243. Isolation: 139 of 139. contrib pageinspect
  (8), amcheck (4, TAP 298 subtests), pgrowlocks (1), pg_visibility (1):
  all pass.
- I ran my earlier tuple-state and concurrency scripts again. The
  pageinspect dumps and the lock timeouts are the same as with v2 on the
  old master: same-XID cases give xmax = 0 with HEAP_XMAX_INVALID, and
  the different-subtransaction and MultiXact cases stay on the old path.
  verify_heapam reports 0 rows, and the server log has no WARNING or
  TRAP.

Comments on the README.tuplock text in 0002, from reading it against the
code. I found no factual error, only wording:
1. "That representation is enough for the case that needs carrying: a
   different transaction's FOR KEY SHARE lock" does not mention a locker
   in a different subtransaction or a MultiXact, which the last sentence
   says still carry.
2. The text says the safety does not depend on comparing lock strengths.
   That is right. It may help to say the test is
   HEAP_XMAX_IS_LOCKED_ONLY(infomask) && xwait == xid.
3. "LOCK_ONLY" and "KEYSHR_LOCK | LOCK_ONLY" appear without the
   HEAP_XMAX_ prefix in two paragraphs, and the other paragraphs use the
   full name.
4. The paragraph "The same-XID case is redundant for a simpler reason"
   could say that if the subtransaction aborts, the xmin of the new
   version aborts too.

What I did not test: check-world, injection points, other platforms (one
Linux build), standby and replication, pg_upgrade, a long concurrent
load, extensions that read xmax (pg_surgery, pgstattuple,
pg_walinspect), deeper nested subtransactions, and partitioned tables or
FK triggers on many rows. I only read the README text, there is no test
for it.

Regards,
Joao Rito



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

От:
Chee Wooson <chee.wooson@gmail.com>
Дата:
Вложения:
Hi Joao,

My earlier v3 mail went only to you. Attached is v4, with the
README.tuplock changes revised following your comments.

The README now explains the general carry-forward mechanism before
introducing the same-XID exception. It covers different-subtransaction
lockers and MultiXact handling, shows the exact condition, uses consistent
flag names, and explains why omitting the self-owned lock is safe even
when the updating subtransaction aborts.

Patch 0001 (code and regression test) is unchanged from v3.
Only patch 0002 (README.tuplock) has changed.

The core regression, isolation, and relevant contrib tests passed for
the unchanged implementation.

Thanks again for the testing and review.

Regards,
Chee Wooson

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

От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi Chee,

Thank you for v4. I tested it on master at 11591f273e6a (Debian container,
gcc 14.2, --enable-cassert --enable-debug --enable-tap-tests).

- Both patches apply with git apply, and git diff --check reports nothing.
The build has 0 warnings.
- make check: 243 of 243. Isolation: 139 of 139. contrib pageinspect (8),
amcheck (4, TAP 298 subtests), pgrowlocks (1), pg_visibility (1): all pass.
- 0001 is the same as v3 (only the subject line differs), so I did not
repeat my earlier tuple-state and concurrency scripts. They passed on v3.

I read the new README.tuplock text against heapam.c, and all four of my
earlier comments look addressed to me: the different-subtransaction and
MultiXact cases are now in the text, the test
HEAP_XMAX_IS_LOCKED_ONLY(infomask) && TransactionIdEquals(xwait, xid) is
shown, the HEAP_XMAX_ prefix is used everywhere, and the abort of the
subtransaction is explained through the xmin of the new version.
I found no factual error. Please correct me if I misread something.

What I did not test: check-world, injection points, other platforms (one
Linux build), standby and replication, pg_upgrade, a long concurrent load,
extensions that read xmax (pg_surgery, pgstattuple, pg_walinspect), deeper
nested subtransactions, and partitioned tables or FK triggers on many rows.
I only read the README text, there is no test for it.

Thanks again for the work.

Regards,
Joao Rito

On Tue, Nov 17, 2026 12:21 AM, Chee Wooson <chee.wooson@gmail.com> wrote:
Hi Joao,

My earlier v3 mail went only to you. Attached is v4, with the
README.tuplock changes revised following your comments.

The README now explains the general carry-forward mechanism before
introducing the same-XID exception. It covers different-subtransaction
lockers and MultiXact handling, shows the exact condition, uses consistent
flag names, and explains why omitting the self-owned lock is safe even
when the updating subtransaction aborts.

Patch 0001 (code and regression test) is unchanged from v3.
Only patch 0002 (README.tuplock) has changed.

The core regression, isolation, and relevant contrib tests passed for
the unchanged implementation.

Thanks again for the testing and review.

Regards,
Chee Wooson
FAQ