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-02 18:49:59
Message-ID: CAD21AoAt43_tVv4M0E5-wWRdJi9VBc9Td37g2i6zX+40k8xQ8g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Dec 1, 2026 at 10:47 AM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> wrote:
>
> 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.

Pushed.

Regards,

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Hannu Krosing 2026-12-02 18:43:21 [PATCH] Do not reuse recovery snapshots after recovery ends