pg_receivewal实践

https://www.cnblogs.com/kuang17/p/10868556.html

测试从pg_receivewal的日志中恢复从库为主库:

主从配置async模式,配置pg_receivewal接收日志pg_receivewal -D /dbaas/pg/data/pg_receivewal_data -v -h 10.9.10.202

主插入1000万数据,当插入一半时,停止从库

主库插完数据,停止

将pg_receivewal的日志拷贝到从库/dbaas/pg/data/pg_receivewal_data下

修改从库recovery.conf文件,添加内容:

restore_command = 'cp /dbaas/pg/data/pg_receivewal_data/%f "%p"'

启动数据库,数据库成功恢复到10000000行。

另外一种用法:

restore_command = 'cp /dbaas/pg/data/pg_receivewal_data/%f "%p"'

standby_mode = on

primary_conninfo = 'host=10.9.10.203 port=5432 user=postgres connect_timeout=60'

recovery_target_timeline = 'latest'

trigger_file = '/dbaas/pg/data/.tfile'

这种把恢复和备库设置放一起,会先去从归档恢复,当恢复完成后,直接建立流复制关系,这样就相当方便了,当有归档时,不用重做从库。

测试pg_receivewal的同步机制:

-S slotname

--slot=slotname

Require pg_receivewal to use an existing replication slot (see Section 26.2.6). When this option is used, pg_receivewal will report a flush position to the server, indicating when each segment has been synchronized to disk so that the server can remove that segment if it is not otherwise needed.

When the replication client of pg_receivewal is configured on the server as a synchronous standby, then using a replication slot will report the flush position to the server, but only when a WAL file is closed. Therefore, that configuration will cause transactions on the primary to wait for a long time and effectively not work satisfactorily. The option --synchronous (see below) must be specified in addition to make this work correctly.

--synchronous

Flush the WAL data to disk immediately after it has been received. Also send a status packet back to the server immediately after flushing, regardless of --status-interval.

This option should be specified if the replication client of pg_receivewal is configured on the server as a synchronous standby, to ensure that timely feedback is sent to the server.

在这个说明中,使用了--slot=for_pgreceivewal --synchronous后,理论上是有点像sync同步一样,数据库会等待接收端进行回放,回放结束,再进行commit。

1)使slotsynchronous参数:

数据库端:

修改postgresql.conf参数synchronous_standby_name=‘pg_receivewal’

重启数据库

postgres=# select pg_create_physical_replication_slot('for_pgreceivewal');

pg_create_physical_replication_slot

-------------------------------------

(for_pgreceivewal,)

(1 row)

postgres=# select pg_get_replication_slots();

pg_get_replication_slots

----------------------------------------

(for_pgreceivewal,,physical,,f,f,,,,,)

(1 row)

postgres=# select pg_get_replication_slots();

pg_get_replication_slots

--------------------------------------------------------

(for_pgreceivewal,,physical,,f,t,629488,,,0/9DD77B00,)

(1 row)

接收端:

pg_receivewal -D /dbaas/pg/data/pg_receivewal_data --slot=for_pgreceivewal --synchronous -v -h 10.9.10.202

数据库端:

postgres=# 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

--------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+------------+------------+------------+------------+-----------------+-----------------+-----------------+---------------+------------

912703 |       10 | postgres | pg_receivewal    | 10.9.10.203 |                 |       63103 | 2019-05-15 02:52:30.029814+00 |              | streaming | 0/D9E44528 | 0/D9E44528 | 0/D9E44528 |            | 00:00:00.010972 | 00:00:00.010972 | 00:06:49.964729 |             1 | sync

(1 row)

在数据库端,使用pgbench测试:

[postgres@90027282-dd74-4642-b5dd-359b6cc67aaf /dbaas/pg/data]$ pgbench -c 10 -j 5 -r -T 100 pgbench

starting vacuum...end.

transaction type: <builtin: TPC-B (sort of)>

scaling factor: 100

query mode: simple

number of clients: 10

number of threads: 5

duration: 100 s

number of transactions actually processed: 15290

latency average = 65.426 ms

tps = 152.845128 (including connections establishing)

tps = 152.855112 (excluding connections establishing)

statement latencies in milliseconds:

0.002  \set aid random(1, 100000 * :scale)

0.001  \set bid random(1, 1 * :scale)

0.001  \set tid random(1, 10 * :scale)

0.000  \set delta random(-5000, 5000)

0.046  BEGIN;

0.300  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;

0.111  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;

0.417  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;

2.878  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;

0.092  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);

61.539  END;

2)不使用slotsynchronous参数

