PostgreSQL数据库关于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的使用

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

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

shell 复制代码
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生效需要重启数据库

shell 复制代码
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,重新启动并关闭一次目标库(此时主库是启动状态,备库是关闭状态)

shell 复制代码
vi $dest/postgres.conf

wal_log_hints = on

3.pg_rewind命令执行

shell 复制代码
[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过来

shell 复制代码
[pg@lzl pg96data_pri]$ mv recovery.done recovery.conf
[pg@lzl pg96data_pri]$ vi recovery.conf
[pg@lzl pg96data_pri]$ vi postgres.conf

5.启动备库

shell 复制代码
[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命令报错一

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

解决办法:

使用高权限用户

shell 复制代码
postgres=# \du
                                    List of roles
  Role name  |                         Attributes                         | Member of 
-------------+------------------------------------------------------------+-----------
 lzl         | Replication                                                | {}
 pg          | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 rewind_user |                                                            | {}

pg用户是pg server自带的超级用户,跟pg安装用户相同。os的安装用户肯定有修改pg_control的权限

shell 复制代码
/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'

pg_rewind命令报错二

shell 复制代码
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_hba,例如

shell 复制代码
host    all       pg         172.17.100.150/32          trust

pg_rewind命令报错三

shell 复制代码
[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,启动再关闭一次目标库(目标库本来就是关闭的,必须启动再关闭一次,不然参数不会生效)
shell 复制代码
vi postgres.conf 加入目标库配置

wal_log_hints = on

重启目标库以生效

shell 复制代码
[pg@lzl pg96data_sla]$  /pg/pg96/bin/pg_ctl -D /pg/pg96data_pri -l /pg/pg96data_pri/server.log start      
server starting
[pg@lzl pg96data_sla]$  /pg/pg96/bin/pg_ctl -D /pg/pg96data_pri -l /pg/pg96data_pri/server.log stop
waiting for server to shut down.... done

参考文档:

https://www.postgresql.org/docs/9.6/app-pgrewind.html

相关推荐
武子康4 小时前
Java-159 MongoDB 副本集容器化 10 分钟速查卡|keyfile + –auth + 幂等 init 附 docker-compose
java·数据库·mongodb·docker·性能优化·nosql·1024程序员节
zz-zjx4 小时前
MySQL 索引深度指南:原理 · 实践 · 运维(适配 MySQL 8.4 LTS)
运维·数据库·mysql
摇滚侠4 小时前
全面掌握PostgreSQL关系型数据库,设置远程连接,笔记05,笔记06
java·数据库·笔记·postgresql
mpHH4 小时前
postgresql plancache --doing
数据库·学习·postgresql·1024程序员节
布朗克1684 小时前
MySQL 运算符详细说明
数据库·mysql·运算符·1024程序员节
机灵猫5 小时前
微服务中的服务熔断、降级与限流
java·数据库·微服务
枫叶丹45 小时前
破局政务数字化核心难题:金仓数据库以国产化方案引领电子证照系统升级之路
数据库·政务·1024程序员节·金仓
托比-马奎尔5 小时前
Redis主从集群
数据库·redis·缓存
码力引擎6 小时前
【零基础学MySQL】第一章:MySQL介绍与安装
数据库·mysql·1024程序员节