pg_xlog 的重要性不言而喻,不可以隨意直接rm讨彼,官方文檔中也有明確說明。
當(dāng)隨著時間的推移柿祈,xlog目錄會越來越大哈误,網(wǎng)上有些方法是先停機(jī)然后再刪除,但我不想停機(jī)刪除躏嚎,那么如何優(yōu)雅清理xlog日志呢蜜自?
方法:
1.登錄到postgres中查看當(dāng)前的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
- 再次查看 wal_keep_segments的值
postgres=# show wal_keep_segments ;
wal_keep_segments
-------------------
512
(1 row)
--> 查看到值已經(jīng)降低卢佣,但是pg_xlog的日志文件數(shù)量仍然是那么多重荠,
原因是因為每隔 checkpoints時間wal 會自動檢查一次,checkpoint_timeout默認(rèn)是5分鐘。
postgres=# show checkpoint_timeout ;
checkpoint_timeout
--------------------
5min
(1 row)
5.想要立刻見效虚茶,可以手動執(zhí)行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不一致戈鲁,可能會導(dǎo)致流復(fù)制不同步,因此在各個節(jié)點一定保持wal_keep_segments數(shù)量一致嘹叫。
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