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: Amit Kapila <amit.kapila16@gmail.com>, Bertrand Drouvot <bertranddrouvot.pg@gmail.com>, Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>, PostgreSQL-development <pgsql-hackers@postgresql.org>,
shveta malik <shveta.malik@gmail.com>
Date: 2025-07-23T06:44:37Z
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 Tue, Jul 22, 2025 at 5:03 AM Masahiko Sawada <sawada.mshk@gmail.com> wrote:
>
> Yes, I agree. The main patch focuses on the part where we
> automatically change the effective WAL level upon the logical slot
> creation and deletion (and potentially remove 'logical' from
> wal_level), and other things are implemented as additional features in
> a separate patch.
I am keeping my focus on patch001 until we decide further on how to
protect the slot. Apart from few comments in [1], please find one
more concern:
There is a race condition between creating and dropping a replication
slot when enabling or disabling logical decoding. We might end up with
logical decoding disabled even when a logical slot is present.
Steps:
1) Set wal_level=replica on primary.
2) Create logical_slot1 which will enable logical decoding, causing
effective_wal_level to become logical.
3) Drop logical_slot1 and pause execution inside
DisableLogicalDecodingIfNecessary() right after the
'n_inuse_logical_slots' check using a debugger.
4) In another session, create logical_slot2. It will attempt to enable
logical-decoding but since it is already enabled,
EnsureLogicalDecodingEnabled() will be a no-op.
5) Release debugger of drop-slot, it will disable logical decoding.
Ultimately, logical_slot2is present while logical decoding is disabled
and thus we see this:
postgres=# select slot_name from pg_replication_slots;
slot_name
---------------
logical_slot2
postgres=# show effective_wal_level;
effective_wal_level
---------------------
replica
(1 row)
postgres=# select pg_logical_slot_get_changes('logical_slot2', NULL,
NULL, 'proto_version', '4', 'publication_names', 'pub');
ERROR: logical decoding is not enabled
HINT: Set "wal_level" >= "logical" or create at least one logical slot.
Shall we acquire LogicalDecodingControlLock in exclusive mode at a
little earlier stage? Currently we acquire it after
IsLogicalDecodingEnabled() check. I think we shall acquire it before
this check in in both enable and disable flow?
[1]: https://www.postgresql.org/message-id/CAJpy0uC0e%3DJ7L4q9RnQ3pbSAtvWy40r9qp3tr41zoogHQmDO8g%40mail.gmail.com
thanks
Shveta