确保 PostgreSQL 9.x 热备份复制仍然正常运行的最便捷方法是什么

确保 PostgreSQL 9.x 热备份复制仍然正常运行的最便捷方法是什么

假设我得到了它热服务器复制一切设置好后,我可以定期采取哪些最简单或最方便的步骤来确保热服务器复制仍然正常运行。

我想过在两台服务器上进行比较。如果 2匹配,SELECT txid_current_snapshot();那么是否可以假设复制工作正常,这样是否足够好且足够安全?txid

答案1

PostgreSQL 的 wiki 上有此信息流复制页面

您可以通过比较主服务器上的当前 WAL 写入位置与备用服务器接收/重放的最后一个 WAL 位置来计算复制滞后。可以使用以下方法检索它们 pg_current_xlog_location在小学和 pg_last_xlog_receive_location / pg_last_xlog_replay_location处于待机状态。

在 PostgreSQL 版本 10 中,这些函数已重命名为 pg_current_wal_lsnpg_last_wal_receive_lsnpg_last_wal_replay_lsn,因此上述名称现在仅对 9.6 及更早版本有效。

这些函数返回一个字符串,格式id/offsetidoffset是十六进制数。为了进行比较,你可以使用这个公式将它们转换为 64 位数字,该公式取自检查postgresNagios 插件:

0xff000000 * from_hex(id) + from_hex(offset)

在 bash 脚本中,可以使用以下函数完成:

xlog_location_to_64bits()
{
  id="${1%%/*}"
  offset="${1##*/}"
  echo $((0xFF000000 * 0x$id + 0x$offset))
}

用主服务器上的值减去从服务器上的值即可得出复制滞后(以字节为单位)。

答案2

对我来说,最方便的方法是本页第 12 步

# The displayed LSNs indicate the byte position that the standby server has
# written up to in the xlogs.
[primary] $ ps -ef | grep sender
postgres  6879  6831  0 10:31 ?        00:00:00 postgres: wal sender process postgres    127.0.0.1(44663) streaming 0/2000000

[standby] $ ps -ef | grep receiver
postgres  6878  6872  1 10:31 ?        00:00:01 postgres: wal receiver process   streaming 0/2000000

相关内容