Missed in PostgreSQL 19 : Toggle logical decoding dynamically based on logical slot presence.

On 23rd of December 2025, Masahiko Sawada committed patch:

Toggle logical decoding dynamically based on logical slot presence.
 
Previously logical decoding required wal_level to be set to 'logical'
at server start. This meant that users had to incur the overhead of
logical-level WAL logging even when no logical replication slots were
in use.
 
This commit adds functionality to automatically control logical
decoding availability based on logical replication slot presence. The
newly introduced module logicalctl.c allows logical decoding to be
dynamically activated when needed when wal_level is set to
'replica'.
 
When the first logical replication slot is created, the system
automatically increases the effective WAL level to maintain
logical-level WAL records. Conversely, after the last logical slot is
dropped or invalidated, it decreases back to 'replica' WAL level.
 
While activation occurs synchronously right after creating the first
logical slot, deactivation happens asynchronously through the
checkpointer process. This design avoids a race condition at the end
of recovery; a concurrent deactivation could happen while the startup
process enables logical decoding at the end of recovery, but WAL
writes are still not permitted until recovery fully completes. The
checkpointer will handle it after recovery is done. Asynchronous
deactivation also avoids excessive toggling of the logical decoding
status in workloads that repeatedly create and drop a single logical
slot. On the other hand, this lazy approach can delay changes to
effective_wal_level and the disabling logical decoding, especially
when the checkpointer is busy with other tasks. We chose this lazy
approach in all deactivation paths to keep the implementation simple,
even though laziness is strictly required only for end-of-recovery
cases. Future work might address this limitation either by using a
dedicated worker instead of the checkpointer, or by implementing
synchronous waiting during slot drops if workloads are significantly
affected by the lazy deactivation of logical decoding.
 
The effective WAL level, determined internally by XLogLogicalInfo, is
allowed to change within a transaction until an XID is assigned. Once
an XID is assigned, the value becomes fixed for the remainder of the
transaction. This behavior ensures that the logging mode remains
consistent within a writing transaction, similar to the behavior of
GUC parameters.
 
A new read-only GUC parameter effective_wal_level is introduced to
monitor the actual WAL level in effect. This parameter reflects the
current operational WAL level, which may differ from the configured
wal_level setting.
 
Bump PG_CONTROL_VERSION as it adds a new field to CheckPoint struct.
 
Reviewed-by: Shveta Malik <shveta.malik@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Reviewed-by: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Reviewed-by: Peter Smith <smithpb2250@gmail.com>
Reviewed-by: Shlok Kyal <shlok.kyal.oss@gmail.com>
Reviewed-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Discussion: https://postgr.es/m/CAD21AoCVLeLYq09pQPaWs+Jwdni5FuJ8v2jgq-u9_uFbcp6UbA@mail.gmail.com

This one I really should have written about when it was committed. It's one of these changes that will make life of DBAs much simpler.

Up to, and including, PostgreSQL 18, if you wanted to use logical replication (or any kind of logical decoding, like for CDC tools), you had to have wal_level set to logical. Changing it required restart. So, quite a lot of people just set it to logical “just in case", paying for additional WAL traffic even if logical replication was never used. And those that didn't, had to schedule restart when they needed it.

Now, it's much nicer. My test server has:

=$ show wal_level;
 wal_level
───────────
 replica
(1 row)
 
=$ show effective_wal_level;
 effective_wal_level
─────────────────────
 replica
(1 row)
 
=$ select name, context, short_desc from pg_settings where name in ('wal_level', 'effective_wal_level');
        name         │  context   │                    short_desc
─────────────────────┼────────────┼───────────────────────────────────────────────────
 effective_wal_level │ internal   │ Shows effective WAL level.
 wal_level           │ postmaster │ Sets the level of information written to the WAL.
(2 rows)

wal_level is still postmaster level setting, but there is new, read-only, effective_wal_level which shows what is really happening.

So, let's create logical replication slot:

=$ create table ld_test (id int4 primary key, payload text);
CREATE TABLE
 
=$ select * from pg_create_logical_replication_slot('missed_test', 'test_decoding');
  slot_name  │    lsn
─────────────┼────────────
 missed_test │ 0/E8614138
(1 row)
 
=$ show effective_wal_level;
 effective_wal_level
─────────────────────
 logical
(1 row)

On Pg 18 this would fail with error saying that logical decoding requires wal_level >= logical. But now it simply worked, and effective wal level got changed to logical. In server log I got:

2026-12-02 11:33:52.034 CET depesz@depesz 60759 [local] LOG:  logical decoding is enabled upon creating a new logical replication slot
2026-12-02 11:33:52.034 CET depesz@depesz 60759 [local] STATEMENT:  select * from pg_create_logical_replication_slot('missed_test', 'test_decoding');

And decoding works:

=$ insert into ld_test values (1, 'first row'), (2, 'second row');
INSERT 0 2
 
=$ update ld_test set payload = 'changed' where id = 2;
UPDATE 1
 
