PostgreSQL 之 pg_rewind 详解 (2024)

pg_rewind 是postgresql主丛数据库之同步数据目录的工具。

pg_rewind只复制表数据文件中更改的块;所有其他文件都被完整复制,包括配置文件。pg_rewind相对于使用pg_basebackup备份或rsync等工具的优势在于,pg_rewind不需要读取数据库中未更改的块。这使得在数据库很大且之间只有一小部分块不同的情况下,速度会快得多。

需要目标服务器在postgresql.conf 中允许wal_log_hints,
或者
在 initdb初始化集群时允许 checksums ,full_page_writes也必须为on

[atlasdb@vm1 ~]$ /usr/local/atlasdb/bin/pg_rewind --helppg_rewind resynchronizes a PostgreSQL cluster with another copy of the cluster.Usage: pg_rewind [OPTION]...Options: -D, --target-pgdata=DIRECTORY existing data directory to modify --source-pgdata=DIRECTORY source data directory to synchronize with --source-server=CONNSTR source server to synchronize with -n, --dry-run stop before modifying anything -P, --progress write progress messages --debug write a lot of debug messages -V, --version output version information, then exit -?, --help show this help, then exitReport bugs to <pgsql-bugs@postgresql.org>.

-D directory --target-pgdata=directory
此选项指定与源同步的目标数据目录。在运行pg_rewind之前,必须干净关闭目标服务器

--source-pgdata=directory
指定要与之同步的源服务器的数据目录的文件系统路径。此选项要求干净关闭源服务器

--source-server=connstr
指定要连接到源PostgreSQL服务器的libpq连接字符串。连接必须是具有超级用户访问权限的正常(非复制)连接。此选项要求源服务器正在运行,而不是处于恢复模式

-n --dry-run
除了实际修改目标目录之外,执行所有操作。

-P --progress
输出进展报告。

--debug
输出很多Debug的信息。如果失败时,可以用此选项定位错误原因。

例如:

pg_rewind --target-pgdata=/data/atlasdb --source-server='host=192.168.0.201 port=5432 user=atlasdb password=atlasdb dbname=atlasdb' -P --debug

环境准备
准备两台主机,安装postgresql v11.5。并配置好流复制

  • vm1: 192.168.0.201 主库
  • vm2: 192.168.0.202 从库
    注:我这里用于测试的版本是 AtlasDB v2.7,基于 postgresql v11.5 优化的版本,显示可能不一样。

vm1、vm2环境变量设置:

export PGDATABASE=atlasdbexport PGHOST=localhostexport PGPORT=5432export PGUSER=atlasdbexport PGHOME=/home/atlasdb/atlasdbexport PGDATA=/data/atlasdbexport PATH=$PGHOME/bin:$HOME/bin:$PATHexport LD_LIBRARY_PATH=$PGHOME/lib/:$LD_LIBRARY_PATH

vm1查询流复制情况

[atlasdb@vm1 ~]$ psqlpsql (11.5)PSQL: Release 2.7.0Connected to:AtlasDB V2.7 Enterprise Edition Release - 64-bit ProductionType "help" for help.atlasdb=# select * from pg_stat_replication ; pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn | replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state ------+----------+---------+------------------+---------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+-----------+-----------+------------+-----------+-----------+------------+---------------+------------ 3421 | 10 | atlasdb | walreceiver | 192.168.0.202 | | 33462 | 2019-12-25 18:38:34.168977+08 | | streaming | 0/3000060 | 0/3000060 | 0/3000060 | 0/3000060 | | | | 0 | async(1 row)atlasdb=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- f(1 row)atlasdb=# 

测试过程
主库vm1:创建测试表和测试数据

atlasdb=# create table test_tab(id int,name varchar(20),e_mail varchar(20),d_id int);CREATE TABLEatlasdb=# \dt+ List of relations Schema | Name | Type | Owner | Size | Description --------+----------+-------+---------+---------+------------- public | test_tab | table | atlasdb | 0 bytes | (1 row)atlasdb=# insert into test_tab values(1,'hemny','hemny@qq.com',10);INSERT 0 1atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+-------+--------------+------ 1 | hemny | hemny@qq.com | 10(1 row)atlasdb=# 

从库vm2:查询数据

