pg_replication_slot_advance() 是一个系统函数,用于推进复制槽(物理或逻辑)当前已确认的位置。
pg_replication_slot_advance() 在 PostgreSQL 11 中引入。
用法
pg_replication_slot_advance (slot_namename,upto_lsnpg_lsn) →
record (slot_namename,end_lsnpg_lsn)
该槽不能向后移动,也不能移动到当前插入位置之外。
由于该槽的先前位置而保留的任何 WAL(写前日志)将在下一次 检查点 时被删除。
如果无法推进该槽,则会引发错误。
变更历史
- PostgreSQL 11
- 添加(提交 9c7d06d6)
示例
推进物理复制槽
postgres=# SELECT slot_name, slot_type, active, restart_lsn, confirmed_flush_lsn FROM pg_replication_slots\gx
-[ RECORD 1 ]-------+----------
slot_name | node2
slot_type | physical
active | f
restart_lsn | 0/301C4D8
confirmed_flush_lsn |
postgres=# INSERT INTO foo VALUES (1, clock_timestamp());
INSERT 0 1
postgres=# SELECT * FROM pg_replication_slot_advance('node2', pg_current_wal_lsn());
slot_name | end_lsn
-----------+-----------
node2 | 0/301C5D8
(1 row)
postgres=# SELECT slot_name, slot_type, active, restart_lsn, confirmed_flush_lsn FROM pg_replication_slots\gx
-[ RECORD 1 ]-------+----------
slot_name | node2
slot_type | physical
active | f
restart_lsn | 0/301C5D8
confirmed_flush_lsn |
尝试推进一个因为从未使用或已失效而未保留 WAL 的槽
postgres=# SELECT * FROM pg_replication_slot_advance('physical_slot_1', '0/3002DE0');
ERROR: replication slot "physical_slot_1" cannot be advanced
DETAIL: This slot has never previously reserved WAL, or it has been invalidated.
尝试将复制槽重置到较早的位置
postgres=# SELECT * FROM pg_replication_slot_advance('test_slot_1', '0/301D4E0');
ERROR: cannot advance replication slot to 0/301D4E0, minimum is 0/492C6A8
尝试将复制槽重置到无效的位置
postgres=# SELECT * FROM pg_replication_slot_advance('test_slot_1', '0/0');
ERROR: invalid target WAL LSN
参考资料
- PostgreSQL 文档: 复制管理函数