数据库端,删除slot

postgres=# select pg_drop_replication_slot('for_pgreceivewal');

pg_drop_replication_slot

--------------------------

(1 row)

postgres=# select pg_get_replication_slots();

pg_get_replication_slots

--------------------------

(0 rows)

接收端:

pg_receivewal -D /dbaas/pg/data/pg_receivewal_data  -v -h 10.9.10.202

压测:

[postgres@90027282-dd74-4642-b5dd-359b6cc67aaf ~]$ pgbench -c 10 -j 5 -r -T 100 pgbench

starting vacuum...end.

transaction type: <builtin: TPC-B (sort of)>

scaling factor: 100

query mode: simple

number of clients: 10

number of threads: 5

duration: 100 s

number of transactions actually processed: 27015

latency average = 37.021 ms

tps = 270.120226 (including connections establishing)

tps = 270.138206 (excluding connections establishing)

statement latencies in milliseconds:

0.002  \set aid random(1, 100000 * :scale)

0.001  \set bid random(1, 1 * :scale)

0.001  \set tid random(1, 10 * :scale)

0.000  \set delta random(-5000, 5000)

0.042  BEGIN;

0.242  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;

0.110  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;

0.291  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;

1.778  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;

0.091  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);

34.396  END;

同步模式下,停止pg_receivewal,会导致主hang住:

postgres=# create table test(id int, info varchar(8), crt_time timestamp);

^CCancel request sent

WARNING:  canceling wait for synchronous replication due to user request

DETAIL:  The transaction has already committed locally, but might not have been replicated to the standby.

CREATE TABLE

pg_receivewal的使用场景思考:

1)当配置高可用集群时,一个备库很危险,容易出现单点故障,当同步节点死掉后,主节点会hang住(这是没有专业高可用工具的情况下,ecox这种高可用工具,会自动处理为单节点可写)。而部署两个同步节点又会浪费资源(不做读写分离的情况下),这个时候,可以创建一个pg_receivewal节点,使用同步模式,当一个同步节点停止后,它可以充当sync节点,给主节点报告LSN。

为了减少网络开销,还可以将pg_receivewal节点设置为主库本地,即起到了归档作用,又起到了同步节点的功能。唯一注意的是,要定期清理日志空间。

2)第二种情况,数据库集群不使用sync模式,想使用更加高性能的async模式。这时,使用pg_receivewal来做日志归档,至少比aync节点同步的日志要多。这样基于他的恢复,也将丢更少的数据。当数据库更新插入极端的大时,pg_receivewal归档的日志,可能还没有归档命令归档的多。归档是到本地,如果服务器挂了,那么归档也没有了,可以使用pgbackrest或者rync工具将归档同步到其他服务器。

用更加精炼的话说:

1)避免单点故障。这点比较鸡肋,某些高级的高可用工具本身就能解决单点故障。

2)用来做日志归档,归档到块级别,且能归档到远端。比archive_command要更加多(默认归档是完成一个wal文件才归档一个,还未写满的wal是不会归档的)。主从之间保持sync模式,那没必要用pg_receivewal来归档;如果是async模式追求性能,pg_receivewal也不能配成同步来影响性能啊(让步:将pg_receivewal放到本地,这样减少网络传输开销,保持同步,能否接受,答案是同步下性能还是很差,而且差距很大,下面是测试数据)。

1

2

3

4

5

6

7

8

postgres=# 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

--------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+--------------+-----------+------------+------------+------------+------------+-----------------+-----------------+-----------------+---------------+------------

 745643 |       10 | <strong>postgres | pg_receivewal    | ::1 </strong>        |                 |       58465 | 2019-05-16 03:53:06.628954+00 |              | streaming | 1/CB059FC8 | 1/CB059FC8 | 1/CB059FC8 |            | 00:00:00.191089 | 00:00:00.191089 | 00:00:32.187898 |             1 | <strong>sync</strong>

 668818 |       10 | postgres | walreceiver      | 10.9.10.203 |                 |       42007 | 2019-05-16 03:37:21.983067+00 |              | streaming | 1/CB059FC8 | 1/CB059FC8 | 1/CB059FC8 | 1/CB059FC8 |                 |                 |                 |             0 | async

(2 rows)

(END)

在本地同步性能:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[postgres@90027282-dd74-4642-b5dd-359b6cc67aaf /dbaas/pg]$ pgbench -c 10 -j 5 -r -T 100 pgbench

starting vacuum...end.

transaction type: <builtin: TPC-B (sort of)>

scaling factor: 100

query mode: simple

number of clients: 10

number of threads: 5

duration: 100 s