[atlasdb@vm2 ~]$ psqlpsql (11.5)PSQL: Release 2.7.0Connected to:AtlasDB V2.7 Enterprise Edition Release - 64-bit ProductionType "help" for help.atlasdb=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- t(1 row)atlasdb=# \dt+ List of relations Schema | Name | Type | Owner | Size | Description --------+----------+-------+---------+------------+------------- public | test_tab | table | atlasdb | 8192 bytes | (1 row)atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+-------+--------------+------ 1 | hemny | hemny@qq.com | 10(1 row)atlasdb=#

提升从库vm2为新主库

[atlasdb@vm2 ~]$ pg_ctl promotewaiting for server to promote.... doneserver promoted[atlasdb@vm2 ~]$ psqlpsql (11.5)PSQL: Release 2.7.0Connected to:AtlasDB V2.7 Enterprise Edition Release - 64-bit ProductionType "help" for help.atlasdb=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- f(1 row)atlasdb=# 

旧主库vm1插入一条记录,模拟旧主库上的数据没有复制到新主库上

atlasdb=# insert into test_tab values(2,'hemny_pg1','hemny_pg1@qq.com',10);INSERT 0 1atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+-----------+------------------+------ 1 | hemny | hemny@qq.com | 10 2 | hemny_pg1 | hemny_pg1@qq.com | 10(2 rows)atlasdb=# 

新主库vm2(原从库)插入一条记录

atlasdb=# insert into test_tab values(2,'hemny_pg2','hemny_pg2@qq.com',10);INSERT 0 1atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+-----------+------------------+------ 1 | hemny | hemny@qq.com | 10 2 | hemny_pg2 | hemny_pg2@qq.com | 10(2 rows)atlasdb=# 

将原主库变成新主库的从库

--停止原主库[atlasdb@vm1 ~]$ pg_ctl -D /data/atlasdb -l logfile stopwaiting for server to shut down.... doneserver stopped--配置流复制文件和参数[atlasdb@vm1 ~]$ vi $PGDATA/recovery.conf[atlasdb@vm1 ~]$ cat $PGDATA/recovery.conf standby_mode = 'on'primary_conninfo = 'user=atlasdb password=atlasdb host=192.168.0.202 port=5432 sslmode=prefer sslcompression=0 target_session_attrs=any'[atlasdb@vm1 ~]$ -- 启动数据库[atlasdb@vm1 ~]$ pg_ctl -D /data/atlasdb -l logfile startwaiting for server to start.... doneserver started[atlasdb@vm1 ~]$ psqlpsql (11.5)PSQL: Release 2.7.0Connected to:AtlasDB V2.7 Enterprise Edition Release - 64-bit ProductionType "help" for help.atlasdb=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- t(1 row)atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+-----------+------------------+------ 1 | hemny | hemny@qq.com | 10 2 | hemny_pg1 | hemny_pg1@qq.com | 10(2 rows)

这里可以看到,新主库(vm2)上的记录没有复制过来

vm1的日志信息

[atlasdb@vm1 ~]$ cat logfile 2019-12-25 18:51:16.434 CST [3441] LOG: listening on IPv4 address "0.0.0.0", port 54322019-12-25 18:51:16.434 CST [3441] LOG: listening on IPv6 address "::", port 54322019-12-25 18:51:16.438 CST [3441] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"2019-12-25 18:51:16.453 CST [3442] LOG: database system was shut down at 2019-12-25 18:46:51 CST2019-12-25 18:51:16.453 CST [3442] LOG: entering standby mode2019-12-25 18:51:16.457 CST [3442] LOG: consistent recovery state reached at 0/301BA582019-12-25 18:51:16.457 CST [3442] LOG: invalid record length at 0/301BA58: wanted 24, got 02019-12-25 18:51:16.457 CST [3441] LOG: database system is ready to accept read only connections Customer:'debug user' Begins On:'1970-01-01 08:00:01' Expires On:'273795839-07-29 17:46:41'2019-12-25 18:51:16.492 CST [3446] LOG: fetching timeline history file for timeline 2 from primary server2019-12-25 18:51:16.499 CST [3446] LOG: started streaming WAL from primary at 0/3000000 on timeline 12019-12-25 18:51:16.500 CST [3446] LOG: replication terminated by primary server2019-12-25 18:51:16.500 CST [3446] DETAIL: End of WAL reached on timeline 1 at 0/301B8B8.2019-12-25 18:51:16.502 CST [3446] LOG: restarted WAL streaming at 0/3000000 on timeline 12019-12-25 18:51:16.503 CST [3446] LOG: replication terminated by primary server2019-12-25 18:51:16.503 CST [3446] DETAIL: End of WAL reached on timeline 1 at 0/301B8B8.2019-12-25 18:51:21.509 CST [3446] LOG: restarted WAL streaming at 0/3000000 on timeline 12019-12-25 18:51:21.511 CST [3446] LOG: replication terminated by primary server2019-12-25 18:51:21.511 CST [3446] DETAIL: End of WAL reached on timeline 1 at 0/301B8B8.2019-12-25 18:51:26.515 CST [3446] LOG: restarted WAL streaming at 0/3000000 on timeline 12019-12-25 18:51:26.516 CST [3446] LOG: replication terminated by primary server2019-12-25 18:51:26.516 CST [3446] DETAIL: End of WAL reached on timeline 1 at 0/301B8B8.

