当前位置: 首页 > 工具软件 > pg_rewind > 使用案例 >

PostgreSql pg_rewind

郑博
2023-12-01

一、概述

pg_rewind 是 postgresql 主从数据库之同步数据目录的工具。
pg_rewind 相比 pg_basebackup 和 rsync 这样的工具来说,优势是它不需要从源目录拷贝所有的数据文件,而是会对比时间线发生偏离的点,只拷贝变化过的文件,这样对于数据量很大的情况下速度更快。
pg_rewind 使用前提:需要目标服务器在 postgresql.conf 中允许 wal_log_hints(默认 off),或者在 initdb 初始化数据库时启用数据校验(–data-checksums),full_page_writes 也必须为 on(默认 on)。

二、语法

pg_rewind [option...] { -D | --target-pgdata } directory { --source-pgdata=directory | --source-server=connstr } 

参数说明:

-D directory 或 --target-pgdata=directory:此选项指定与源同步的目标数据目录。在运行pg_rewind之前,必须干净关闭目标服务器。
–source-pgdata=directory:指定要与之同步的源服务器的数据目录的文件系统路径。此选项要求干净关闭源服务器。
–source-server=connstr:指定要连接到源 PostgreSQL 服务器的 libpq 连接字符串。连接必须是具有超级用户访问权限的正常(非复制)连接。此选项要求源服务器正在运行,而不是处于恢复模式。
-R 或 --write-recovery-conf:创建standby.signal并将连接设置附加到输出目录中的 postgresql.auto.conf 中。–source-server对于此选项是必需的。
-n 或 --dry-run:除了实际修改目标目录之外,执行所有操作。
-N 或 --no-sync:默认情况下,pg_rewind 将等待所有文件安全地写入磁盘。 此选项会导致 pg_rewind 不等待即可返回,这更快,但意味着后续操作系统崩溃会使同步数据目录损坏。通常情况,此选项可用于测试,但不应使用于生产安装。
-P 或 --progress:启用进度报告。在从源集簇拷贝数据时,打开这个选项将会发送一个近似的进度报告。
-c 或 --restore-target-wal:如果在 pg_wal 目录中不再可用这些文件,请使用在目标群集配置中定义的 restore_command 从WAL存档中检索WAL文件。
–debug:打印冗长的调试输出,这主要对于调试pg_rewind的开发者有用。
–no-ensure-shutdown:pg_rewind 要求目标服务器在重放之前彻底关闭。 默认情况下,如果目标服务器没有完全关闭,pg_rewind 会以单用户模式启动目标服务器,先完成崩溃恢复,然后将其停止。 通过传递这个选项,如果服务器没有完全关闭,pg_rewind 会跳过这个并立即出错。 在这种情况下,用户应该自己处理这种情况。
-V --version:显示版本信息然后退出。

三、示例

1. 环境

CentOS 7.8 + HGDB v4.5.7 安全版
192.168.100.11  主
192.168.100.12  备

2. 构建测试数据

--主从(11,12),确保两参数均为 on
show wal_log_hints;
show full_page_writes;

alter system set wal_log_hints = 'on';
pg_ctl restart

--主(11)
create table test(id int,name varchar(20));
insert into test values(1,'aa'),(2,'bb'),(3,'cc');

--主从(11,12)
select * from test;

3.模拟主从时间线偏离

1)提升从库为主库

--从(12)
pg_ctl promote
select pg_is_in_recovery();

--主(11)
select pid,state,client_addr,sync_priority,sync_state from pg_stat_replication;

2)原主库插入数据

--原主(11)
insert into test values(4,'dd');
select * from test;

3)原从库插入数据

--原从(12)
insert into test values(5,'ee');
select * from test;

4)将原主库调为从库

--原主(11)
vi /opt/HighGo4.5.7-see/data/postgresql.auto.conf

standby_mode = 'on'    --12之后参数废弃,创建标识文件替代 touch $PGDATA/standby.signal
primary_conninfo = 'user=sysdba password=Hello@123 host=192.168.100.12 port=5866'

--重启数据库后观察原主日志,可发现 wal 时间线已发生偏离(11)
pg_ctl restart
select pg_is_in_recovery();

tail -200f /opt/HighGo4.5.7-see/data/hgdb_log/highgodb_09.csv

streaming: ERROR:  requested starting point 0/9000000 on timeline 3 is not in this server's history
DETAIL:  This server's history forked from timeline 3 at 0/8034C50.",,,,,,,,,""
2022-03-09 15:42:44.256 CST,,,25847,,62285a74.64f7,5,,2022-03-09 15:42:44 CST,1/0,0,LOG,00000,"new timeline 4 forked off current database system timeline 3 before current recovery point 0/90000A0",,,,,,,,,""

--原备库可查询发生时间线偏离的 wal 文件(12)
select pg_xlogfile_name_offset('0/90000A0');  --10之前
select pg_walfile_name_offset('0/90000A0');

4. 使用 pg_rewind 同步时间线

--原主(11)
pg_ctl stop
pg_rewind --target-pgdata /opt/HighGo4.5.7-see/data/ --source-server='host=192.168.100.12 port=5866 user=sysdba dbname=highgo password=Hello@123'
#若报错缺少 wal 文件,从归档路径或另外节点拷贝 cp /highgo/hgdbbak/archive/000000030000000000000008 /opt/HighGo4.5.7-see/data/pg_wal/

vi /opt/HighGo4.5.7-see/data/postgresql.auto.conf

standby_mode = 'on'    --12之后参数废弃,创建标识文件替代 touch $PGDATA/standby.signal
primary_conninfo = 'user=sysdba password=Hello@123 host=192.168.100.12 port=5866'

5. 启动数据库验证同步

--原主(11)
pg_ctl start
select * from test;

--原从(12)
select * from test;
select pid,state,client_addr,sync_priority,sync_state from pg_stat_replication;
 类似资料:

相关阅读

相关文章

相关问答