Replication slot

Last checked · 2 min read

A replication slot is a named, crash-safe bookmark in a Postgres server's change stream. It remembers how far its reader has got, and the server keeps the write-ahead log that reader still needs, even while the reader is disconnected. Logical decoding, logical replication and most change data capture tools read through one.

What a slot does

Postgres writes every change to its write-ahead log (WAL), and normally recycles old WAL once a checkpoint no longer needs it. A reader that streams changes needs the server to wait for it. A slot is that promise. In the documentation’s words, a logical slot “represents a stream of changes that can be replayed to a client in the order they were made on the origin server”.1

Two properties follow:

  • It outlives the connection. Slots persist independently of the connection using them and are crash-safe.1 A reader can disconnect, restart, and carry on from where it stopped, with nothing missed.
  • One reader at a time. Only one receiver may consume changes from a slot at any given time.1 Two consumers need two slots.

The cost: WAL that can’t be removed

A slot keeps what its reader hasn’t consumed, whether or not anything is connected. The documentation is blunt: slots “will prevent removal of required resources even when there is no connection using them”, neither the WAL nor the catalog rows a slot needs can be removed by VACUUM, and in extreme cases this could cause the database to shut down to prevent transaction ID wraparound. Its advice is to drop a slot that is no longer required.1

The example shows how quickly it adds up. Nothing was reading audit_slot, so after one 200,000-row insert it was holding 25 MB of WAL. A slot left behind by a tool you stopped using keeps growing like that until the disk fills.

Keeping slots safe

  • Cap the WAL a slot may hold. max_slot_wal_keep_size limits how much WAL slots may retain. Its default, -1, lets them retain an unlimited amount.3 Past the cap, a reader that falls too far behind may not be able to continue, which is better than a full disk.
  • Expire idle slots. PostgreSQL 18 adds idle_replication_slot_timeout, which invalidates slots that have been inactive for longer than the limit. It is off (0) by default.3
  • Watch them. pg_replication_slots shows each slot, whether it is active, its wal_status, and its restart_lsn, from which you can compute how much WAL it holds, as in the example.
  • Count them. max_replication_slots caps how many slots the server supports; the default is 10.3
  • Drop what you don’t use, with pg_drop_replication_slot.

Why audits use one

An audit that reads the change stream must not miss a transaction because its reader restarted. A slot gives exactly that: every committed change, in commit order, from the reader’s last position. That is what lets a reader say “these are all the rows this transaction changed” rather than “these are the rows we happened to see”.

An example

PostgreSQL 18.6: an idle logical slot after a 200,000-row inserttext
shop=# SELECT slot_name FROM pg_create_logical_replication_slot('audit_slot', 'pgoutput');
shop=# INSERT INTO public.orders SELECT g, 'paid' FROM generate_series(1000, 200999) g;
INSERT 0 200000
shop=# CHECKPOINT;
shop=# SELECT slot_name, active, wal_status,
         pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS wal_held
       FROM pg_replication_slots;
 slot_name  | active | wal_status | wal_held
------------+--------+------------+----------
 audit_slot | f      | reserved   | 25 MB
shop=# SHOW max_slot_wal_keep_size;
 -1

How Deedglass uses it

Observe mode reads Postgres's change log through the deedglass_witness role and the replication slot that deedglass connector postgres init creates.4 deedglass connector postgres check notes when max_slot_wal_keep_size is -1, because a stalled reader could then fill the disk, and suggests 4GB; on PostgreSQL 18 it also notes an idle-slot timeout under a day.

Related on this site

Sources

  1. 1PostgreSQL 18 documentation: Logical decoding concepts · postgresql.org · checked 18 October 2026
  2. 2PostgreSQL 18 documentation: Log-shipping standby servers (replication slots) · postgresql.org · checked 18 October 2026
  3. 3PostgreSQL 18 documentation: Replication settings · postgresql.org · checked 18 October 2026
  4. 4Deedglass docs: Observe mode beside another gate · docs.deedglass.com · checked 18 October 2026
  5. 5Deedglass docs: The Postgres connector · docs.deedglass.com · checked 18 October 2026
  6. 6Deedglass source: the Postgres connector check (slot settings) · github.com · checked 18 October 2026

Last checked 18 October 2026. We re-check the glossary every month. If something here is wrong, write to hello@deedglass.com.