[PATCH v1 0/1] Avoid carrying self lock-only xmax to updated tuple

Started by chee.wooson3 months ago10 messageshackers
Jump to latest
#1chee.wooson
chee.wooson@gmail.com

Hi,

heap_update() normally carries pre-existing tuple lockers from the old
tuple version to the new one. That behavior is required when the old
tuple is locked by another transaction, in particular for FOR KEY SHARE
locks taken by referential-integrity checks. Such remote lockers must
continue to protect the updated row version.

However, when the old tuple's xmax is a single-XID, lock-only xmax, and
that XID is the same XID that is now updating the tuple, there is no
independent locker to carry forward. In the single-XID case,
heap_update() carries the old xmax to the new tuple only as
HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY, with no HEAP_KEYS_UPDATED
bit. That is useful for preserving a remote key-share locker, but it is
redundant for a self-owned lock: the new tuple's xmin is the updating
XID, so other transactions cannot treat the new tuple as a visible
committed tuple while the update is in progress, and the self-owned lock
ends when that same transaction ends.

One simple way to hit this repeatedly is a PL/pgSQL loop with an
exception block, since each block is run in a subtransaction:

CREATE TABLE lock_carry_counter (
tenant_id text NOT NULL,
counter_name text NOT NULL,
counter_value bigint,
PRIMARY KEY (tenant_id, counter_name)
);

INSERT INTO lock_carry_counter VALUES ('t1', 'main', 1);

CREATE FUNCTION lock_carry_counter_bump(n integer) RETURNS void
LANGUAGE plpgsql AS $$
DECLARE
current_value bigint;
BEGIN
FOR i IN 1..n LOOP
BEGIN
SELECT counter_value INTO current_value
FROM lock_carry_counter
WHERE tenant_id = 't1' AND counter_name = 'main'
FOR UPDATE;

UPDATE lock_carry_counter
SET counter_value = counter_value + 1
WHERE tenant_id = 't1' AND counter_name = 'main';
EXCEPTION WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
$$;

Before the patch, each new tuple version can carry the previous
subtransaction's self-owned lock-only xmax. Later iterations then have
to process that carried xmax, and can end up doing unnecessary MultiXact
work. After the patch, the chain tail has an invalid xmax in this
same-XID case.

In a local benchmark of the repeated subtransaction pattern above, with
fsync disabled, 10000 iterations took about 126s before the patch and
about 27s after the patch, roughly a 4x improvement. The speedup comes
from avoiding the repeated creation/expansion and rereading of MultiXacts
caused by carrying the previous subtransaction's self lock-only xmax to
the new tuple version. This is not intended as a general UPDATE
performance claim; it describes this specific repeated lock/update
pattern.

The patch changes heap_update() so that, after the MultiXact case has
already been excluded, a single-XID lock-only xmax equal to the current
update XID is treated as having no remaining locker to carry to the new
tuple. Remote lockers, different subtransactions, and MultiXacts keep
the existing behavior.

The included pageinspect regression test and README.tuplock update are
intended to make the behavior and invariant easier to review: the test
exposes that the chain tail no longer carries a self lock-only xmax or
creates a MultiXact, and the README notes that single-XID carry-forward
only preserves a KEYSHR_LOCK | LOCK_ONLY marker, which is needed for
remote key-share lockers but redundant for a same-XID self locker.
Feedback on whether to keep, move, or drop either part would be welcome.

This issue was previously discussed in 2019:

/messages/by-id/20190724232439.lpxzjw2jg3ukgcqn@alap3.anarazel.de

The patch addresses the single-XID self-lock carry-forward case described
there: ON CONFLICT DO UPDATE and manual row locking can leave the updated
tuple with a redundant self-owned lock-only xmax.

The attached v1 patch is based on master at 0b776de09ed.

Feedback on the correctness of the same-XID exception would be
appreciated.

Regards,
chee.wooson

chee.wooson (1):
Avoid carrying self lock-only xmax to updated tuple

contrib/pageinspect/expected/page.out | 62 ++++++++++++++++++++++++++
contrib/pageinspect/sql/page.sql | 57 +++++++++++++++++++++++
src/backend/access/heap/README.tuplock | 38 ++++++++++++++++
src/backend/access/heap/heapam.c | 13 ++++++
4 files changed, 170 insertions(+)

--
2.43.0