vm1上停止新从库

[atlasdb@vm1 ~]$ pg_ctl -D /data/atlasdb -l logfile stopwaiting for server to shut down.... doneserver stopped[atlasdb@vm1 ~]$ 

在vm1上使得pg_rewind 同步数据库时间线

[atlasdb@vm1 ~]$ pg_rewind --target-pgdata=$PGDATA --source-server='host=192.168.0.202 port=5432 user=atlasdb password=atlasdb dbname=atlasdb' -Pconnected to serverservers diverged at WAL location 0/301B8B8 on timeline 1rewinding from last common checkpoint at 0/301B810 on timeline 1reading source file listreading target file listreading WAL in targetneed to copy 52 MB (total source directory size is 76 MB)53300/53300 kB (100%) copiedcreating backup label and updating control filesyncing target data directoryDone![atlasdb@vm1 ~]$ 

注意,pg_rewind之后,需要重新配置流复制文件

[atlasdb@vm1 ~]$ mv $PGDATA/recovery.done $PGDATA/recovery.conf[atlasdb@vm1 ~]$ vi $PGDATA/recovery.conf[atlasdb@vm1 ~]$ cat $PGDATA/recovery.conf standby_mode = 'on'primary_conninfo = 'user=atlasdb password=atlasdb host=192.168.0.202 port=5432 sslmode=prefer sslcompression=0 target_session_attrs=any'[atlasdb@vm1 ~]$ 

注意要修改连接信息

在vm1上启动新的从库

[atlasdb@vm1 ~]$ pg_ctl -D /data/atlasdb -l logfile startwaiting for server to start.... doneserver started[atlasdb@vm1 ~]$ psqlpsql (11.5)PSQL: Release 2.7.0Connected to:AtlasDB V2.7 Enterprise Edition Release - 64-bit ProductionType "help" for help.atlasdb=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- t(1 row)atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+-----------+------------------+------ 1 | hemny | hemny@qq.com | 10 2 | hemny_pg2 | hemny_pg2@qq.com | 10(2 rows)atlasdb=# 

在新主库上插入的记录已同步,原主库没有复制到丛库的记录消失,完成

新主库vm2中

[atlasdb@vm2 ~]$ psqlpsql (11.5)PSQL: Release 2.7.0Connected to:AtlasDB V2.7 Enterprise Edition Release - 64-bit ProductionType "help" for help.atlasdb=# select pg_is_in_recovery(); pg_is_in_recovery ------------------- f(1 row)atlasdb=# select * from pg_stat_replication ; pid | usesysid | usename | application_name | client_addr | client_hostname | client_port | backend_start | backend_xmin | state | sent_lsn | write_lsn | flush_lsn | replay_lsn | write_lag | flush_lag | replay_lag | sync_priority | sync_state ------+----------+---------+------------------+---------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+-----------+-----------+------------+-----------------+----------------+-----------------+---------------+------------ 3399 | 10 | atlasdb | walreceiver | 192.168.0.201 | | 38144 | 2019-12-25 18:59:59.275035+08 | | streaming | 0/30475D8 | 0/30475D8 | 0/30475D8 | 0/30475D8 | 00:00:00.006679 | 00:00:00.00858 | 00:00:00.008581 | 0 | async(1 row)atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+-----------+------------------+------ 1 | hemny | hemny@qq.com | 10 2 | hemny_pg2 | hemny_pg2@qq.com | 10(2 rows)atlasdb=# insert into test_tab values(3,'hemny_pg2_new','hemny_pg2_new@qq.com',10);INSERT 0 1atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+---------------+----------------------+------ 1 | hemny | hemny@qq.com | 10 2 | hemny_pg2 | hemny_pg2@qq.com | 10 3 | hemny_pg2_new | hemny_pg2_new@qq.com | 10(3 rows)atlasdb=# 

