阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

PostgreSQL如何维护WAL日志/归档日志

156次阅读
没有评论

共计 3781 个字符,预计需要花费 10 分钟才能阅读完成。

WAL 日志介绍

wal 全称是 write ahead log,是 postgresql 中的 online redo log,是为了保证数据库中数据的一致性和事务的完整性。而在 PostgreSQL 7 中引入的技术。它的中心思想是“先写日志后写数据”,即要保证对数据库文件的修改应放生在这些修改已经写入到日志之后,同时,在 PostgreSQL 8.3 以后又加入了 WalWriter 日志写进程,可以保证事务提交记录不是在提交时同步写入到磁盘,而是异步写入,这样就极大的减轻了 I / O 的压力。所以说 WAL 日志很重要。对保证数据库中数据的一致性和事务的完整性。

PostgreSQL 的 WAL 日志文件在 pg_xlog 目录下,一般情况下,每个文件为 16M 大小:000000010000000000000010 文件名称为 16 进制的 24 个字符组成,每 8 个字符一组,每组的意义如下:
•时间线:英文为 timeline,是以 1 开始的递增数字,如 1,2,3…
•LogId:32bit 长的一个数字,是以 0 开始递增的数字,如 0,1,2,3…
•LogSeg:32bit 长的一个数字,是以 0 开始递增的数字,如 0,1,2,3…

wal 日志跟 online redo log 一样,其个数,也不是无限的。归档日志就出现了。

WAL 日志维护

1. 参数 max_wal_size/min_wal_size
  9.5 以前:(2 + checkpoint_completion_target) * checkpoint_segments + 1
  9.5:PostgreSQL 9.5 将废弃 checkpoint_segments 参数, 并引入 max_wal_size 和 min_wal_size 参数,
  通过 max_wal_size 和 checkpoint_completion_target 参数来控制产生多少个 XLOG 后触发检查点,
  通过 min_wal_size 和 max_wal_size 参数来控制哪些 XLOG 可以循环使用。
2. 参数 wal_keep_segments
  在流复制的环境中。使用流复制建好备库,如果备库由于某些原因接收日志较慢。导致备库还未接收到。就被覆盖了。导致主备无法同步。这个需要重建备库。
  避免这种情况提供了该参数。每个日志文件大小 16M。如果参数设置 64. 占用大概 64×16=1GB 的空间。根据实际环境设置。
3. pg_resetxlog
  在前面参数设置合理的话。是用不到 pg_resetxlog 命令。
[postgres@postgres128 ~]$ pg_resetxlog -?
pg_resetxlog resets the PostgreSQL transaction log.

Usage:
  pg_resetxlog [OPTION]… DATADIR

Options:
  -c XID,XID      set oldest and newest transactions bearing commit timestamp
                  (zero in either value means no change)
 [-D] DATADIR      data directory
  -e XIDEPOCH      set next transaction ID epoch
  -f              force update to be done
  -l XLOGFILE      force minimum WAL starting location for new transaction log
  -m MXID,MXID    set next and oldest multitransaction ID
  -n              no update, just show what would be done (for testing)
  -o OID          set next OID
  -O OFFSET        set next multitransaction offset
  -V, –version    output version information, then exit
  -x XID          set next transaction ID
  -?, –help      show this help, then exit

Report bugs to <pgsql-bugs@postgresql.org>.     

归档日志维护

1. pg_archivecleanup 清理归档日志。
[postgres@postgres128 ~]$ pg_archivecleanup -?
pg_archivecleanup removes older WAL files from PostgreSQL archives.

Usage:
  pg_archivecleanup [OPTION]… ARCHIVELOCATION OLDESTKEPTWALFILE

Options:
  -d            generate debug output (verbose mode)
  -n            dry run, show the names of the files that would be removed
  -V, –version  output version information, then exit
  -x EXT        clean up files if they have this extension
  -?, –help    show this help, then exit

For use as archive_cleanup_command in recovery.conf when standby_mode = on:
  archive_cleanup_command = ‘pg_archivecleanup [OPTION]… ARCHIVELOCATION %r’
e.g.
  archive_cleanup_command = ‘pg_archivecleanup /mnt/server/archiverdir %r’

Or for use as a standalone archive cleaner:
e.g.
  pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup

1.1 当主库不断把 WAL 日志拷贝到备库。这个时候需要清理。在 recovery.conf 可以配置 
e.g.  archive_cleanup_command = ‘pg_archivecleanup /mnt/server/archiverdir %r’
1.2 可以收到执行命令。
e.g.  pg_archivecleanup /home/postgres/arch/ 000000010000000000000009
在归档目录 /home/postgres/arch/ 把 000000010000000000000009 之前的日志都清理。

2. pg_rman 备份
在 pg_rman 备份保留策略中。在每天都备份。可以清理归档日志。
对流复制环境中。备份一般是在备库。可以把归档日志传送到备库中。
  –keep-arclog-files=NUM  keep NUM of archived WAL
  –keep-arclog-days=DAY    keep archived WAL modified in DAY days
e.g 保留归档日志个数 10。或者保留 10 天内的归档日志。
KEEP_ARCLOG_FILES = 10 
KEEP_ARCLOG_DAYS = 10 
在备份信息中会产生以下信息。
INFO: start deleting old archived WAL files from ARCLOG_PATH (keep files = 10, keep days = 10)

Ubuntu 16.04 下安装 PostgreSQL 和 phpPgAdmin  http://www.linuxidc.com/Linux/2016-08/134260.htm

Linux 下 RPM 包方式安装 PostgreSQL  http://www.linuxidc.com/Linux/2016-03/128906.htm

Linux 下安装 PostgreSQL  http://www.linuxidc.com/Linux/2016-12/138765.htm

Linux 下 PostgreSQL 安装部署指南  http://www.linuxidc.com/Linux/2016-11/137603.htm

Linux 下安装 PostgreSQL 并设置基本参数  http://www.linuxidc.com/Linux/2016-11/137324.htm

Ubuntu 16.04 下 PostgreSQL 主从复制配置  http://www.linuxidc.com/Linux/2017-08/146190.htm

Fedota 24 将数据库升级到 PostgreSQL 9.5  http://www.linuxidc.com/Linux/2016-11/137374.htm

CentOS 6.5 下 PostgreSQL 服务部署  http://www.linuxidc.com/Linux/2017-01/139144.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-09/146896.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计3781字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中