pg_rewind详解

什么是pg_rewind?

  • pg_rewind是pg提供的工具,当2个pg实例时间线(timeline)出现分叉时,pg_rewind可以做实例间的同步。(比如主库运行的情况下,备库failover后运行了一段时间,此时主备的时间线就出现了分叉)
  • pg_rewind会对比两者的大小,然后把大小不一样的文件从源拷贝到目标,包括配置文件。但是它不会对比没有发生改变的文件,所以pg_rewind在比较大的库,更改少量数据时,运行效率较高。
  • pg_rewind可以运用在备库failover后,备库即时运行一段时间,也可以把备库拉到和主库一样的状态,重新成为standby。
  • pg_rewind运行过程中,会对比主(源)备(目标)的差异点,并把主库的差异点后的WAL日志传递给备库。所以,如果主库在差异点之后的WAL也丢失了,那么rewind是不会拷贝不存在的WAL日志的,所以此时备库仍然不会被成功做成standby。解决该问题需要用restore。

!!!在使用pg_rewind时,应备份目标实例。pg_rewind会直接覆盖目标库的文件,如果rewind失败,那么可能目标库无法启动。

 

pg_rewind的使用 

主备切换后,老主库仍然运行,导致主备时间线不一致,老主库无法当做新主库的备库启动

拉起备库时,报时间线错误如下:

LOG:  entering standby mode
FATAL:  requested timeline 2 is not a child of this server's history
DETAIL:  Latest checkpoint is at 0/6000028 on timeline 1, but in the history of the requested timeline, the server forked off from that timeline at 0/4000098.
LOG:  startup process (PID 22321) exited with exit code 1
LOG:  aborting startup due to startup process failure
LOG:  database system is shut down

此时需要用rewind重新拉齐一次主备

 1.配置当前主库的pg_hba
配置pg_rewind的登陆用户登陆源库许可,hba生效需要重启数据库

vi $source/pg_hba.conf
host    all       pg         172.17.100.150/32          trust

pg_rewind需要使用高权限用户,pg新版本可以授权,pg老版本最好用超级用户。
我当前环境的版本为pg9.6,直接使用OS超级用户

2.wal_log_hints = on参数配置
将wal_log_hints = on追加到目标库postgres.conf,重新启动并关闭一次目标库(此时主库是启动状态,备库是关闭状态)

ps:设置wal_log_hints这个参数使得能够记录特定提示位(hint-bit)的变化。这个参数只能在服务器启动的时候被设置。默认值为off。一些特定的工具会要求设置这个参数来正常的运行,所以将它设置为ON就好。

3.pg_rewind命令执行

[pg@lzl pg96data_sla]$ /pg/pg96/bin/pg_rewind 
--target-pgdata /pg/pg96data_pri 
--source-server='host=172.17.100.150 port=5433 user=pg password=oracle  dbname=postgres'

servers diverged at WAL position 0/4000098 on timeline 1
rewinding from last common checkpoint at 0/4000028 on timeline 1
Done!

4.配置备库参数

更改postgres.conf和recovery.conf中的IP、端口、目录等配置,pg_rewind会把配置文件也cp过来
[pg@lzl pg96data_pri]$ mv recovery.done recovery.conf
[pg@lzl pg96data_pri]$ vi recovery.conf
[pg@lzl pg96data_pri]$ vi postgres.conf

5.启动备库

[pg@lzl pg96data_pri]$  /pg/pg96/bin/pg_ctl -D /pg/pg96data_sla -l /pg/pg96data_sla/server.log start        
server starting
[pg@lzl pg96data_sla]$ psql -p5433 postgres
psql (9.6.17)
postgres=# \x
Expanded display is on.
postgres=# select * from  pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid              | 24766
usesysid         | 16384
usename          | lzl
application_name | walreceiver
client_addr      | 172.17.100.150
client_hostname  | 
client_port      | 47345
backend_start    | 2021-07-30 07:44:05.582546+00
backend_xmin     | 
state            | streaming
sent_location    | 0/4033790
write_location   | 0/4033790
flush_location   | 0/4033790
replay_location  | 0/4033790
sync_priority    | 0
sync_state       | async
 

 

常见问题

 pg_rewind命令报错一

could not fetch remote file "global/pg_control": ERROR:  must be superuser to read files
Failure, exiting

解决办法:
使用高权限用户

pg_rewind命令报错二

could not connect to server: FATAL:  no pg_hba.conf entry for host "172.17.100.150", user "rewind_user", database "postgres"
Failure, exiting

没有配置pg_hba.conf连接

pg_rewind命令报错三

[pg@lzl pg96data_sla]$   /pg/pg96/bin/pg_rewind --target-pgdata /pg/pg96data_pri --source-server='host=172.17.100.150 port=5433 user=pg password=oracle  dbname=postgres'
 
target server needs to use either data checksums or "wal_log_hints = on"

问题原因:
1. full_page_writes (默认开启)
2. wal_log_hints 设置成 on 或者 PG 在初始化时开启 checksums 功能 
解决办法:将wal_log_hints = on配置到目标库postgres.conf,启动再关闭一次目标库(目标库本来就是关闭的,必须启动再关闭一次,不然参数不会生效)
vi postgres.conf 加入目标库配置
 

参考文档:
https://www.postgresql.org/docs/9.6/app-pgrewind.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值