Add new archive_mode value always to allow standbys to always archive received WAL files
PostgreSQL 流复制架构中备节点不支持接收来自主节点的 WAL 日志文件,如果备节点要取归档文件时,一般有两种解决方案,第一种是主节点将 WAL 文件归档到共享存储上(NFS 也可以),主,备节点都能读取归档的日志文件; 另一种是将主节点已归档的日志文件 copy 到备节点上。
9.5 版本新增 archive_mode = always,允许备库接收主库的 WAL 日志文件。之前版本 archive_mode 参数仅允许 on 或 off,接着做个小实验:
环境准备
主节点 192.168.2.38/1931 主机名 db2 归档目录 /archive/pg95/
主节点 192.168.2.37/1931 主机名 db1 归档目录 /archive/pg95/
insert.sh 脚本
1 |
|
备注:写个脚本定时插入数据,让数据库处于写的状态。
执行脚本
1 |
[pg95/dev/null 2>&1 & 2 tf]$./insert.sh > |
配置归档参数
修改备节点 archive_mode 参数:db1 上执行
1 |
[pg95@db1 pg95]$ grep "archive_mode =" $PGDATA/postgresql.conf |
删除归档文件: db1 上执行
1 |
[pg95@db1 pg_root]$ cd /archive/pg95 |
查看归档命令: db2 上执行
1 |
[pg95@db2 tf]$ psql -c "show archive_command" |
切换 WAL: db2 上执行
1 |
postgres=# select pg_switch_xlog(); |
查看归档情况
查看归档日志, db2 上执行
1 |
[pg95@db2 pg95]$ ls -alrt | tail -n 1 |
查看归档日志, db1 上执行
1 |
[pg95@db1 pg95]$ ll |
备注:可以看出备节点归档目录新增加的 WAL 文件和主节点归档目录产生的 WAL 一样。
附 archive_mode (enum)
When archive_mode is enabled, completed WAL segments are sent to archive storage by setting archive_command. In addition to off, to disable, there are two modes: on, and always. During normal operation, there is no difference between the two modes, but when set to always the WAL archiver is enabled also during archive recovery or standby mode. In always mode, all files restored from the archive or streamed with streaming replication will be archived (again). See Section 25.2.9 for details.
archive_mode and archive_command are separate variables so that archive_command can be changed without leaving archiving mode. This parameter can only be set at server start. archive_mode cannot be enabled when wal_level is set to minimal.
参考
- Postgres 9.5 feature highlight: archive_mode = always
- Continuous archiving in standby
- PostgreSQL:关于 archive_command 归档命令
- PostgreSQL:“ FATAL: requested WAL segment 0000000800002A0000000000 has already been removed”
原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/tech/database/239640.html