Re: POC: enable logical decoding when wal_level = 'replica' without a server restart
shveta malik <shveta.malik@gmail.com>
From: shveta malik <shveta.malik@gmail.com>
To: Masahiko Sawada <sawada.mshk@gmail.com>
Cc: Peter Smith <smithpb2250@gmail.com>,
Amit Kapila <amit.kapila16@gmail.com>, "Hayato Kuroda (Fujitsu)" <kuroda.hayato@fujitsu.com>,
Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>, Shlok Kyal <shlok.kyal.oss@gmail.com>,
Bertrand Drouvot <bertranddrouvot.pg@gmail.com>, PostgreSQL-development <pgsql-hackers@postgresql.org>,
shveta malik <shveta.malik@gmail.com>
Date: 2025-11-12T11:42:20Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix regression test failure when wal_level is set to minimal.
- 0de5f0d869d1 19 (unreleased) landed
-
Toggle logical decoding dynamically based on logical slot presence.
- 67c20979ce72 19 (unreleased) landed
-
Disallow server start with sync_replication_slots = on and wal_level < logical.
- 12da45742cfd 19 (unreleased) cited
On Wed, Nov 12, 2025 at 3:36 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
>
> On Tue, Nov 11, 2025 at 6:05 PM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
> >
> > On Mon, Nov 10, 2025 at 8:05 PM shveta malik <shveta.malik@gmail.com> wrote:
> > >
> > > On Thu, Nov 6, 2025 at 4:32 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
> > > >
> > > >
> > > > I've updated and rebased the patch.
> > > >
> > >
> > > Thanks for the patch. Please find a few comments:
> > >
> > >
> > > 1)
> > > ReplicationSlotsDropDBSlots:
> > >
> > > + SpinLockAcquire(&s->mutex);
> > > + invalidated = s->data.invalidated == RS_INVAL_NONE;
> > > + SpinLockRelease(&s->mutex);
> > > +
> > > + /*
> > > + * Count slots on other databases too so we can disable logical
> > > + * decoding only if no slots in the cluster.
> > > + */
> > > + if (invalidated)
> > > + n_valid_logicalslots++;
> > >
> > >
> > > This seems confusing to me. Can we instead do:
> > >
> > > SpinLockAcquire(&s->mutex);
> > > if (s->data.invalidated == RS_INVAL_NONE)
> > > n_valid_logicalslots++;
> > > SpinLockRelease(&s->mutex);
> > >
> > > 2)
> > > InvalidateObsoleteReplicationSlots:
> > >
> > > + bool islogical = SlotIsLogical(s);
> > >
> > > /* Prevent invalidation of logical slots during binary upgrade */
> > > if (SlotIsLogical(s) && IsBinaryUpgrade)
> > > + {
> > > + SpinLockAcquire(&s->mutex);
> > > + if (s->data.invalidated == RS_INVAL_NONE)
> > > + n_valid_logicalslots++;
> > > + SpinLockRelease(&s->mutex);
> > > +
> > > continue;
> > > + }
> > >
> > > We should use 'islogical' instead of SlotIsLogical here.
> > >
> > > 3)
> > > InvalidateObsoleteReplicationSlots() is more robust now as we are
> > > using both 'invalidated' and 'released_lock' flags but still nowhere
> > > we guarantee that invalidated=true implies released_lock=true. Since
> > > we jump to 'restart' label only if released_lock is true, it becomes
> > > important to have an ASSERT which says invalidated=true implicitly
> > > means released_lock=true or vice versa. Because at the end we go by
> > > 'invalidated_logical' rather than 'released_lock' to decide about
> > > logical-decoding disabling.
> > >
> > > In this logic:
> > >
> > > + if (InvalidatePossiblyObsoleteSlot(possible_causes, s, oldestLSN,
> > > + dboid, snapshotConflictHorizon,
> > > + &released_lock))
> > > {
> > > - /* if the lock was released, start from scratch */
> > > - goto restart;
> > > + /* Remember we have invalidated a physical or logical slot */
> > > + invalidated = true;
> > > +
> > > + /*
> > > + * Additionally, remember we have invalidated a logical slot too
> > > + * as we can request disabling logical decoding later.
> > > + */
> > > + if (islogical)
> > > + invalidated_logical = true;
> > > }
> > >
> > > Shall we have an Assert(released_lock) if
> > > InvalidatePossiblyObsoleteSlot returns true. Or any better way?
> > >
> > > 4)
> > > + SpinLockAcquire(&s->mutex);
> > > + if (s->data.invalidated == RS_INVAL_NONE)
> > > + n_valid_logicalslots++;
> > >
> > > In the same function, isn't the above code problematic: Don't we need
> > > 'islogical' check before incrementing 'n_valid_logicalslots',
> > > otherwise it may wrongly count valid physical slots as well.
> >
> > Agreed with all the above points. Will fix and update the updated version.
> >
>
> I've attached the updated version patch. I addressed all comments I
> got so far, and made some cosmetic changes.
>
Thanks. A few comments:
1)
Shall we update comments atop InvalidateObsoleteReplicationSlots() as
well, similar to other functions. Something like:
If it invalidates the last logical slot in the cluster, it requests to
disable logical decoding.
2)
With the new sanity check (Assert(released_lock)) in
InvalidateObsoleteReplicationSlots, we have made sure that whenever a
slot is invalidated, we do release-lock. But we have not made sure
that released_lock=true always implies a slot is invalidated. Looking
at InvalidatePossiblyObsoleteSlot(), that seems to be the case always,
but shall we have a sanity check in for this as well. Thoughts?
Something like:
if (InvalidatePossiblyObsoleteSlot(...)
{
Assert(released_lock); (already there)
....
}
else
{
Assert (!released_lock) (to be added)
}
3)
Regarding this new comment:
+ * deactivation. We cannot rely on the end-of-recovery to allow toggling
+ * of the logical decoding status because it's possible that concurrent
+ * processes write non-logical WAL records after the startup process
+ * enables logical decoding.
Do we really want to say 'to allow toggling' here or 'to restrict
toggling'. Let me know if I have misunderstood.
thanks
Shveta