postgres流复制环境下pg_xlog日志优雅的清理

pg_xlog 的重要性不言而喻,不可以随意直接rm,官方文档中也有明确说明。
当随着时间的推移,xlog目录会越来越大,网上有些方法是先停机然后再删除,但我不想停机删除,那么如何优雅清理xlog日志呢?

方法:
1.登录到postgres中查看当前的wal_keep_segments

postgres=# show wal_keep_segments ;
 wal_keep_segments 
-------------------
 1024
(1 row)

2.在 postgres.conf中修改 降低wal_keep_segments的值,本案例中降低到512

3.重载 postgres配置文件

-bash-4.1$ pg_ctl reload -D $PGDATA
  1. 再次查看 wal_keep_segments的值
postgres=# show wal_keep_segments ;
 wal_keep_segments 
-------------------
 512
(1 row)
--> 查看到值已经降低,但是pg_xlog的日志文件数量仍然是那么多,
原因是因为每隔 checkpoints时间wal 会自动检查一次,checkpoint_timeout默认是5分钟。

postgres=# show checkpoint_timeout ;
 checkpoint_timeout 
--------------------
 5min
(1 row)

5.想要立刻见效,可以手动执行checkpoint

postgres=# checkpoint ;

Note:
“ If a standby server connected to the sending server falls behind by more than wal_keep_segments segments, the sending server might remove a WAL segment still needed by the standby, in which case the replication connection will be terminated” 官方 文档中明确说了如果wal_keep_segments不一致,可能会导致流复制不同步,因此在各个节点一定保持wal_keep_segments数量一致。

《postgres流复制环境下pg_xlog日志优雅的清理》 关于wal_keep_segments的说明

pg归档的删除方法

-bash-4.1$ pg_archivecleanup --help
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

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

删除0000001D0000000000000054 之前的归档

-bash-4.1$ pg_archivecleanup  . 0000001D0000000000000054
    原文作者:Watson168
    原文地址: https://www.jianshu.com/p/57ec124a20f9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