=$ select * from pg_logical_slot_get_changes('missed_test', null, null);
    lsn     │ xid │                                  data
────────────┼─────┼────────────────────────────────────────────────────────────────────────
 0/E8614138 │ 735 │ BEGIN 735
 0/E8614138 │ 735 │ table public.ld_test: INSERT: id[integer]:1 payload[text]:'first row'
 0/E8614220 │ 735 │ table public.ld_test: INSERT: id[integer]:2 payload[text]:'second row'
 0/E86142D8 │ 735 │ COMMIT 735
 0/E86142D8 │ 736 │ BEGIN 736
 0/E86142D8 │ 736 │ table public.ld_test: UPDATE: id[integer]:2 payload[text]:'changed'
 0/E8614358 │ 736 │ COMMIT 736
(7 rows)

What happens when I drop the slot?

=$ select pg_drop_replication_slot('missed_test');
 pg_drop_replication_slot
──────────────────────────
 
(1 row)
 
=$ show effective_wal_level;
 effective_wal_level
─────────────────────
 logical
(1 row)
 
=$ select pg_sleep(1);
 pg_sleep
──────────
 
(1 row)
 
=$ show effective_wal_level;
 effective_wal_level
─────────────────────
 replica
(1 row)

As described in commit message – switching back to replica is done asynchronously, by checkpointer. In my case it took less than a second, and in logs I saw:

2026-12-02 11:35:49.115 CET @ 60601  LOG:  logical decoding is disabled because there are no valid logical replication slots

Current state is also visible in output of pg_control_checkpoint(), which got new column logical_decoding:

=$ select checkpoint_lsn, logical_decoding from pg_control_checkpoint();
 checkpoint_lsn │ logical_decoding
────────────────┼──────────────────
 6/0E37F828     │ f
(1 row)

pg_controldata shows it too.

Of course, test_decoding is nice, but how about “real" logical replication, using publication and subscription? Let's try. In database depesz I made table and publication, and then in ld_sub database (on the same server, just to make the test simpler) I created subscription.

One note: because both databases are on the same instance, I had to create the slot manually, and tell subscription to not create it. Otherwise CREATE SUBSCRIPTION hangs – but that's nothing new, it has always been like this when subscribing to the same cluster.

=$ show effective_wal_level;
 effective_wal_level
─────────────────────
 replica
(1 row)
 
=$ create table ld_test (id int4 primary key, payload text);
CREATE TABLE
 
=$ insert into ld_test values (1, 'before subscription');
INSERT 0 1
 
=$ create publication ld_pub for table ld_test;
CREATE PUBLICATION
 
=$ create database ld_sub;
CREATE DATABASE
 
=$ \c ld_sub
You are now connected to database "ld_sub" as user "depesz".
 
=$ create table ld_test (id int4 primary key, payload text);
CREATE TABLE
 
=$ \c depesz
You are now connected to database "depesz" as user "depesz".
 
=$ select * from pg_create_logical_replication_slot('ld_sub', 'pgoutput');
 slot_name │    lsn
───────────┼────────────
 ld_sub    │ 0/E8AC87F0
(1 row)
 
=$ show effective_wal_level;
 effective_wal_level
─────────────────────
 logical
(1 row)
 
=$ \c ld_sub
You are now connected to database "ld_sub" as user "depesz".
 
=$ create subscription ld_sub connection 'dbname=depesz' publication ld_pub with (create_slot = false);
CREATE SUBSCRIPTION
 
=$ select * from ld_test;
 id │       payload
────┼─────────────────────
  1 │ before subscription
(1 row)

Initial sync worked. Now, new row on the publisher side:

=$ \c depesz
You are now connected to database "depesz" as user "depesz".
 
=$ select slot_name, plugin, slot_type, active from pg_replication_slots;
 slot_name │  plugin  │ slot_type │ active
───────────┼──────────┼───────────┼────────
 ld_sub    │ pgoutput │ logical   │ t
(1 row)
 
=$ insert into ld_test values (2, 'after subscription');
INSERT 0 1
 
=$ \c ld_sub
You are now connected to database "ld_sub" as user "depesz".
 
=$ select * from ld_test;
 id │       payload
────┼─────────────────────
  1 │ before subscription
  2 │ after subscription
(2 rows)

And after dropping the subscription (which also drops the slot on publisher):

=$ drop subscription ld_sub;
NOTICE:  dropped replication slot "ld_sub" on publisher
DROP SUBSCRIPTION
 
=$ \c depesz
You are now connected to database "depesz" as user "depesz".
 
=$ show effective_wal_level;
 effective_wal_level
─────────────────────
 replica
(1 row)

All of this without touching config, and without restart. This is great.

The one thing to remember – the switch is driven by existence of logical slots. If you have some old, forgotten, logical slot lying around, your effective wal level will stay at logical. Which is yet another reason to monitor pg_replication_slots.

Thanks a lot to everyone involved.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.