Replication slot

On this page 7

A replication slot is a named, persistent object on a PostgreSQL server that records how far a replication consumer has progressed and guarantees that the server retains the write-ahead log (WAL) that consumer has not yet received. In database replication, slots make streaming replicas and logical decoding clients safe against WAL removal, so that a temporarily disconnected consumer can resume exactly where it stopped.

The same guarantee is also the main risk: a slot whose consumer never returns keeps WAL indefinitely, and unbounded WAL growth can fill the disk of the primary server and stop it.

How a replication slot retains WAL

A replication slot stores a log sequence number (LSN) that marks the oldest WAL position its consumer still requires. Before the checkpointer recycles or deletes WAL segment files, it checks the minimum restart_lsn across all slots and keeps every segment from that point onward. The consumer advances the slot by confirming receipt or processing of changes.

Slots are crash-safe: their state is written to disk under pg_replslot and survives a server restart. Before slots existed, administrators relied on wal_keep_size (formerly wal_keep_segments), a fixed buffer that either wasted space or proved too small when a replica fell far behind. Slots replace that guess with exact per-consumer tracking.

The slot state is exposed in the pg_replication_slots view. Its most important columns are summarized below.

FieldMeaningWhy it matters
slot_typePhysical or logicalDetermines what the slot protects
activeWhether a consumer is connectedInactive slots still retain WAL
restart_lsnOldest WAL the consumer may needDrives WAL retention on disk
confirmed_flush_lsnLast position a logical consumer confirmedDefines where decoding resumes
catalog_xminOldest transaction whose catalog rows are neededHolds back vacuum of system catalogs
wal_statusReserved, extended, unreserved or lostShows whether the slot is still usable
Key fields of the pg_replication_slots view

Physical and logical replication slots

PostgreSQL provides two slot types that serve different replication methods. A physical slot supports streaming replication of the entire cluster to a standby, while a logical slot supports logical replication and change data capture through an output plugin such as pgoutput or wal2json.

PropertyPhysical slotLogical slot
Typical consumerStandby serverSubscriber or CDC tool
ScopeWhole clusterOne database
Data formatRaw WAL recordsDecoded row changes
Output pluginNot usedRequired, such as pgoutput
Holds back vacuumOnly with hot_standby_feedbackAlways, through catalog_xmin
Created withpg_create_physical_replication_slotpg_create_logical_replication_slot
Physical versus logical replication slots

Logical slots have an additional cost: they must retain old system catalog rows so that the decoder can interpret historical WAL with the table definitions valid at that time. A stalled logical slot therefore causes catalog bloat as well as WAL accumulation. Since PostgreSQL 17, logical slots can also be synchronized to a standby, so that logical consumers survive a failover.

A replication slot is one of several ways to keep WAL available, and it differs from the others in being driven by consumer progress rather than by a fixed size or an external archive. Comparable concepts exist in other systems, for example the binary log retention in MySQL and the transaction log retention behind Always On availability groups.

MechanismRetention ruleMain weakness
Replication slotUntil the consumer confirmsUnbounded growth if the consumer stalls
wal_keep_sizeA fixed amount of recent WALA lagging replica may still lose its position
WAL archivingCopies segments to external storageReplica must fetch from the archive
max_slot_wal_keep_sizeCaps the WAL any slot can holdAn overrun slot becomes lost
WAL retention options compared

Production setups often combine these mechanisms: slots for precise retention, a cap through max_slot_wal_keep_size as a safety limit, and WAL archiving for point-in-time recovery and replica rebuilding.

Configuring and operating replication slots

Correct configuration starts with capacity parameters and ends with monitoring. The max_replication_slots setting limits how many slots may exist, and wal_level must be replica for physical slots or logical for logical slots. Tools such as Patroni create and manage physical slots for cluster members automatically.

  • Set a retention cap: max_slot_wal_keep_size (PostgreSQL 13 and later) protects the primary from disk exhaustion.
  • Monitor retained WAL: compare pg_current_wal_lsn() with each slot’s restart_lsn using pg_wal_lsn_diff.
  • Alert on inactive slots: an active = false slot that persists for hours usually indicates an abandoned consumer.
  • Drop unused slots promptly: pg_drop_replication_slot releases the retained WAL immediately.
  • Use idle timeouts where available: PostgreSQL 18 adds idle_replication_slot_timeout to invalidate long-idle slots.

A temporary slot exists only for the session that created it and is dropped automatically on disconnect, which makes it suitable for base backups with pg_basebackup and for short-lived tests.

Failure modes of replication slots

Most incidents involving replication slots follow a small number of recognizable patterns. The table below lists the symptoms, their causes and the standard remedies.

SymptomCauseFix
pg_wal directory fills the diskAbandoned or stalled slotDrop the slot or restore the consumer
wal_status shows lostRetention cap exceededRecreate the slot and resynchronize the consumer
Table and catalog bloat growsOld `catalog_xmin` or xminAdvance or drop the lagging slot
Logical replication stops after failoverSlot missing on new primaryEnable slot synchronization or recreate it
Slot cannot be createdSlot limit reachedRaise max_replication_slots and restart
Common replication slot problems

The central trade-off is clear: a slot without a cap favors consumer correctness over primary availability, while a cap favors primary availability over consumer continuity. Most operators choose the cap and accept occasional resynchronization.

Put simply

Think of the database as a newspaper office that keeps old editions only for a while. A replication slot is a reservation note for one subscriber: it says which editions that subscriber has not yet collected, and the office promises not to throw those away.

This works well while the subscriber keeps coming back. If the subscriber disappears and nobody removes the note, the old editions pile up until the storage room is full. That is why slots need someone to watch them.

In practice

A logistics company streams order changes from its PostgreSQL primary to an analytics platform through a logical replication slot read by a change data capture connector. One Friday evening, the connector crashes after a configuration change, and nobody notices over the weekend. The slot keeps retaining WAL, and by Sunday the pg_wal directory has grown from about 2 GB to over 300 GB, with the data disk at 94 percent.

An alert on retained WAL size wakes the on-call engineer before the disk fills. The connector is restarted and catches up within two hours, and the slot releases the retained segments. Afterward, the team sets max_slot_wal_keep_size to 100 GB and adds an alert for slots inactive longer than 30 minutes, so that a stalled consumer can no longer endanger the primary.