Postgres:如何安全地删除副本?

Postgres: How do I safely remove a replica?

如果我永久删除其唯一的副本,是否需要在主服务器上执行任何操作?我担心 WAL 文件会填满磁盘。

我想从单节点复制设置中删除唯一的副本:

P -> R

我想删除 R.

你的担心是完全正确的。副本在主服务器上创建 replication slot,其中存储 restart_lsn。根据文档,restart_lsn 是:

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.

如果副本没有在这个复制槽中推进 LSN,主将保留从这个位置开始的所有 WAL 段并忽略 max_wal_size 限制。

如果要删除副本并启用 WAL 轮换,则还必须删除复制槽:

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

有一个 patch on Postgres Commitfest,它引入了新的 GUC 来限制复制槽持有的 WAL 数量。但是,补丁是长期存在的,但仍未提交。