#2chee.wooson
chee.wooson@gmail.com
In reply to: chee.wooson (#1)
[PATCH v1 1/1] Avoid carrying self lock-only xmax to updated tuple

heap_update() normally carries pre-existing tuple lockers from the old
tuple version to the new one. That behavior is required when the old
tuple is locked by another transaction, in particular for FOR KEY SHARE
locks taken by referential-integrity checks.

However, when the old tuple's xmax is a single-XID, lock-only xmax, and
that XID is the same XID that is now updating the tuple, there is no
independent locker to carry forward.

In the single-XID case, heap_update() carries the old xmax to the new
tuple only as HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY, with no
HEAP_KEYS_UPDATED bit. That is useful for preserving a remote key-share
locker, but it is redundant for a self-owned lock.

The new tuple's xmin is the updating XID, so other transactions cannot
treat the new tuple as a visible committed tuple while the update is in
progress, and the self-owned lock ends when that same transaction ends.

Treat such a same-XID lock-only xmax as having no remaining locker to
carry to the new tuple. Remote lockers, different subtransactions, and
MultiXacts keep the existing behavior.

Add pageinspect coverage for a repeated subtransaction pattern that locks
and updates the same tuple.

Discussion: /messages/by-id/20190724232439.lpxzjw2jg3ukgcqn@alap3.anarazel.de
---
contrib/pageinspect/expected/page.out | 62 ++++++++++++++++++++++++++
contrib/pageinspect/sql/page.sql | 57 +++++++++++++++++++++++
src/backend/access/heap/README.tuplock | 38 ++++++++++++++++
src/backend/access/heap/heapam.c | 13 ++++++
4 files changed, 170 insertions(+)

diff --git a/contrib/pageinspect/expected/page.out b/contrib/pageinspect/expected/page.out
index fcf19c5ca5a..266810774d5 100644
--- a/contrib/pageinspect/expected/page.out
+++ b/contrib/pageinspect/expected/page.out
@@ -161,6 +161,68 @@ SELECT unnest(combined_flags)
  HEAP_XMIN_FROZEN
 (3 rows)
+-- Repeated subtransactions need not carry a lock held only by the updating
+-- XID forward to each new tuple version.
+CREATE TABLE test_lock_carry_counter (
+	tenant_id text NOT NULL,
+	counter_name text NOT NULL,
+	counter_value bigint,
+	PRIMARY KEY (tenant_id, counter_name)
+) WITH (autovacuum_enabled = off);
+INSERT INTO test_lock_carry_counter VALUES ('t1', 'main', 1);
+CREATE FUNCTION test_lock_carry_counter_bump(n integer) RETURNS void
+LANGUAGE plpgsql AS $$
+DECLARE
+	current_value bigint;
+BEGIN
+	FOR i IN 1..n LOOP
+		BEGIN
+			SELECT counter_value INTO current_value
+			  FROM test_lock_carry_counter
+			  WHERE tenant_id = 't1' AND counter_name = 'main'
+			  FOR UPDATE;
+			UPDATE test_lock_carry_counter
+			  SET counter_value = counter_value + 1
+			  WHERE tenant_id = 't1' AND counter_name = 'main';
+		EXCEPTION WHEN OTHERS THEN
+			NULL;
+		END;
+	END LOOP;
+END;
+$$;
+BEGIN;
+SELECT test_lock_carry_counter_bump(2);
+ test_lock_carry_counter_bump 
+------------------------------
+ 
+(1 row)
+
+WITH items AS (
+	SELECT lp, t_xmax, raw_flags
+	  FROM heap_page_items(get_raw_page('test_lock_carry_counter', 0)),
+	       LATERAL heap_tuple_infomask_flags(t_infomask, t_infomask2)
+	  WHERE lp_flags = 1
+),
+tail AS (
+	SELECT t_xmax, raw_flags
+	  FROM items
+	  ORDER BY lp DESC
+	  LIMIT 1
+)
+SELECT (SELECT count(*) FROM items) AS tuple_versions,
+       (SELECT bool_or(array_position(raw_flags, 'HEAP_XMAX_IS_MULTI') IS NOT NULL)
+        FROM items) AS any_multi,
+       t_xmax = '0'::xid AS tail_xmax_invalid,
+       array_position(raw_flags, 'HEAP_XMAX_INVALID') IS NOT NULL AS tail_has_invalid
+  FROM tail;
+ tuple_versions | any_multi | tail_xmax_invalid | tail_has_invalid 
+----------------+-----------+-------------------+------------------
+              3 | f         | t                 | t
+(1 row)
+
+ROLLBACK;
+DROP FUNCTION test_lock_carry_counter_bump(integer);
+DROP TABLE test_lock_carry_counter;
 -- no flags at all
 SELECT * FROM heap_tuple_infomask_flags(0, 0);
  raw_flags | combined_flags 
diff --git a/contrib/pageinspect/sql/page.sql b/contrib/pageinspect/sql/page.sql
index c75fe1147f6..4e92488f9b2 100644
--- a/contrib/pageinspect/sql/page.sql
+++ b/contrib/pageinspect/sql/page.sql
@@ -57,6 +57,63 @@ SELECT unnest(raw_flags)
 SELECT unnest(combined_flags)
   FROM heap_tuple_infomask_flags(x'FFFF'::int, x'FFFF'::int) ORDER BY 1;
+-- Repeated subtransactions need not carry a lock held only by the updating
+-- XID forward to each new tuple version.
+CREATE TABLE test_lock_carry_counter (
+	tenant_id text NOT NULL,
+	counter_name text NOT NULL,
+	counter_value bigint,
+	PRIMARY KEY (tenant_id, counter_name)
+) WITH (autovacuum_enabled = off);
+
+INSERT INTO test_lock_carry_counter VALUES ('t1', 'main', 1);
+
+CREATE FUNCTION test_lock_carry_counter_bump(n integer) RETURNS void
+LANGUAGE plpgsql AS $$
+DECLARE
+	current_value bigint;
+BEGIN
+	FOR i IN 1..n LOOP
+		BEGIN
+			SELECT counter_value INTO current_value
+			  FROM test_lock_carry_counter
+			  WHERE tenant_id = 't1' AND counter_name = 'main'
+			  FOR UPDATE;
+			UPDATE test_lock_carry_counter
+			  SET counter_value = counter_value + 1
+			  WHERE tenant_id = 't1' AND counter_name = 'main';
+		EXCEPTION WHEN OTHERS THEN
+			NULL;
+		END;
+	END LOOP;
+END;
+$$;
+
+BEGIN;
+SELECT test_lock_carry_counter_bump(2);
+WITH items AS (
+	SELECT lp, t_xmax, raw_flags
+	  FROM heap_page_items(get_raw_page('test_lock_carry_counter', 0)),
+	       LATERAL heap_tuple_infomask_flags(t_infomask, t_infomask2)
+	  WHERE lp_flags = 1
+),
+tail AS (
+	SELECT t_xmax, raw_flags
+	  FROM items
+	  ORDER BY lp DESC
+	  LIMIT 1
+)
+SELECT (SELECT count(*) FROM items) AS tuple_versions,
+       (SELECT bool_or(array_position(raw_flags, 'HEAP_XMAX_IS_MULTI') IS NOT NULL)
+        FROM items) AS any_multi,
+       t_xmax = '0'::xid AS tail_xmax_invalid,
+       array_position(raw_flags, 'HEAP_XMAX_INVALID') IS NOT NULL AS tail_has_invalid
+  FROM tail;
+ROLLBACK;
+
+DROP FUNCTION test_lock_carry_counter_bump(integer);
+DROP TABLE test_lock_carry_counter;
+
 -- no flags at all
 SELECT * FROM heap_tuple_infomask_flags(0, 0);
 -- no combined flags
diff --git a/src/backend/access/heap/README.tuplock b/src/backend/access/heap/README.tuplock
index 16f7d78b7d2..6d14d9649b5 100644
--- a/src/backend/access/heap/README.tuplock
+++ b/src/backend/access/heap/README.tuplock
@@ -73,6 +73,44 @@ in the tuple itself.  We do this by storing the locker's Xid in XMAX, and
 setting infomask bits specifying the locking strength.  See "Infomask Bits"
 below for details on the bit patterns we use.
+Carrying locks across UPDATE
+----------------------------
+
+When UPDATE creates a new tuple version, it normally carries pre-existing
+lockers from the old tuple to the new tuple.  That is necessary when another
+transaction has locked the old tuple, since the lock must continue to protect
+the updated version.  This is essential for referential-integrity checks that
+use FOR KEY SHARE locks.
+
+There is a narrow exception when the old tuple's XMAX is a single XID that is
+LOCK_ONLY, and that XID is the same XID that is now updating the tuple.  In
+that case, there is no independent locker to carry forward.
+
+For a single-XID XMAX carried to the new tuple, heap_update() does not preserve
+the old tuple lock's original strength.  The carried state is encoded as
+HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY, with no HEAP_KEYS_UPDATED bit.
+That representation is enough for the case that needs carrying: a different
+transaction's FOR KEY SHARE lock, such as one taken by a referential-integrity
+check.  It is not a mechanism for preserving a self-held FOR UPDATE or FOR NO
+KEY UPDATE lock on the new tuple version.
+
+The same-XID case is redundant for a simpler reason.  The new tuple's XMIN is
+the updating XID, so other transactions cannot treat the new version as a
+visible committed tuple while the update is in progress.  Once the updating
+transaction ends, any self-owned tuple lock ends with it.  A self-owned
+KEYSHR_LOCK | LOCK_ONLY marker carried to the new tuple therefore has no
+remaining concurrency or referential-integrity role.
+
+The old tuple still records the update in its XMAX.  If the old locker and the
+updater are the same XID, compute_new_xmax_infomask() can fold those states
+while preparing the old tuple's XMAX, but the safety of dropping the carried
+new-tuple XMAX does not depend on comparing the old lock strength with the
+UPDATE lock strength.
+
+This exception does not apply when the old locker is another transaction, a
+different subtransaction of the current transaction, or a MultiXact.  Those
+cases still need to preserve the lock information on the new tuple.
+
 MultiXacts
 ----------
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 72d6541734c..135569eaa63 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -3628,6 +3628,19 @@ l2:
 				TransactionIdDidAbort(update_xact))
 				can_continue = true;
 		}
