In PostgreSQL, what does a physical replication slot hold back on the primary compared with a logical slot, as pg_replication_slots shows it?
answer
- WAL and rows, two kinds of pin
- feedback fills a physical xmin
- logical decoding needs old catalogs
- restart_lsn trails confirmed_flush_lsn
basics
~10 sBoth slot types pin WAL from restart_lsn. A physical slot also pins xmin for all tables when its standby sends hot_standby_feedback. A logical slot pins catalog_xmin, keeping old system-catalog rows, and tracks confirmed_flush_lsn.
solid answer
~50 sIn `pg_replication_slots`, every slot pins WAL from `restart_lsn`. A **physical** slot serves a streaming standby; its `xmin` is filled only when the standby runs with `hot_standby_feedback = on`, and then VACUUM on every table in the cluster keeps rows newer than it, even while the standby is disconnected. Its `catalog_xmin` is normally NULL and carries a value only when the standby itself has logical slots. A **logical** slot belongs to one `database` and one output `plugin`; it pins `catalog_xmin`, so VACUUM keeps old system-catalog rows needed to decode with the schema as it was, and it records `confirmed_flush_lsn`, how far the consumer has confirmed. Its `restart_lsn` usually trails `confirmed_flush_lsn` because decoding must re-read transactions that were still open. So a stalled logical slot means WAL growth plus catalog bloat; a stalled physical slot with feedback means WAL growth plus table bloat.
code
sql · 9 linesSELECT slot_name,
slot_type,
database,
active,
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) AS retained_wal,
age(xmin) AS xmin_age,
age(catalog_xmin) AS catalog_xmin_age
FROM pg_replication_slots
ORDER BY pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) DESC NULLS LAST;go deeper
Remember that physical slots feed standbys and logical slots feed decoding consumers, and that both keep WAL from restart_lsn.
Explain which columns each type fills: xmin from hot_standby_feedback, catalog_xmin and confirmed_flush_lsn for logical slots, and why restart_lsn trails the confirmed point.
Read pg_replication_slots during a bloat or disk investigation: retained bytes, age of xmin and catalog_xmin, and which kind of bloat each stalled slot produces.
Weigh hot_standby_feedback through a slot: it protects standby queries indefinitely but lets one replica's long query or outage bloat every table on the primary.
## Two kinds of thing a slot can pin A PostgreSQL **replication slot** protects its consumer from two kinds of cleanup on the primary: - **WAL removal**: checkpoints remove or recycle `pg_wal` segments, and a slot stops that at its `restart_lsn`. - **Row removal**: VACUUM removes dead row versions older than the cluster's oldest needed transaction, and a slot can hold that horizon back with `xmin` or `catalog_xmin`. Which of these a slot pins depends on its type, shown in the `slot_type` column of `pg_replication_slots` as `physical` or `logical`. ## Physical slots A **physical slot** serves a streaming standby or another WAL receiver (such as `pg_receivewal`). It has no `database` and no `plugin`. - `restart_lsn` is the oldest WAL the standby may still need. - `xmin` is NULL unless the standby sends **hot-standby feedback**. With `hot_standby_feedback = on`, the standby reports its oldest snapshot and the walsender stores it in the slot. VACUUM then keeps dead rows newer than that `xmin` in **every table of every database**, because a physical standby replays the whole cluster. Because the value lives in the slot, the protection continues while the standby is disconnected. - `catalog_xmin` is normally NULL. It carries a value when the standby has logical slots of its own and reports their catalog horizon through the same feedback. ## Logical slots A **logical slot** serves logical decoding: a subscription, `pg_recvlogical`, or a change-data-capture tool. It belongs to one `database` and names its output `plugin`. - `catalog_xmin` is the oldest transaction whose **system-catalog** rows the slot still needs. Decoding a change requires the table definition as it was when the change was written, so VACUUM must keep old rows of catalogs such as `pg_class` and `pg_attribute`. - `confirmed_flush_lsn` is how far the consumer has confirmed receiving data; it is NULL for physical slots. - `restart_lsn` is usually **older** than `confirmed_flush_lsn`: to decode a transaction that was still open at the confirmed point, the decoder must re-read WAL from where that transaction began. - `xmin` is NULL in steady state; a logical slot holds a data `xmin` only briefly while it builds its initial snapshot. ## Side by side | Column | Physical slot | Logical slot | |---|---|---| | `restart_lsn` | oldest WAL the standby needs | oldest WAL decoding needs, usually before the confirmed point | | `confirmed_flush_lsn` | NULL | how far the consumer confirmed | | `xmin` | set by `hot_standby_feedback`; holds VACUUM on all tables | NULL in steady state | | `catalog_xmin` | NULL unless the standby has logical slots | holds VACUUM on system catalogs | | `database`, `plugin` | NULL | one database, one output plugin | ## What a stalled slot costs 1. **Any stalled slot** grows `pg_wal` from its `restart_lsn`; measure it with `pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)`. 2. **A stalled physical slot with feedback** stops VACUUM from cleaning user tables cluster-wide, so heavily updated tables bloat. 3. **A stalled logical slot** bloats the system catalogs; this shows first on workloads that create and drop many temporary tables or run frequent DDL. An old `catalog_xmin` also holds back freezing; how that ends in wraparound protection is the vacuum-wraparound leaf's subject. The docs say it plainly: slots keep resources even when no connection uses them, so a slot that is no longer required should be dropped. ## Reading the view - `age(xmin)` and `age(catalog_xmin)` show how many transactions the slot holds back. - `active` and `active_pid` show whether a consumer is attached; `inactive_since` (17+) shows since when it has been idle. - Compare a slot's horizon with the other things that hold VACUUM back (long transactions, prepared transactions); the general list belongs to the autovacuum leaf. ## Common misreadings - **"The physical slot has no xmin, so the standby cannot cause bloat."** True only while `hot_standby_feedback` is off. Turn it on and the same slot starts pinning rows in every table. - **"The logical consumer is current because `confirmed_flush_lsn` is recent."** WAL retention is governed by `restart_lsn`, which can stay far behind while one long transaction remains open on the primary. - **"VACUUM VERBOSE says it removed nothing, so the table must be fine."** When a slot holds the horizon, VACUUM reports dead rows it is not yet allowed to remove; the fix is releasing the horizon, not running VACUUM again. - **"A slot only matters while its consumer is connected."** Both the WAL claim and the row horizons persist with the slot.
- Why does an old catalog_xmin cause bloat in pg_attribute but not in the application's own tables?`catalog_xmin` only restrains VACUUM on system catalogs (and tables declared `user_catalog_table`), because decoding needs historic table definitions, not historic user data. The decoded changes come from WAL, so user-table rows can be cleaned normally. Workloads that create many temporary tables churn `pg_class` and `pg_attribute`, so those catalogs bloat first behind a stalled logical slot.
- If you turn hot_standby_feedback off on the standby, what happens to the physical slot's xmin?The standby's next feedback message carries no valid xmin, and the walsender clears the slot's `xmin`, releasing the VACUUM horizon on the primary. The standby's long queries then risk cancellation by recovery conflicts again; that trade-off belongs to the streaming-standby leaf.
- A physical slot always holds back VACUUM, even without hot_standby_feedback.
- A logical slot's catalog_xmin stops VACUUM on the application's own tables.
- restart_lsn and confirmed_flush_lsn are always the same LSN on a logical slot.
- Only logical slots retain WAL; physical slots just record a position.
- A slot's xmin stops protecting rows as soon as its consumer disconnects.