使用pgBackRest并行归档解决wal堆积问题

作者:温智尹

问题现象:

最近生产上一台postgresql云主机磁盘告警,查看各文件目录大小,发现pg_wal目录竟然占用600G+,数据目录300G。

现有架构:

rds云主机 一主一从
磁盘大小1.2T
数据盘为ssd 归档与备份存储在ks3存储文件上

解决思路:

1.查找wal日志持续不释放原因

首先我们得了解那些参数影响wal日志产生的量与pg_wal目录文件的大小:max_wal_size (integer)
:在自动WAL检查点使得WAL增长到最大尺寸。这是软限制;特殊情况下WAL大小可以超过
max_wal_size,如重负载下,错误archive_command,或者 较大wal_keep_segments的设置。缺省是1GB。
增加这个参数会延长崩溃恢复所需要的时间。 这个参数只能在postgresql.conf文件或者服务器命令行上设置。
wal_keep_segments (integer)
:指定在后备服务器需要为流复制获取日志段文件的情况下,pg_wal目录下所能保留的过去日志文件段的最小数目。每个段通常是 16
兆字节。如果一个连接到发送服务器的后备服务器落后了超过wal_keep_segments个段,发送服务器可以移除一个后备机仍然需要的WAL
段,在这种情况下复制连接将被中断。最终结果是下行连接也将最终失败(不过,如果在使用 WAL 归档,后备服务器可以通过从归档获取段来恢复)。
只设置pg_wal中保留的文件段的最小数目;系统可能需要为 WAL
归档或从一个检查点恢复保留更多段。如果wal_keep_segments为零(默认值), 更多的空间来
存放WAL归档或从一个检查点恢复。如果wal_keep_segments是零(缺省),
系统不会为后备目的保留任何多余的段,因此后备服务器可用的旧 WAL 段的数量是一个上个检查点位置和 WAL
归档状态的函数。这个参数只能在postgresql.conf文件中或在服务器命令行上设置

如上解释,影响wal日志个数的原因主要有以上两个参数,查找该实例相关参数的值

postgres=# show max_wal_size; 
max_wal_size 
-------------- 
10GB 
postgres=# show wal_keep_segments ; 
wal_keep_segments 
------------------- 
600 

综合来看,在数据库正常情况下,pg_wal 目录文件大小应该在10G左右,那么为什么现在会产生600G+的wal,其实上述参数已经给了我们答案:特殊情况下WAL大小可以超过 max_wal_size,如重负载下,错误archive_command,或者 较大wal_keep_segments的设置。于是检查pg_wal/archive_status下文件,发现有大量xxx.ready的文件,表示该wal日志还未及时归档。结合现有架构,归档过程中由于ks3存储文件在性能限制上归档速度远远慢于本地ssd上wal日志产生的速度,所以wal无法及时归档,不断累积,最终造成磁盘告警。 原因找到了,接下来就是解决办法。

2.解决方法
加快wal归档速度

查看归档命令:

archive_command = 'DIR=/ks3data/wal/v3/d5ddd1d7-f458-4c50-ae23-012abc6e0570/723f790f-b66b-4312-bf08-02f8d0f083e5/`date +%F`; sudo test ! -d $DIR && sudo mkdir $DIR; sudo test ! -f $DIR/%f && sudo cp %p $DIR/%f

归档命令串行复制wal日志,由于往s3存储上拷贝,收到性能影响,归档速度远落后与wal产生的速度,于是想到能不能并行归档wal日志。查找相关资料,最终决定测试pgBackRest工具。

pgBackRest
pgBackRest旨在成为一个可靠,易于使用的备份和还原解决方案,通过利用针对特定数据库需求进行了优化的算法,可以无缝地扩展到最大的数据库和工作负载。

产品特点:
1.并行备份和还原
2.本地或远程操作
3.完整,增量和差异备份
4.备份轮换和存档到期
5.备份完整性
6.页面校验和
7.备份简历
8.流压缩和校验和
9.增量还原
10.并行,异步WAL Push&Get
11.表空间和链接支持
12.S3和Azure兼容对象存储支持
13.加密
14.与PostgreSQL > = 8.3的兼容性

相关连接:
https://pgbackrest.org/
https://github.com/pgbackrest/pgbackrest

具体安装使用参考链接文档,这里我们主要使用第10个特性:并行,异步WAL Push&Get
配置pgbackrest参数文件:

[demo] 
pg1-path=/rds/postgresql/ 
pg1-port=5432 
pg1-socket-path=/tmp 
pg1-host-user=postgres 

[global] repo1-path=/ks3data/wal/v3/c2880a97-c981-4962-a25b-568ce07fbe80/7ab5da8d-ef99-4783-85ac-628730ea0124/2021-01-29 
repo1-retention-full=2 
archive-async=y 
log-level-file=detail 
spool-path=/rds/pgbackrest 

[global:archive-push] 
compress-level=3 
process-max= 3 

[global:archive-get] 
process-max=3 
compress-level=3 

其中
archive-async=y 控制异步模式打开,如果该参数关闭,process-max无效,始终为1 process-max=3 表示最大进程数

关闭archive-async效果:

