Re: Fix LogStandbySnapshot() race during logical decoding activation

From: Zhijie Hou <houzhijie22(at)gmail(dot)com>
To: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix LogStandbySnapshot() race during logical decoding activation
Date: 2026-11-24 03:11:56
Message-ID: 1828e313-7c11-40f3-81bc-63deb10f6b7a@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Tue, Nov24, 2026 at 06:25 AM Masahiko Sawada wrote:
> On Sun, Nov 22, 2026 at 9:46 AM Zhijie Hou <houzhijie22(at)gmail(dot)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

Attachment Content-Type Size
v2-0001-Fix-LogStandbySnapshot-race-when-enabling-logical.patch text/plain 7.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-11-24 03:14:31 Re: Rework: Enable fast default for domains with non-volatile constraints
Previous Message Zhijie Hou 2026-11-24 03:04:20 Re: Fix standby logical slot creation during dynamic logical decoding activation