number of transactions actually processed: 17313

latency average = 57.785 ms

tps = 173.054591 (including connections establishing)

tps = 173.066373 (excluding connections establishing)

statement latencies in milliseconds:

         0.002  \set aid random(1, 100000 * :scale)

         0.001  \set bid random(1, 1 * :scale)

         0.001  \set tid random(1, 10 * :scale)

         0.000  \set delta random(-5000, 5000)

         0.048  BEGIN;

         0.268  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;

         0.111  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;

         0.457  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;

         2.694  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;

         0.092  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);

        53.995  END;

异地同步性能更低:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[postgres@90027282-dd74-4642-b5dd-359b6cc67aaf /dbaas/pg]$ pgbench -c 10 -j 5 -r -T 100 pgbench

starting vacuum...end.

transaction type: <builtin: TPC-B (sort of)>

scaling factor: 100

query mode: simple

number of clients: 10

number of threads: 5

duration: 100 s

number of transactions actually processed: 14289

latency average = 70.130 ms

tps = 142.591560 (including connections establishing)

tps = 142.601721 (excluding connections establishing)

statement latencies in milliseconds:

         0.002  \set aid random(1, 100000 * :scale)

         0.001  \set bid random(1, 1 * :scale)

         0.001  \set tid random(1, 10 * :scale)

         0.000  \set delta random(-5000, 5000)

         0.045  BEGIN;

         0.244  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;

         0.111  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;

         0.388  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;

         2.975  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;

         0.091  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);

        65.978  END;

异地异步,性能高很多:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[postgres@90027282-dd74-4642-b5dd-359b6cc67aaf /dbaas/pg]$ pgbench -c 10 -j 5 -r -T 100 pgbench

starting vacuum...end.

transaction type: <builtin: TPC-B (sort of)>

scaling factor: 100

query mode: simple

number of clients: 10

number of threads: 5

duration: 100 s

number of transactions actually processed: 26333

latency average = 37.987 ms

tps = 263.246388 (including connections establishing)

tps = 263.264004 (excluding connections establishing)

statement latencies in milliseconds:

         0.002  \set aid random(1, 100000 * :scale)

         0.001  \set bid random(1, 1 * :scale)

         0.001  \set tid random(1, 10 * :scale)

         0.000  \set delta random(-5000, 5000)

         0.042  BEGIN;

         0.228  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;

         0.109  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;

         0.310  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;

         1.764  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;

         0.090  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);

        35.349  END;

本地异步呢,更快:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[postgres@90027282-dd74-4642-b5dd-359b6cc67aaf /dbaas/pg]$ pgbench -c 10 -j 5 -r -T 100 pgbench

starting vacuum...end.

transaction type: <builtin: TPC-B (sort of)>

scaling factor: 100

query mode: simple

number of clients: 10

number of threads: 5

duration: 100 s

number of transactions actually processed: 29572

latency average = 33.821 ms

tps = 295.670234 (including connections establishing)

tps = 295.691390 (excluding connections establishing)

statement latencies in milliseconds:

         0.002  \set aid random(1, 100000 * :scale)

         0.001  \set bid random(1, 1 * :scale)

         0.001  \set tid random(1, 10 * :scale)

         0.000  \set delta random(-5000, 5000)

         0.042  BEGIN;

         0.219  UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;

         0.108  SELECT abalance FROM pgbench_accounts WHERE aid = :aid;

         0.253  UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;

         1.592  UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;

         0.091  INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);

        31.412  END;

接下来要思考的场景:

那如果遇到一个落后很久的备库,想不重建的情况下恢复。有归档日志,我们应该怎么做?

1)先从归档恢复,再建立同步?不行,恢复后,日志线就+1了,不能再和主建立同步了。

2)将归档日志,从库缺少的地方开始,拷贝到主库pg_wal下,再进行启动从库。待验证。

3)直接在从库的recovery.conf文件配置如下,先从归档恢复,再恢复流复制关系,这样最靠谱:

restore_command = 'cp /dbaas/pg/data/pg_receivewal_data/%f "%p"'

standby_mode = on

primary_conninfo = 'host=10.9.10.203 port=5432 user=postgres connect_timeout=60'

recovery_target_timeline = 'latest'

trigger_file = '/dbaas/pg/data/.tfile'

严以律己、宽以待人

