Re: Fix LogStandbySnapshot() race during logical decoding activation

From: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
To: Narayanan Venkateswaran <narayananvpostgres(at)gmail(dot)com>
Cc: houzhijie22(at)gmail(dot)com, alvherre(at)kurilemu(dot)de, kuroda(dot)hayato(at)fujitsu(dot)com, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix LogStandbySnapshot() race during logical decoding activation
Date: 2026-12-01 18:47:30
Message-ID: CAD21AoCBxh8PGEaZvfmXwFVGuvSLkKURqN61sTS1=3xP1CtQBA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Dec 1, 2026 at 8:19 AM Narayanan Venkateswaran
<narayananvpostgres(at)gmail(dot)com> wrote:
>
> Hi Masahiko, Zhijie,
>
> > I've pushed the v5 patch.
>
> The new Assert in LogStandbySnapshot() runs while XidGenLock is still held:
>
> /* 1. standby.c: LogStandbySnapshot() (when xlog_logical_info is false) */
> Assert(xlog_logical_info || !IsLogicalDecodingEnabled()); /* takes
> LogicalDecodingControlLock */
> ...
> LWLockRelease(XidGenLock);
>
> That reverses the lock order used elsewhere and can deadlock on
> --enable-cassert builds:
>
> /* 2. logicalctl.c: DisableLogicalDecoding() */
> LWLockAcquire(LogicalDecodingControlLock, LW_EXCLUSIVE);
> ... CheckLogicalSlotExists() /* takes
> ReplicationSlotControlLock */
>
> /* 3. launcher.c: reset_conflict_slot_xmin_to_safe_horizon() */
> LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
> LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
>
> /* 4. procarray.c: ProcArrayAdd() */
> LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
> LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
>
> I reproduced this deadlock locally on master with --enable-cassert.

Thank you for the report.

I agree with your analysis. I was careful about not taking
LogicalDecodingControlLock while holding ProcArrayLock, but missed
potential deadlocks with a combination of other multiple processes.

> we can just move the Assert right after LWLockRelease(XidGenLock);

I think it's the right fix. We don't process interrupts between
checking xlog_logical_info and the new location of the assertion, so
it still holds there.

I've made a cosmetic change and updated the commit message. I'm going
to push it, barring objections.

Regards,

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

Attachment Content-Type Size
v2-0001-Fix-possible-deadlock-in-an-assertion-in-LogStand.patch text/x-patch 2.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Corey Huinker 2026-12-01 19:10:01 Re: postgres_fdw can crash on malformed remote results
Previous Message David Rowley 2026-12-01 18:46:04 Re: Should we skip foreign key checks in binary upgrade mode?