前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PostgreSQL-wal日志

PostgreSQL-wal日志

原创
作者头像
DB之路
修改2021-03-18 17:52:12
2K0
修改2021-03-18 17:52:12
举报
文章被收录于专栏:数据库PG数据库PG

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)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档