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.
| Field | Meaning | Why it matters |
|---|---|---|
slot_type | Physical or logical | Determines what the slot protects |
active | Whether a consumer is connected | Inactive slots still retain WAL |
restart_lsn | Oldest WAL the consumer may need | Drives WAL retention on disk |
confirmed_flush_lsn | Last position a logical consumer confirmed | Defines where decoding resumes |
catalog_xmin | Oldest transaction whose catalog rows are needed | Holds back vacuum of system catalogs |
wal_status | Reserved, extended, unreserved or lost | Shows whether the slot is still usable |
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.
| Property | Physical slot | Logical slot |
|---|---|---|
| Typical consumer | Standby server | Subscriber or CDC tool |
| Scope | Whole cluster | One database |
| Data format | Raw WAL records | Decoded row changes |
| Output plugin | Not used | Required, such as pgoutput |
| Holds back vacuum | Only with hot_standby_feedback | Always, through catalog_xmin |
| Created with | pg_create_physical_replication_slot | pg_create_logical_replication_slot |
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.
Replication slots and related retention mechanisms
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.
| Mechanism | Retention rule | Main weakness |
|---|---|---|
| Replication slot | Until the consumer confirms | Unbounded growth if the consumer stalls |
| wal_keep_size | A fixed amount of recent WAL | A lagging replica may still lose its position |
| WAL archiving | Copies segments to external storage | Replica must fetch from the archive |
| max_slot_wal_keep_size | Caps the WAL any slot can hold | An overrun slot becomes lost |
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’srestart_lsnusingpg_wal_lsn_diff. - Alert on inactive slots: an
active = falseslot that persists for hours usually indicates an abandoned consumer. - Drop unused slots promptly:
pg_drop_replication_slotreleases the retained WAL immediately. - Use idle timeouts where available: PostgreSQL 18 adds
idle_replication_slot_timeoutto 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.
| Symptom | Cause | Fix |
|---|---|---|
pg_wal directory fills the disk | Abandoned or stalled slot | Drop the slot or restore the consumer |
wal_status shows lost | Retention cap exceeded | Recreate the slot and resynchronize the consumer |
| Table and catalog bloat grows | Old `catalog_xmin` or xmin | Advance or drop the lagging slot |
| Logical replication stops after failover | Slot missing on new primary | Enable slot synchronization or recreate it |
| Slot cannot be created | Slot limit reached | Raise max_replication_slots and restart |
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.