pg_receivewal实践相关推荐

  1. Golang TDD实践报告:快速排序Quick Sort

    Golang TDD实践报告:快速排序Quick Sort [阅读时间:约5分钟] 0.项目需求 1.编写符合项目输入输出的一个测试 2.尝试运行测试 3.先使用最少的代码来让失败的测试先跑起来 4. ...

  2. 编写高性能Java代码的最佳实践

    编写高性能Java代码的最佳实践 摘要:本文首先介绍了负载测试.基于APM工具的应用程序和服务器监控,随后介绍了编写高性能Java代码的一些最佳实践.最后研究了JVM特定的调优技巧.数据库端的优化和架 ...

  3. 【K8S实践】关于Kubernetes Dashboard的实践学习

    前言: 陆陆续续,学k8s一个月了,从开始看docker容器书,视频,怎么都无法上手:到后来闷着头实践地傻瓜式操作:到今天整个完全串通好!感觉到学习也是一段深刻的成长经历! 过程: 这个过程中使用的工 ...

  4. 提高期——SLA实践指导秘籍

    前言: 第二遍看提高期的SLA文档,开始提高期的第一个项目. 感受: 这个秘籍和自己看的<番茄工作法图解>非常像,写作风格和内容量上. 从大脑的功能分析,为什么要这样做.我们的学习方法符合 ...

  5. Spring Data JPA 五分钟快速入门和实践

    Spring Data JPA(类似于Java Web 中的 DAO) 操作声明持久层的接口(Repository) 三个核心接口: CrudRepository PagingAndSortingRe ...

  6. TVM优化c++部署实践

    TVM优化c++部署实践 使用TVM导入神经网络模型: 模型支持pytorch , tensorflow , onnx, caffe 等.平时pytorch用的多,这里给一种pytorch的导入方式. ...

  7. Dockerfile构建实践

    Dockerfile构建实践 本文介绍了用于构建有效图像的推荐最佳实践和方法. Docker通过从一个Dockerfile文本文件中读取指令来自动构建映像,该文本文件按顺序包含构建给定映像所需的所有命 ...

  8. Tensor基础实践

    Tensor基础实践 飞桨(PaddlePaddle,以下简称Paddle)和其他深度学习框架一样,使用Tensor来表示数据,在神经网络中传递的数据均为Tensor. Tensor可以将其理解为多维 ...

  9. MindSpore模型精度调优实践

    MindSpore模型精度调优实践 引论:在模型的开发过程中,精度达不到预期常常让人头疼.为了帮助用户解决模型调试调优的问题,为MindSpore量身定做了可视化调试调优组件:MindInsight. ...

最新文章

  1. 黑科技:绕过眼睛植入幻觉,科学家成功在盲人脑海中呈现指定图像!
  2. 全球及中国垃圾发电行业运营管理及十四五投资价值评估报告2021-2027年
  3. 随机地址生成器_随机连接神经网络性能超过人工设计!何恺明等人发布新研究...
  4. linux关闭开发者模式的命今,CentOS防火墙用命令行方式关闭的方式
  5. [基础题] 7.第二种(*)按如下要求编写Java程序:
  6. MDK KEIL 烧录STM32下载错误:Flash Timeout.Reset the Target and try it again.解决办法(芯片解锁 解除读报护)
  7. Hdu 1753 大明A+B 高精度小数相加
  8. iOS开发 - Swift实现清除缓存功能
  9. IBM收购以色列应用发现公司EZSource
  10. TAFE的完整形式是什么?
  11. java分数表示_表示Java分数的最佳方法?
  12. JS判断一个数字是否为小数
  13. 【白皮书分享】2020年未来智慧园区白皮书-华为.pdf(附下载链接)
  14. hive高级数据类型
  15. Ofbiz架构讲解与讨论(crud)
  16. 8-18 高可用读写分离
  17. Three.js使用PointerLockControls控制相机实现第一人称视角
  18. 创建一个单选框的html代码是,HTML试题(超有用).doc
  19. android拦截黑名单电话和短信
  20. Python 实现定时任务的八种方案

热门文章

  1. (一)OSG初学者入门基础教程
  2. 连接Mysql提示Can’t connect to local MySQL server through socket的解决方法
  3. 【数据挖掘】关联规则之Galois Closure Based Approach(基于Galois闭包的方法)
  4. UX最佳实践:提高用户体验影响力的艺术
  5. Arcmap制图调色
  6. wimlib-imagex.exe、DISM、WIMGAPI三种方式应用WIM的速度对比
  7. 方舟手游服务器gg修改,【修改贴】关于单机版gg修改器的应用。
  8. oracle命令导入expdp,oracle的导入导出命令:expdp和impdp
  9. 【方向盘】启动命令和IDEA如何传递:VM参数、命令行参数、系统参数、环境变量参数、main方法参数
  10. 如何查看存款和贷款的基准利率