所以,最后我自己找到了解决方案。到目前为止,postgres 将所有更改存储为特殊的日志文件,然后将更改复制到从服务器上。同时 postgres 拥有丰富的 API 方法,可以读取不同内部进程的状态。所以最后我使用这个查询:
-- On MASTER server current xlog location:
SELECT
pg_xlog_location_diff(pg_current_xlog_location(), '0/0') AS offset;
-- On SLAVE server current offset of received and applied changes
SELECT
pg_xlog_location_diff(pg_last_xlog_receive_location(), '0/0') AS receive,
pg_xlog_location_diff(pg_last_xlog_replay_location(), '0/0') AS replay;
从 MASTER 和 SLAVE 获取这些值我可以确定服务器之间的差异有多大。当我需要确保两台服务器处于相同状态时,我可以等到 SLAVE 上的偏移量等于或大于 MASTER 上的偏移量。
如果您有同样的问题,这个查询可能也很方便:
-- Check is current server in cluster
SELECT
pg_is_in_recovery() AS recovery;
-- False — server is MASTER
-- True — server is SLAVE