DEV Community

Alex Georgiev
Alex Georgiev

Posted on AI-assisted

Turning on Postgres logical replication without a restart

PostgreSQL 19 went GA on 19 November, five days before I wrote this, and one line in the release notes stopped me scrolling: logical replication can now turn itself on without a server restart, as long as wal_level is already set to replica. Adding logical replication to a database that's already serving traffic has always meant the same tedious step: ALTER SYSTEM SET wal_level = 'logical', then a restart, then the moment of "sorry, back in a minute" while the server comes back up with a different WAL level. So I pulled the GA image and checked whether that line in the release notes holds up.

The old way, and the new one

I ran two throwaway containers, both with wal_level=replica, nothing special:

docker run -d --name pg18 -e POSTGRES_PASSWORD=test -p 15433:5432 postgres:18 \
  -c wal_level=replica -c max_replication_slots=5 -c max_wal_senders=5
docker run -d --name pg19 -e POSTGRES_PASSWORD=test -p 15432:5432 postgres:19 \
  -c wal_level=replica -c max_replication_slots=5 -c max_wal_senders=5
Enter fullscreen mode Exit fullscreen mode

On 18.7, asking for a logical slot at that wal_level gets you the error everyone who has done this before already knows:

$ psql -c "SELECT pg_create_logical_replication_slot('test_slot', 'pgoutput');"
ERROR:  logical decoding requires "wal_level" >= "logical"
Enter fullscreen mode Exit fullscreen mode

On 19.0, the exact same command, same config, no restart in between:

$ psql -c "SELECT pg_create_logical_replication_slot('test_slot', 'pgoutput');"
 pg_create_logical_replication_slot
------------------------------------
 (test_slot,0/0175F720)
(1 row)
Enter fullscreen mode Exit fullscreen mode

It just works. SHOW wal_level still reports replica - the setting on disk hasn't moved - but there's a new value that has:

$ psql -c "SHOW wal_level; SHOW effective_wal_level;"
 wal_level
-----------
 replica

 effective_wal_level
----------------------
 logical
Enter fullscreen mode Exit fullscreen mode

effective_wal_level is new in 19. The server quietly runs at logical the moment something needs it to, and reports replica for everything else. That's the whole trick.

What the restart actually costs

To put a number on what this avoids, I did it the old way on 18.7: ALTER SYSTEM SET wal_level = 'logical', then a restart, while a second process hammered the server with SELECT 1 every 50ms and logged any failures. Three runs, each a fresh restart:

Run Downtime window
1 0.36s
2 0.33s
3 0.33s

Then the same probe loop against 19.0 while I created a second logical slot in the middle of it - 200 probes at 50ms intervals, so a ten-second window either side:

no downtime observed
Enter fullscreen mode Exit fullscreen mode

Zero failed probes. I should be honest about what this is and isn't: a docker restart on an idle local container, not a production Postgres recovering from a real shutdown checkpoint under load, which will usually take longer than a third of a second. But the shape of the result doesn't depend on that - one path has a restart in it, the other has none, and that's the entire difference.

Proving it actually replicates, not just that the slot gets created

A slot creation call succeeding is not the same as logical replication working. I wired up a second container as a subscriber on the same Docker network and ran the real thing against the 19.0 publisher, which was still sitting at wal_level=replica the entire time:

-- on the publisher
CREATE PUBLICATION pub_test FOR TABLE repltest;

-- on the subscriber
CREATE SUBSCRIPTION sub_test
  CONNECTION 'host=pg19 port=5432 dbname=postgres user=postgres password=test'
  PUBLICATION pub_test;
Enter fullscreen mode Exit fullscreen mode
NOTICE:  created replication slot "sub_test" on publisher
CREATE SUBSCRIPTION
Enter fullscreen mode Exit fullscreen mode

Insert, update, delete, all checked on the subscriber afterwards:

Action on publisher Subscriber state
INSERT (1,'hello'), (2,'from publisher') both rows present
UPDATE id=1 SET note='updated' row 1 updated
DELETE id=2 row 2 gone

All of it arrived, in order, without me ever running ALTER SYSTEM SET wal_level.

I also wanted to know whether this only works on an empty table, since a fresh demo table is not a realistic starting point. I loaded 50,000 rows into a publisher table first, then created the subscription against it: the initial sync (a separate, COPY-based mechanism from the WAL decoding) picked up all 50,000 rows in 0.23 seconds, still with the publisher at wal_level=replica throughout.

Where it still refuses

The "automatic" part only covers the WAL level. It doesn't bypass the resource limits around replication, which is the sensible thing for it to do but worth confirming. On a container started with max_replication_slots=0 and max_wal_senders=0, still at wal_level=replica:

ERROR:  replication slots can only be used if "max_replication_slots" > 0
Enter fullscreen mode Exit fullscreen mode

So the zero-restart path only works if you already planned for at least one slot and one WAL sender. If you've been running with those at zero to save a few bytes of shared memory, you're back to a restart - just a different one than before.

Dropping the only logical slot reverses the effect immediately, no checkpoint or delay needed:

$ psql -c "SELECT pg_drop_replication_slot('test_slot2');"
$ psql -c "SHOW effective_wal_level;"
 effective_wal_level
----------------------
 replica
Enter fullscreen mode Exit fullscreen mode

The number that argues against the headline

My assumption going in was that running at effective_wal_level=logical would cost more WAL even before anything subscribes, because that's the usual story with logical decoding. I bloated a 20,000-row table with the default REPLICA IDENTITY and ran the same full-table UPDATE four times, alternating with and without an active logical slot, measuring WAL bytes via pg_wal_lsn_diff:

State (default identity) WAL generated
Logical slot active 6133 kB
Logical slot active 6167 kB
No logical slot 6151 kB
No logical slot 6169 kB

That's noise, not a trend. For a table with the default identity, the automatic escalation didn't add a measurable write-time cost. I didn't want to leave it there, because the usual advice is that REPLICA IDENTITY FULL is where logical decoding gets expensive, so I ran the same comparison on a second table with REPLICA IDENTITY FULL set:

State (REPLICA IDENTITY FULL) WAL generated
No logical slot 6200 kB
No logical slot 6155 kB
Logical slot active 8360 kB
Logical slot active 8353 kB

That's a real difference, about 35% more WAL for the same 20,000-row update, because FULL identity means the whole old row goes into the WAL record once something might decode it, not just the key. So the automatic escalation is free on an ordinary table and genuinely not free on a FULL-identity one - the cost was always tied to REPLICA IDENTITY, not to whether you had to restart to get there, and PostgreSQL 19 doesn't change that part at all.

How you'd actually see this in production

SHOW effective_wal_level is live. The on-disk control file is not. pg_control_checkpoint() has a logical_decoding boolean, and right after creating a slot it still read f until I ran CHECKPOINT by hand - the field reflects the last checkpoint, not the current instant:

$ psql -c "SELECT logical_decoding FROM pg_control_checkpoint();"
 f
$ psql -c "CHECKPOINT;"
$ psql -c "SELECT logical_decoding FROM pg_control_checkpoint();"
 t
Enter fullscreen mode Exit fullscreen mode

Anything that monitors replication readiness by reading the control file directly - backup tooling, some HA agents - will lag behind the live catalog view by up to a checkpoint interval. SHOW effective_wal_level or pg_controldata after a forced checkpoint are the two honest ways to check, and they can disagree for a while.

What I got wrong on the way

My first attempt at testing concurrency was to loop single-row inserts through docker exec psql while a slot was created in the middle. The loop's own per-call overhead - spawning a fresh psql process through docker exec every time - sat around 100ms, bigger than any lock a slot-creation call would plausibly hold. I got a flat, boring latency graph that told me nothing except that my harness was the slowest thing in the room.

Rather than report that as "no effect", I redid it with pgbench running natively inside one connection instead of one docker exec per statement: four clients, eight seconds, a plain single-row insert as the script, with a logical slot created three seconds in. 38,162 transactions went through, zero failures. Latency in the second where the slot got created peaked at 10.9ms against a baseline that peaked at 3-5ms most other seconds, with the mean unchanged at under a millisecond. There's a small, real blip, not a stall.

Run it yourself

docker run -d --name pg19 -e POSTGRES_PASSWORD=test postgres:19 -c wal_level=replica
sleep 5
docker exec pg19 psql -U postgres -c \
  "SELECT pg_create_logical_replication_slot('t', 'pgoutput'); SHOW effective_wal_level;"
Enter fullscreen mode Exit fullscreen mode

What I'd do now

If I'm adding logical replication to something that's already serving traffic, I'm not scheduling a maintenance window for it any more, provided I checked max_replication_slots and max_wal_senders are non-zero beforehand - that one check is the whole difference between zero downtime and the same restart I was trying to avoid. I'd still force a CHECKPOINT right after setting it up before I trust any tooling that reads the control file instead of querying the server. And if any of the tables involved use REPLICA IDENTITY FULL, I'd budget for the WAL growth up front rather than be surprised by it later - the restart disappeared, but that particular cost didn't.

Top comments (0)