root@vm10-72-87-103 archive_status]# ps -ef | grep arch 
postgres 736 729 0 Jan26 ? 00:00:00 postgres: archiver archiving 0000000100000004000000EE 
postgres 225852 736 0 21:31 ? 00:00:00 sh -c DIR=/ks3data/binlogs/v3/c2880a97-c981-4962-a25b-568ce07fbe80/7ab5da8d-ef99-4783-85ac-628730ea0124/`date +%F`; sudo test ! -d $DIR && sudo mkdir $DIR; sudo test ! -f $DIR/0000000100000004000000EE && sudo pgbackrest --stanza=demo archive-push pg_wal/0000000100000004000000EE 
root 225867 225852 0 21:31 ? 00:00:00 sudo pgbackrest --stanza=demo archive-push pg_wal/0000000100000004000000EE 
root 225868 225867 9 21:31 ? 00:00:00 pgbackrest --stanza=demo archive-push pg_wal/0000000100000004000000EE root 225877 217995 0 21:31 pts/1 00:00:00 grep arch 

打开archive-async:

[root@vm10-72-87-103 pgbackrest]# ps -ef | grep arch 
postgres 736 729 0 Jan26 ? 00:00:00 postgres: archiver archiving 000000010000000500000043 
root 227107 1 0 21:32 ? 00:00:00 pgbackrest --log-level-console=off --log-level-stderr=off --stanza=demo archive-push:async /rds/postgresql/pg_wal 
root 227108 227107 7 21:32 ? 00:00:07 pgbackrest --host-id=1 --log-level-console=off --log-level-file=off --log-level-stderr=error --process=1 --remote-type=repo --stanza=demo archive-push:local 
root 227109 227107 7 21:32 ? 00:00:07 pgbackrest --host-id=1 --log-level-console=off --log-level-file=off --log-level-stderr=error --process=2 --remote-type=repo --stanza=demo archive-push:local 
root 227110 227107 7 21:32 ? 00:00:07 pgbackrest --host-id=1 --log-level-console=off --log-level-file=off --log-level-stderr=error --process=3 --remote-type=repo --stanza=demo archive-push:local 
root 228624 217995 0 21:33 pts/1 00:00:00 grep arch 
postgres 228626 736 0 21:33 ? 00:00:00 sh -c DIR=/ks3data/binlogs/v3/c2880a97-c981-4962-a25b-568ce07fbe80/7ab5da8d-ef99-4783-85ac-628730ea0124/`date +%F`; sudo test ! -d $DIR && sudo mkdir $DIR; sudo test ! -f $DIR/000000010000000500000043 && sudo pgbackrest --stanza=demo archive-push pg_wal/000000010000000500000043

比较,一共有pgbackrest三个进程,
查看相关日志

-------------------PROCESS START------------------- 2021-01-29 21:32:29.735 P00 INFO: archive-push:async command begin 2.30: [/rds/postgresql/pg_wal] --archive-async --compress-level=3 --log-level-console=off --log-level-file=detail --log-level-stderr=off --pg1-path=/rds/postgresql --process-max=3 --repo1-path=/ks3data/binlogs/v3/c2880a97-c981-4962-a25b-568ce07fbe80/7ab5da8d-ef99-4783-85ac-628730ea0124/2021-01-27 --spool-path=/rds/pgbackrest --stanza=demo 2021-01-29 21:32:29.736 P00 INFO: push 72 WAL file(s) to archive: 00000001000000050000000A...000000010000000500000051 
2021-01-29 21:32:32.877 P02 DETAIL: pushed WAL file '00000001000000050000000B' to the archive 
2021-01-29 21:32:33.508 P03 DETAIL: pushed WAL file '00000001000000050000000C' to the archive 
2021-01-29 21:32:34.445 P01 DETAIL: pushed WAL file '00000001000000050000000A' to the archive 
2021-01-29 21:32:36.955 P01 DETAIL: pushed WAL file '00000001000000050000000F' to the archive 
2021-01-29 21:32:37.764 P02 DETAIL: pushed WAL file '00000001000000050000000D' to the archive 
2021-01-29 21:32:39.259 P03 DETAIL: pushed WAL file '00000001000000050000000E' to the archive 
2021-01-29 21:32:41.799 P01 DETAIL: pushed WAL file '000000010000000500000010' to the archive 
2021-01-29 21:32:42.598 P02 DETAIL: pushed WAL file '000000010000000500000011' to the archive 
2021-01-29 21:32:43.564 P03 DETAIL: pushed WAL file '000000010000000500000012' to the archive 
2021-01-29 21:32:45.864 P01 DETAIL: pushed WAL file '000000010000000500000013' to the archive 
2021-01-29 21:32:46.533 P02 DETAIL: pushed WAL file '000000010000000500000014' to the archive 
2021-01-29 21:32:47.608 P03 DETAIL: pushed WAL file '000000010000000500000015' to the archive 
2021-01-29 21:32:50.496 P01 DETAIL: pushed WAL file '000000010000000500000016' to the archive
............ 
2021-01-29 21:34:22.864 P00 INFO: archive-push:async command end: completed successfully (113129ms) 

由日志可见:p00负责统计需要归档的wal文件格式和检查p01,p02,p03进程归档状态 ,p01,p02,p03异步并行负责日志归档。

结果&结论:
使用pgBackRest并行归档,问题解决。

你可能感兴趣的:(数据库,postgresql)