Question: How can you get the replication slot size in PostgreSQL?


In PostgreSQL, replication slots are a feature used to ensure that the master server retains WAL segments until they have been received by all standby servers. This is crucial for preventing data loss during replication. However, over time, replication slots can accumulate a significant amount of data, especially if a standby server goes offline for an extended period. Monitoring the size of these slots is important for database health and disk space management.

To get the size of a replication slot in PostgreSQL, you essentially need to check how much WAL data is pending consumption by the replica associated with that slot. Unfortunately, PostgreSQL does not provide a direct function or view that shows the size of the replication slot in terms of disk space. However, you can estimate it by comparing the WAL locations of the server and the replication slot's confirmed flush location.

Here's a step-by-step guide on how to do this:

  1. Log into your PostgreSQL server: Access your PostgreSQL terminal using psql or any other method you prefer.

  2. Identify the current WAL write location: Use the pg_current_wal_lsn() function to find out the latest WAL location.

    SELECT pg_current_wal_lsn();
  3. Get the replication slot’s confirmed flush location: You can find this information by querying the pg_replication_slots view.

    SELECT slot_name, confirmed_flush_lsn FROM pg_replication_slots;
  4. Calculate the difference: By comparing the current WAL location with the replication slot's confirmed flush location, you can estimate the amount of data waiting to be replicated. For this calculation, use the pg_wal_lsn_diff function.

    SELECT slot_name, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) AS replication_lag_size FROM pg_replication_slots;

    This will give you an estimated size of the replication lag for each slot, which indirectly indicates the size of the data retained because of the replication slot.

Note: Keep in mind that these steps provide an estimation rather than an exact byte count of the disk space used. The actual disk space consumed by the WAL files might be slightly different due to internal PostgreSQL optimizations and the way WAL files are managed and recycled.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.