+		else if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) &&
+				 TransactionIdEquals(xwait, xid))
+		{
+			/*
+			 * At this point Xmax is known not to be a MultiXactId.  The tuple
+			 * is locked only by the same XID that is now updating it, so
+			 * there is no independent locker to carry forward to the new
+			 * tuple.  The update itself is recorded in the old tuple's Xmax.
+			 */
+			checked_lockers = true;
+			locker_remains = false;
+			can_continue = true;
+		}
 		else if (TransactionIdIsCurrentTransactionId(xwait))
 		{
 			/*
-- 
2.43.0
#3chee.wooson
chee.wooson@gmail.com
In reply to: chee.wooson (#2)
Re: [PATCH v1 1/1] Avoid carrying self lock-only xmax to updated tuple

Hi,

Re-attaching the v1 patch as a .patch file for CommitFest/cfbot tracking.

Regards,
chee.wooson

Attachments:

t253655_3
v1-0001-Avoid-carrying-self-lock-only-xmax-to-updated-tup.patchapplication/octet-stream; name=v1-0001-Avoid-carrying-self-lock-only-xmax-to-updated-tup.patchDownload+170-1
#4chee.wooson
chee.wooson@gmail.com
In reply to: chee.wooson (#3)
[PATCH v2] Avoid carrying self lock-only xmax to updated tuple

Hi,

Attached is v2 of the patch.

This version removes the pageinspect test and updates the existing
ON CONFLICT DO UPDATE regression test to reflect the new behavior:
a self-owned lock-only xmax is no longer carried to the new tuple.

The patch only changes the same-XID, single-XID, lock-only case.
Remote lockers and MultiXacts retain the existing behavior.

The relevant regression tests pass.

Comments and review are welcome.

Regards,
chee wooson

Attachments:

t253655_4
v2-0001-Avoid-carrying-self-lock-only-xmax-to-updated-tup.patchapplication/octet-stream; name=v2-0001-Avoid-carrying-self-lock-only-xmax-to-updated-tup.patchDownload+57-3
#5Joao Rito
joaovictor.rito@gmail.com
In reply to: chee.wooson (#4)
Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple

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

#6chee.wooson
chee.wooson@gmail.com
In reply to: Joao Rito (#5)
Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple

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

#7Joao Rito
joaovictor.rito@gmail.com
In reply to: chee.wooson (#6)
Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple

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

#8Joao Rito
joaovictor.rito@gmail.com
In reply to: Joao Rito (#7)
Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple

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

#9chee.wooson
chee.wooson@gmail.com
In reply to: Joao Rito (#8)
Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple

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

Attachments:

t253655_9
v4-0002-Document-carrying-tuple-locks-across-UPDATE.patchapplication/octet-stream; name=v4-0002-Document-carrying-tuple-locks-across-UPDATE.patchDownload+41-1
v4-0001-Avoid-carrying-self-lock-only-xmax-to-updated-tup.patchapplication/octet-stream; name=v4-0001-Avoid-carrying-self-lock-only-xmax-to-updated-tup.patchDownload+19-3
#10Joao Rito
joaovictor.rito@gmail.com
In reply to: chee.wooson (#9)
Re: [PATCH v2] Avoid carrying self lock-only xmax to updated tuple

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:

Show quoted text

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