新从库vm1中

atlasdb=# select * from test_tab ; id | name | e_mail | d_id ----+---------------+----------------------+------ 1 | hemny | hemny@qq.com | 10 2 | hemny_pg2 | hemny_pg2@qq.com | 10 3 | hemny_pg2_new | hemny_pg2_new@qq.com | 10(3 rows)atlasdb=# 

©

著作权归作者所有,转载或内容合作请联系作者

  • 人面猴

    序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...

    沈念sama阅读 175,490评论 5赞 419

  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...

    沈念sama阅读 74,060评论 2赞 335

  • 救了他两次的神仙让他今天三更去死

    文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...

    开封第一讲书人阅读 124,407评论 0赞 291

  • 道士缉凶录:失踪的卖姜人

    文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...

    开封第一讲书人阅读 47,741评论 0赞 248

  • 港岛之恋(遗憾婚礼)

    正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...

    茶点故事阅读 56,543评论 3赞 329

  • 恶毒庶女顶嫁案:这布局不是一般人想出来的

    文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...

    开封第一讲书人阅读 43,040评论 1赞 246

  • 城市分裂传说

    那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...

    沈念sama阅读 34,107评论 3赞 358

  • 双鸳鸯连环套:你想象不到人心有多黑

    文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...

    开封第一讲书人阅读 32,646评论 0赞 229

  • 万荣杀人案实录

    序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...

    沈念sama阅读 36,694评论 1赞 271

  • 护林员之死

    正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...

    茶点故事阅读 32,398评论 2赞 279

  • 白月光启示录

    正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...

    茶点故事阅读 33,987评论 1赞 288

  • 活死人

    序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...

    沈念sama阅读 30,097评论 3赞 285

  • 日本核电站爆炸内幕

    正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...

    茶点故事阅读 35,298评论 3赞 282

  • 男人毒药:我在死后第九天来索命

    文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...

    开封第一讲书人阅读 27,278评论 0赞 14

  • 一桩弑父案,背后竟有这般阴谋

    文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...

    开封第一讲书人阅读 28,413评论 1赞 232

  • 情欲美人皮

    我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...

    沈念sama阅读 38,397评论 2赞 309

  • 代替公主和亲

    正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...

    茶点故事阅读 38,099评论 2赞 314

推荐阅读更多精彩内容

  • 【转】postgresql 9.4 在linux环境的安装步骤详解

    原文连接:http://www.cnblogs.com/neights/p/5992979.html[http:/...

    mytao4032阅读 1,107评论 0赞 4

  • postgresql搬运

    pg_ctl 名称 pg_ctl -- 启动、停止、重启 PostgreSQL语法 pg_ctl start [-...

    老肖阅读 918评论 0赞 0

  • 本博客的目的在于简述MySQL和PostgreSQL之间如何跨数据库进行复制。涉及跨数据库复制的databases...

    yanzongshuaiDBA阅读 305评论 0赞 4

  • 2019-02-16 亲爱的甲方姑娘

    亲爱的甲方姑娘,见字如面 第一次见你,是参加一场甲乙双方的新项目见面会。 你坐在总监身边,最后一个进来,抱着一本红...

    大渔拓拓阅读 678评论 0赞 51

  • 我最期待的旅行是这样

    我期待这样的一次旅行 可以远离那些让我喘不过气的混凝土怪兽 可以远离那些聒噪不堪的马路高峰 可以远离那些勾心斗角多...

    心安处即归途阅读 546评论 0赞 0

PostgreSQL 之 pg_rewind 详解 (2024)

References

Top Articles
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 5322

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.