Search code examples
postgresqlreplicationdatabase-replication

Postgres: How do I safely remove a replica?


Do I need to do anything on the primary if I permanently remove its only replica? I'm concerned about WAL files filling up the disk.

I want to remove the only replica from a single-node replication setup:

P -> R

I want to remove R.


Solution

  • Your concerns are absolutely correct. Replica creates replication slot on primary server, where restart_lsn is stored. According to the docs, restart_lsn is:

    The address (LSN) of oldest WAL which still might be required by the consumer of this slot and thus won't be automatically removed during checkpoints.

    If replica does not advance LSN in this replication slot, primary will keep all WAL segments starting from this position and ignoring max_wal_size limit.

    If you want to remove replica and enable WAL rotation, then you have to drop replication slot as well:

    postgres=# SELECT * FROM pg_replication_slots;
    postgres=# SELECT pg_drop_replication_slot('replication_slot_name');
    

    There is a patch on Postgres Commitfest, which introduces new GUC to limit a volume of WALs held by replication slot. However, the patch is long-living and still not yet committed.