跳过正文
  1. 文章/

pg_rewind初识

·1485 字·3 分钟
liuzhilong62
作者
liuzhilong62
PostgreSQL DBA,关注数据库内核、案例分析、源码解读
C M

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

vi $dest/postgres.conf

wal_log_hints = 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

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

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的权限

/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命令报错二
#

 

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,例如

host    all       pg         172.17.100.150/32          trust

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 加入目标库配置

wal_log_hints = on

重启目标库以生效

[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

相关文章

ogg搭建oracle-pg同步——实操步骤

·3072 字·7 分钟
​ 源库: oracle(11.2.0.4) 192.168.10.141 目标库:pgsql(10.12) 192.168.10.128 ogg软件版本:(19.1.0.0.4) ogg软件下载:Oracle GoldenGate Downloads glibc问题处理:https://www.cnblogs.com/hxlasky/p/16779047.html

ogg搭建pg-oracle同步——实操步骤

·1885 字·4 分钟
ogg软件版本:(19.1.0.0.4) oracle版本:11.2.0.4 pg版本:pg10 ogg下载地址:https://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html glibc问题处理:https://www.cnblogs.com/hxlasky/p/16779047.html 1.源端创建库及表 # [root@node2 ~]# su - postgres Last login: Tue Jul 21 21:08:52 CST 2020 on pts/0 [postgres@node2 ~]$ pg_ctl -D /opt/pgsql_data -l logfile start waiting for server to start.... done server started postgres=# create database test postgres=# \c lzldb postgres=# create table tab1(id int primary key,name varchar(20)) 2.目标端创建库及表 # sqlplus / as sysdba SQL> create table ORALZL.tab1(id number primary key,name varchar2(20)); 3.解压安装ogg for postgresql # --跟ogg for oracle不同,ogg for pg只需要解压。for o需要跑runinstaller [postgres@node1 ~]$ id postgres uid=54323(postgres) gid=54330(postgres) groups=54330(postgres) [postgres@node1 ~]$ exit logout [root@node1 ~]# mkdir /ogg [root@node1 ~]# chown -R postgres /ogg [root@node1 ~]# chmod -R 755 /ogg [root@node1 ~]# [root@node1 soft]# ls -l total 240744 -rw-r--r--. 1 root root 87028695 Jul 22 02:51 19100200714_ggs_Linux_x64_PostgreSQL_64bit.zip [root@node1 soft]# chmod 777 19100200714_ggs_Linux_x64_PostgreSQL_64bit.zip [root@node1 soft]# unzip 19100200714_ggs_Linux_x64_PostgreSQL_64bit.zip Archive: 19100200714_ggs_Linux_x64_PostgreSQL_64bit.zip inflating: ggs_Linux_x64_PostgreSQL_64bit.tar inflating: OGG-19.1.0.0-README.txt inflating: release-notes-oracle-goldengate_19.1.0.200714.pdf [root@node1 soft]# chmod 777 ggs_Linux_x64_PostgreSQL_64bit.tar [root@node1 soft]# su - postgres [postgres@node1 ~]$ cd /soft [postgres@node1 soft]$ tar -xf ggs_Linux_x64_PostgreSQL_64bit.tar -C /ogg 4.pg用户环境变量配置 # pg源端

我是如何考上武汉大学在职研究生的

·8356 字·17 分钟
我为什么要考在职研究生? # 提升学历。我的学历是一个普通的本科学校。学历高一点的话在以后的职业生涯会稍微增加一点竞争力 之前投递过一家国企,简历石沉大海。但是同办公室的一个学历较高的同事就通过了,所以如果是国企单位,高学历就是敲门砖 弥补大四考研失败的遗憾,重拾研究生的梦想 学习总不会有错,这是我的信仰 全日制研究生和在职研究生的区别 # 学习状态 # 全日制是脱产的,在职是可以工作的。这条基本定死了大部分打工人只能考非全