From: Heikki Linnakangas <hlinnaka@iki.fi>
To: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Lost FOR KEY SHARE lock
Date: Mon, 2 Nov 2026 16:07:59 +0200
Message-ID: <ce8b11dd-5908-45ac-bb3a-4ccfd5f88cf5@iki.fi> (raw)
Hi,
While looking at another issue [1], I noticed that there's a race
condition in heap_update() where a FOR KEY SHARE lock can be lost.
Leaving out a lot of details, heap_update() looks like this:
> /*
> * If the tuple we're updating is locked, we need to preserve the locking
> * info in the old tuple's Xmax. Prepare a new Xmax value for this.
> */
> compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(oldtup.t_data),
> oldtup.t_data->t_infomask,
> oldtup.t_data->t_infomask2,
> xid, *lockmode, true,
> &xmax_old_tuple, &infomask_old_tuple,
> &infomask2_old_tuple);
> ...
> if (need_toast || newtupsize > pagefree)
> {
> ...
> /*
> * To prevent concurrent sessions from updating the tuple, we have to
> * temporarily mark it locked, while we release the page-level lock.
> *
> * To satisfy the rule that any xid potentially appearing in a buffer
> * written out to disk, we unfortunately have to WAL log this
> * temporary modification. We can reuse xl_heap_lock for this
> * purpose. If we crash/error before following through with the
> * actual update, xmax will be of an aborted transaction, allowing
> * other sessions to proceed.
> */
>
> /*
> * Compute xmax / infomask appropriate for locking the tuple. This has
> * to be done separately from the combo that's going to be used for
> * updating, because the potentially created multixact would otherwise
> * be wrong.
> */
> compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(oldtup.t_data),
> oldtup.t_data->t_infomask,
> oldtup.t_data->t_infomask2,
> xid, *lockmode, false,
> &xmax_lock_old_tuple, &infomask_lock_old_tuple,
> &infomask2_lock_old_tuple);
>
> /* ... update the old tuple with the computed values ... */
>
> LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
>
> /* ... toast or find new page ... */
>
> /* Re-acquire the lock on the old tuple's page. */
> LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
> }
>
> /* Clear obsolete visibility flags, possibly set by ourselves above... */
> oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
> oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
> /* ... and store info about transaction updating this tuple */
> Assert(TransactionIdIsValid(xmax_old_tuple));
> HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple);
> oldtup.t_data->t_infomask |= infomask_old_tuple;
> oldtup.t_data->t_infomask2 |= infomask2_old_tuple;
> HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
While we're not holding the lock, another backend can acquire a KEY
SHARE lock on the tuple. But the 'xmax_old_tuple', 'infomask_old_tuple',
and 'infomask2_old_tuple' values were calculated before releasing the
lock, and won't include the new locker. By updating the old tuple with
those values, any KEY SHARE lock acquired in that window is lost.
Attached is an isolation test case to demonstrate that. I ran out of
energy trying to fix it; it doesn't seem that hard at first glance but
it's a little subtle. If you just recompute the values after locking the
tuple, then the values will also include the transaction's own lock on
the tuple. That's not wrong, but I think it will lead to a lot of extra
multixid. I think this needs to the members of the multixid, remove
itself as a locker, and add back as an updater.
Another observation is that if the transaction already has a suitable
lock on the tuple, there's no need to lock it again here. That would
seem like an easy optimization. heap_lock_tuple() already contains logic
for that, it could perhaps be reused here.
- Heikki
Attachments:lost-for-share-lock-repro.patch (text/x-patch, 5.9K) downloaddiff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.cindex 6a09a1a1517..b0bdb789523 100644--- a/src/backend/access/heap/heapam.c+++ b/src/backend/access/heap/heapam.c@@ -3965,6 +3965,8 @@ l2:
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
+ INJECTION_POINT("heap_update-toast-unlocked", NULL);+
/*
* Let the toaster do its thing, if needed.
*
@@ -4221,6 +4223,12 @@ l2:
oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
/* ... and store info about transaction updating this tuple */
+ /*+ * FIXME: These values were computed before temporarily released the+ * lock. If the tuple was locked in FOR KEY SHARE mode while we were not+ * holding the lock, these do not contain the locker's XID, and the lock+ * is lost.+ */
Assert(TransactionIdIsValid(xmax_old_tuple));
HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple);
oldtup.t_data->t_infomask |= infomask_old_tuple;
diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefileindex 8e2dfaa2384..b06a149b15c 100644--- a/src/test/modules/injection_points/Makefile+++ b/src/test/modules/injection_points/Makefile@@ -37,7 +37,8 @@ ISOLATION = basic \
$(WAIT_CLEANUP_TST) \
heap_lock_update \
on_conflict_probe_window \
- multixact-aborted-updater+ multixact-aborted-updater \+ heap_update_toast_lock
# some isolation tests require wal_level=replica
ISOLATION_OPTS = --temp-config $(top_srcdir)/src/test/modules/injection_points/extra.conf
diff --git a/src/test/modules/injection_points/expected/heap_update_toast_lock.out b/src/test/modules/injection_points/expected/heap_update_toast_lock.outnew file mode 100644index 00000000000..57f0bfab2fc--- /dev/null+++ b/src/test/modules/injection_points/expected/heap_update_toast_lock.out@@ -0,0 +1,26 @@+Parsed test spec with 3 sessions++starting permutation: s1update s2begin s2keyshare wake s1keyupdate s2commit s1check+step s1update: UPDATE t SET val = bigstring() WHERE id = 1; <waiting ...>+step s2begin: BEGIN;+step s2keyshare: SELECT id FROM t WHERE id = 1 FOR KEY SHARE;+id+--+ 1+(1 row)++step wake: + SELECT FROM injection_points_detach('heap_update-toast-unlocked');+ SELECT FROM injection_points_wakeup('heap_update-toast-unlocked');+ <waiting ...>+step s1update: <... completed>+step wake: <... completed>+step s1keyupdate: UPDATE t SET id = id + 1; <waiting ...>+step s2commit: COMMIT;+step s1keyupdate: <... completed>+step s1check: SELECT id, length(val), other FROM t;+id|length|other+--+------+-----+ 2| 6400| 0+(1 row)+diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.buildindex 97e7a736282..db455f1d9b5 100644--- a/src/test/modules/injection_points/meson.build+++ b/src/test/modules/injection_points/meson.build@@ -45,6 +45,7 @@ injection_points_isolation = [
'heap_lock_update',
'on_conflict_probe_window',
'multixact-aborted-updater',
+ 'heap_update_toast_lock',
]
# wait_cleanup terminates another backend, whose FATAL message can be lost
diff --git a/src/test/modules/injection_points/specs/heap_update_toast_lock.spec b/src/test/modules/injection_points/specs/heap_update_toast_lock.specnew file mode 100644index 00000000000..32088a534d4--- /dev/null+++ b/src/test/modules/injection_points/specs/heap_update_toast_lock.spec@@ -0,0 +1,73 @@+# Test that heap_update() protects the old tuple from concurrent updates+# while it has released the buffer lock to do TOASTing.+#+# When the new tuple needs toasting (or doesn't fit on the page), heap_update()+# temporarily marks the old tuple as locked by its own transaction, using the+# tuple lock mode computed from the modified columns (LockTupleNoKeyExclusive+# if no key columns were changed), and releases the buffer lock. While it's+# not holding the buffer lock, another transaction can acquire a KEY SHARE lock+# on the same tuple.+#+# The bug: the FOR SHARE lock acquired in that window will be lost when the+# update finishes, because the update overwrites the tuple's xmax with a new+# xmax and infomasks that were computed before releasing the lock.+setup+{+ CREATE EXTENSION injection_points;++ CREATE TABLE t (id int PRIMARY KEY, val text, other int);+ INSERT INTO t VALUES (1, 'initial', 0);++ -- random, incompressible text well over TOAST_TUPLE_THRESHOLD+ CREATE FUNCTION bigstring() RETURNS text AS $$+ SELECT string_agg(md5(i::text), '') FROM generate_series(1, 200) i+ $$ LANGUAGE SQL+}+teardown+{+ DROP TABLE t;+ DROP EXTENSION injection_points;+}++session s1+setup {+ SELECT FROM injection_points_set_local();+ SELECT FROM injection_points_attach('heap_update-toast-unlocked', 'wait');+}++step s1update { UPDATE t SET val = bigstring() WHERE id = 1; }+step s1keyupdate { UPDATE t SET id = id + 1;}++step s1check { SELECT id, length(val), other FROM t; }++session s2+step s2begin { BEGIN; }+step s2keyshare { SELECT id FROM t WHERE id = 1 FOR KEY SHARE; }+step s2commit { COMMIT; }++session wake+step wake {+ SELECT FROM injection_points_detach('heap_update-toast-unlocked');+ SELECT FROM injection_points_wakeup('heap_update-toast-unlocked');+}++# Concurrent non-key update must wait for s1 to commit.+permutation+ # Start UPDATE on the tuple. This pauses on the injection point, after+ # locking the tuple but before completing the UPDATE.+ s1update++ # Lock the tuple from another session+ s2begin s2keyshare++ # Finish the update and the s2 locker. Session s2 continues to+ # hold its KEY SHARE lock.+ wake(s1update)++ # Try to update the key column. This should block on s2's FOR KEY SHARE lock+ #+ # FIXME: this does not block as it should+ s1keyupdate++ s2commit+ s1check
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: hlinnaka@iki.fi
Subject: Re: Lost FOR KEY SHARE lock
In-Reply-To: <ce8b11dd-5908-45ac-bb3a-4ccfd5f88cf5@iki.fi>
References: <ce8b11dd-5908-45ac-bb3a-4ccfd5f88cf5@iki.fi>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox