Fix LogStandbySnapshot() race during logical decoding activation

Started by Zhijie Hou6 days ago9 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Zhijie Hou
houzhijie22@gmail.com

Hi,

While testing the dynamic logical decoding status control, I noticed
another race condition in LogStandbySnapshot(), when logical decoding is
concurrently enabled, ProcArrayLock can be released before logging the
xl_running_xacts WAL record, causing historic snapshot corruption. The
process is as follows:

1. S1 calls GetRunningTransactionData() while T1 (XID 800, e.g., CREATE
TABLE tab_snap) is still in ProcArray, so S1 computes oldestRunningXid =
800 in memory.
2. S1 releases ProcArrayLock early, before writing its xl_running_xacts
record to WAL.
3. T1 commits: it writes COMMIT 800 at LSN 100 and finishes
ProcArrayEndTransaction(), removing 800 from ProcArray. (Had S1 kept
ProcArrayLock, T1 would have blocked here.)
4. S2 runs LogStandbySnapshot(): since 800 is already gone from
ProcArray, S2 computes oldestRunningXid = 801 (nextXid) and writes its
xl_running_xacts record at LSN 200.
5. S1 writes its delayed xl_running_xacts record at LSN 300 with the
stale oldestRunningXid = 800.

Replaying that out-of-order WAL stream corrupts SnapBuild's state:
- LSN 100 (COMMIT 800): SnapBuild adds 800 to builder->committed.xip and
sets builder->xmax = 801.
- LSN 200 (S2's RUNNING_XACTS, oldestRunningXid = 801): SnapBuild sets
builder->xmin = 801 and purges 800 from builder->committed.xip.
- LSN 300 (S1's RUNNING_XACTS, oldestRunningXid = 800): SnapBuild
overwrites builder->xmin = 800. Now 800 is back inside [xmin, xmax)
after already being purged from committed.xip, so subsequent historic
snapshots built by SnapBuild mark committed transaction 800 as still in
progress (e.g., failing with "ERROR: pg_attribute catalog is missing 1
attribute(s) for relation OID ..." or "ERROR: could not map filenumber
... to relation OID").

Currently, LogStandbySnapshot() checks IsLogicalDecodingEnabled()
(logical_decoding_enabled) to decide whether to keep ProcArrayLock.
However, enabling logical decoding happens in two phases:

1. EnableLogicalDecoding() sets xlog_logical_info = true and waits for
all processes to absorb a ProcSignalBarrier.
2. Only after that barrier wait finishes does it set
logical_decoding_enabled = true, after which the caller reserves the
new slot's restart_lsn.

If S1 absorbs the barrier in phase 1 and then calls LogStandbySnapshot()
before phase 2 sets logical_decoding_enabled = true,
IsLogicalDecodingEnabled() still returns false, so S1 releases
ProcArrayLock early. Meanwhile, since S1 has already absorbed the
barrier, the activating backend does not wait for S1: it can finish
phase 2 and reserve the new slot's restart_lsn before S1 writes its
xl_running_xacts record, exposing that out-of-order record to logical
decoding.

Attached is a patch set to fix this:

0001 checks IsXLogLogicalInfoEnabled() right before
GetRunningTransactionData() (not while holding ProcArrayLock, to respect
the lock ordering with DisableLogicalDecoding()). If xlog_logical_info
is still false at that point, neither GetRunningTransactionData() nor
LogCurrentRunningXacts() processes ProcSignalBarriers, so no concurrent
activation can finish its barrier wait until after the WAL record is
inserted.

0002 adds injection point to reproduce/test the above case. I kept this
in a separate patch because it looks complicate and might not be worth
pushing.

--
Best Regards,
Zhijie Hou

Attachments:

t259646_1
v1-0002-Add-test-for-LogStandbySnapshot-race-when-enablin.patchtext/plain; charset=UTF-8; name=v1-0002-Add-test-for-LogStandbySnapshot-race-when-enablin.patchDownload+204-11
v1-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchtext/plain; charset=UTF-8; name=v1-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchDownload+22-4
#2Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Zhijie Hou (#1)
Re: Fix LogStandbySnapshot() race during logical decoding activation

On Sun, Nov 22, 2026 at 9:46 AM Zhijie Hou <houzhijie22@gmail.com> wrote:

Hi,

While testing the dynamic logical decoding status control, I noticed
another race condition in LogStandbySnapshot(), when logical decoding is
concurrently enabled, ProcArrayLock can be released before logging the
xl_running_xacts WAL record, causing historic snapshot corruption. The
process is as follows:

1. S1 calls GetRunningTransactionData() while T1 (XID 800, e.g., CREATE
TABLE tab_snap) is still in ProcArray, so S1 computes oldestRunningXid =
800 in memory.
2. S1 releases ProcArrayLock early, before writing its xl_running_xacts
record to WAL.
3. T1 commits: it writes COMMIT 800 at LSN 100 and finishes
ProcArrayEndTransaction(), removing 800 from ProcArray. (Had S1 kept
ProcArrayLock, T1 would have blocked here.)
4. S2 runs LogStandbySnapshot(): since 800 is already gone from
ProcArray, S2 computes oldestRunningXid = 801 (nextXid) and writes its
xl_running_xacts record at LSN 200.
5. S1 writes its delayed xl_running_xacts record at LSN 300 with the
stale oldestRunningXid = 800.

Replaying that out-of-order WAL stream corrupts SnapBuild's state:
- LSN 100 (COMMIT 800): SnapBuild adds 800 to builder->committed.xip and
sets builder->xmax = 801.
- LSN 200 (S2's RUNNING_XACTS, oldestRunningXid = 801): SnapBuild sets
builder->xmin = 801 and purges 800 from builder->committed.xip.
- LSN 300 (S1's RUNNING_XACTS, oldestRunningXid = 800): SnapBuild
overwrites builder->xmin = 800. Now 800 is back inside [xmin, xmax)
after already being purged from committed.xip, so subsequent historic
snapshots built by SnapBuild mark committed transaction 800 as still in
progress (e.g., failing with "ERROR: pg_attribute catalog is missing 1
attribute(s) for relation OID ..." or "ERROR: could not map filenumber
... to relation OID").

Currently, LogStandbySnapshot() checks IsLogicalDecodingEnabled()
(logical_decoding_enabled) to decide whether to keep ProcArrayLock.
However, enabling logical decoding happens in two phases:

1. EnableLogicalDecoding() sets xlog_logical_info = true and waits for
all processes to absorb a ProcSignalBarrier.
2. Only after that barrier wait finishes does it set
logical_decoding_enabled = true, after which the caller reserves the
new slot's restart_lsn.

If S1 absorbs the barrier in phase 1 and then calls LogStandbySnapshot()
before phase 2 sets logical_decoding_enabled = true,
IsLogicalDecodingEnabled() still returns false, so S1 releases
ProcArrayLock early. Meanwhile, since S1 has already absorbed the
barrier, the activating backend does not wait for S1: it can finish
phase 2 and reserve the new slot's restart_lsn before S1 writes its
xl_running_xacts record, exposing that out-of-order record to logical
decoding.

Thank you for the report. I agree with your analysis.

Attached is a patch set to fix this:

0001 checks IsXLogLogicalInfoEnabled() right before
GetRunningTransactionData() (not while holding ProcArrayLock, to respect
the lock ordering with DisableLogicalDecoding()). If xlog_logical_info
is still false at that point, neither GetRunningTransactionData() nor
LogCurrentRunningXacts() processes ProcSignalBarriers, so no concurrent
activation can finish its barrier wait until after the WAL record is
inserted.

I think the fix is correct. Checking the shared xlog_logical_info
instead of the local cache makes sense given that the cache update can
be delayed in a transaction with an XID. A few comments:

+    */
+   xlog_logical_info = IsXLogLogicalInfoEnabled();

Is there a reason to move the check here? The comment explains why we
cannot do it while holding ProcArrayLock, but the beginning of the
function, where we called IsLogicalDecodingEnabled() before, is also
outside of the lock.

---
+   /*
+    * Check whether logical WAL logging is enabled before
+    * GetRunningTransactionData() acquires ProcArrayLock.
+    * IsXLogLogicalInfoEnabled() acquires LogicalDecodingControlLock, so
+    * calling it while holding ProcArrayLock could cause a three-way deadlock
+    * with DisableLogicalDecoding() (which takes LogicalDecodingControlLock
+    * before ReplicationSlotControlLock) and e.g. CreateInitDecodingContext()
+    * (which takes ReplicationSlotControlLock before ProcArrayLock).
+    */

I think it would be good to have an explanation for why we cannot use
IsLogicalDecodingEnabled() here.

0002 adds injection point to reproduce/test the above case. I kept this
in a separate patch because it looks complicate and might not be worth
pushing.

I agree that it's a bit too much for this fix. I think it can be
simplified by having an injection point right after
IsXLogLogicalInfoEnabled() call and by having the assertion
"xlog_logical_info || !IsLogicalDecodingEnabled()" after inserting the
record. That is, if xlog_logical_info was false when we checked,
logical decoding must not have been enabled by the time the record is
inserted. It cannot fail with a concurrent activation since the
activating process has to wait for us to absorb the barrier, which we
cannot do while holding XidGenLock. I've attached a draft patch, which
can be applied on top of the 0001 patch.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

Attachments:

regression_test.patchtext/x-patch; charset=US-ASCII; name=regression_test.patchDownload+70-0
#3Zhijie Hou
houzhijie22@gmail.com
In reply to: Masahiko Sawada (#2)
Re: Fix LogStandbySnapshot() race during logical decoding activation

Hi,

On Tue, Nov24, 2026 at 06:25 AM Masahiko Sawada wrote:

On Sun, Nov 22, 2026 at 9:46 AM Zhijie Hou <houzhijie22@gmail.com> wrote:

Attached is a patch set to fix this:

0001 checks IsXLogLogicalInfoEnabled() right before
GetRunningTransactionData() (not while holding ProcArrayLock, to
respect the lock ordering with DisableLogicalDecoding()). If
xlog_logical_info is still false at that point, neither
GetRunningTransactionData() nor LogCurrentRunningXacts() processes
ProcSignalBarriers, so no concurrent activation can finish its
barrier wait until after the WAL record is inserted.

I think the fix is correct. Checking the shared xlog_logical_info
instead of the local cache makes sense given that the cache update can
be delayed in a transaction with an XID. A few comments:

Thanks for the comments.

+    */
+   xlog_logical_info = IsXLogLogicalInfoEnabled();

Is there a reason to move the check here? The comment explains why we
cannot do it while holding ProcArrayLock, but the beginning of the
function, where we called IsLogicalDecodingEnabled() before, is also
outside of the lock.

I rechecked and confirmed that check in the beginning is also safe. So,
I moved this back in the new version patch.

---
+   /*
+    * Check whether logical WAL logging is enabled before
+    * GetRunningTransactionData() acquires ProcArrayLock.
+    * IsXLogLogicalInfoEnabled() acquires LogicalDecodingControlLock, so
+    * calling it while holding ProcArrayLock could cause a three-way deadlock
+    * with DisableLogicalDecoding() (which takes LogicalDecodingControlLock
+    * before ReplicationSlotControlLock) and e.g. CreateInitDecodingContext()
+    * (which takes ReplicationSlotControlLock before ProcArrayLock).
+    */

I think it would be good to have an explanation for why we cannot use
IsLogicalDecodingEnabled() here.

Added.

0002 adds injection point to reproduce/test the above case. I kept
this in a separate patch because it looks complicate and might not
be worth pushing.

I agree that it's a bit too much for this fix. I think it can be
simplified by having an injection point right after
IsXLogLogicalInfoEnabled() call and by having the assertion
"xlog_logical_info || !IsLogicalDecodingEnabled()" after inserting the
record. That is, if xlog_logical_info was false when we checked,
logical decoding must not have been enabled by the time the record is
inserted. It cannot fail with a concurrent activation since the
activating process has to wait for us to absorb the barrier, which we
cannot do while holding XidGenLock. I've attached a draft patch, which
can be applied on top of the 0001 patch.

The attached tests look good to me, I have merged it into the patch.

Here is the V2 patch.

--
Best Regards,
Zhijie Hou

Attachments:

t259646_3
v2-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchtext/plain; charset=UTF-8; name=v2-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchDownload+94-4
#4Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Zhijie Hou (#3)
RE: Fix LogStandbySnapshot() race during logical decoding activation

Hi,

Here is the V2 patch.

Thanks for updating the patch.

Quick (and possibly stupid) question. Is there a reason why
IsXLogLogicalInfoEnabled() is done before GetRunningTransactionLocks() and
LogAccessExclusiveLocks()?
You said to put it right before GetRunningTransactionData(), but these functions
are done in-between. It's harmless but it's clearer for me to put them before.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#5Zhijie Hou
houzhijie22@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#4)
Re: Fix LogStandbySnapshot() race during logical decoding activation

Hi,

On Tue, Nov24, 2026 at 12:25 PM "Hayato Kuroda (Fujitsu)" wrote:

Hi,

Here is the V2 patch.

Quick (and possibly stupid) question. Is there a reason why
IsXLogLogicalInfoEnabled() is done before GetRunningTransactionLocks()
and LogAccessExclusiveLocks()? You said to put it right before
GetRunningTransactionData(), but these functions are done in-between.
It's harmless but it's clearer for me to put them before.

Thanks for looking at the patch.

I don't see a functional difference in the order here and didn't intend
to change the original placement, since IsLogicalDecodingEnabled() was
already called at the beginning of LogStandbySnapshot() on HEAD, and
none of the calls before inserting the xl_running_xacts record process
interrupts (so this backend cannot absorb the barrier and let a
concurrent activation complete in-between). The comment was only meant
to remind future readers not to defer the check until ProcArrayLock is held.

I slightly adjusted the comments in this version to make the points clear.

--
Best Regards,
Zhijie Hou

Attachments:

t259646_5
v3-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchtext/plain; charset=UTF-8; name=v3-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchDownload+97-4
#6Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Zhijie Hou (#5)
RE: Fix LogStandbySnapshot() race during logical decoding activation

Hi,

Quick (and possibly stupid) question. Is there a reason why
IsXLogLogicalInfoEnabled() is done before GetRunningTransactionLocks()
and LogAccessExclusiveLocks()? You said to put it right before
GetRunningTransactionData(), but these functions are done in-between.
It's harmless but it's clearer for me to put them before.

Thanks for looking at the patch.

I don't see a functional difference in the order here and didn't intend
to change the original placement, since IsLogicalDecodingEnabled() was
already called at the beginning of LogStandbySnapshot() on HEAD, and
none of the calls before inserting the xl_running_xacts record process
interrupts (so this backend cannot absorb the barrier and let a
concurrent activation complete in-between). The comment was only meant
to remind future readers not to defer the check until ProcArrayLock is held.

Thanks for the detailed descriptions. I understood. v3 LGTM.

Best regards,
Hayato Kuroda
FUJITSU LIMITED

#7Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#6)
Re: Fix LogStandbySnapshot() race during logical decoding activation

On Tue, Nov 24, 2026 at 9:07 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:

Hi,

Quick (and possibly stupid) question. Is there a reason why
IsXLogLogicalInfoEnabled() is done before GetRunningTransactionLocks()
and LogAccessExclusiveLocks()? You said to put it right before
GetRunningTransactionData(), but these functions are done in-between.
It's harmless but it's clearer for me to put them before.

Thanks for looking at the patch.

I don't see a functional difference in the order here and didn't intend
to change the original placement, since IsLogicalDecodingEnabled() was
already called at the beginning of LogStandbySnapshot() on HEAD, and
none of the calls before inserting the xl_running_xacts record process
interrupts (so this backend cannot absorb the barrier and let a
concurrent activation complete in-between). The comment was only meant
to remind future readers not to defer the check until ProcArrayLock is held.

Thanks for the detailed descriptions. I understood. v3 LGTM.

Thank you for updating the patch!

I've reviewed the v3 patch and felt the new comments were a bit long
and redundant to me.

I've attached the v4 patch that rewrote the comments and a commit
message update. The attached nocfbot patch is the diff between v3 and
v4.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

Attachments:

t259646_7
nocfbot_comment_fix_masahiko.patchtext/x-patch; charset=US-ASCII; name=nocfbot_comment_fix_masahiko.patchDownload+20-24
v4-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchtext/x-patch; charset=US-ASCII; name=v4-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patchDownload+94-4
#8Zhijie Hou
houzhijie22@gmail.com
In reply to: Masahiko Sawada (#7)
Re: Fix LogStandbySnapshot() race during logical decoding activation

Hi,

On Thu, Nov26, 2026 at 04:02 AM Masahiko Sawada wrote:

I've reviewed the v3 patch and felt the new comments were a bit long
and redundant to me.

I've attached the v4 patch that rewrote the comments and a commit
message update. The attached nocfbot patch is the diff between v3 and
v4.

Thanks for updating the patch ! The new version looks good to me.

--
Best Regards,
Zhijie Hou

#9Alvaro Herrera
alvherre@kurilemu.de
In reply to: Masahiko Sawada (#7)
Re: Fix LogStandbySnapshot() race during logical decoding activation

On 2026-Nov-25, Masahiko Sawada wrote:

I've reviewed the v3 patch and felt the new comments were a bit long
and redundant to me.

The v3 patch looks good to me, thanks.

--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
"It takes less than 2 seconds to get to 78% complete; that's a good sign.
A few seconds later it's at 90%, but it seems to have stuck there. Did
somebody make percentages logarithmic while I wasn't looking?"
http://smylers.hates-software.com/2005/09/08/1995c749.html