pg_archivecleanup简介
【简介】
pg_archivecleanup设计用于archive_cleanup_command作为备用服务器运行时自动清理WAL归档日志。
pg_archivecleanup也可以作为一个独立的程序来清理WAL归档日志。
如果开启了归档,则在归档路径下的archive_status目录里,会有一个类似xxx.ready和xxx.done的文件。
.ready表示xlog已经写满,可以调用归档命令了
.done表示已归档完成
开启归档以后,只有归档成功的xlog才可以被清理。
WAL的命名格式为16进制的24个字符组成,每8个字符一组,每组的意义如下:
00000001 00000000 00000001
时间线 逻辑id 物理id
【自动清理WAL的场景】
1.做检查点的时候。
2.数据库启动时,或者修改了相关参数重启数据库时。
【触发检查点的情况】
1.从前一个检查点过后,超过checkpoint_timeout设置的间隔(默认5分钟)。
2.<=pg9.4的版本,超过使用checkpoint_segments设置的WAL文件数量(默认3)。
3.>=pg9.5的版本,超过max_wal_size设置的值(默认64个文件,共1GB)。
4.postgre在smart和fast模式下停止。
5.手动执行checkpoint。
【示例】
方式一:
1.双机模式,建议设置自动清理。
2.设置备节点配置文件standby.signal:
standby_mode = 'on'
primary_conninfo = 'host=192.168.1.251 port=5432 user=postgres'
recovery_target_timeline = 'latest'
restore_command = 'cp /opt/pg14/data_node2/archivedir/%f %p'
archive_cleanup_command = 'pg_archivecleanup /opt/pg14/data_node2/archivedir %r'
方式二:
1.用于设置不当,导致WAL堆积的情况下,手动删除指定目录下某归档文件之前的所有归档文件。
2.pg_archivecleanup参数说明:
pg_archivecleanup [option] archivelocation oldestkeptwalfile
-d:debug模式
-n:试运行,打印将被删除的WAL日志文件
-V/–version:打印pg_archivecleanup版本并退出
-x extension:连同压缩归档日志一并删除
-?/–help:显示pg_archivecleanup有关的帮助信息
注意:删除前,需要先执行pg_controldata $PGDATA查看已经执行的检查点。
【具体演示过程】
1.配置postgresql.conf文件,并重启数据库生效。
listen_addresses = '*'
archive_mode = on
archive_command = 'test ! -f /opt/pg14/archivedir/%f && cp %p /opt/pg14/archivedir/%f'
如果压缩存储归档日志:
archive_command = 'gzip <%p> /opt/pg14/archivedir/%f.gz'
利用pg_archivecleanup删除的话,需要利用-x参数执行。
2.在/opt/pg14目录下创建归档目录archivedir。
3.重启数据库使参数生效。
4.执行pgbench -i -F 10 -s 10 -Upostgres postgres生成WAL日志。
[postgres@localhost bin]$ ./pgbench -i -F 10 -s 10 -Upostgres postgres
dropping old tables...
NOTICE: table "pgbench_accounts" does not exist, skipping
NOTICE: table "pgbench_branches" does not exist, skipping
NOTICE: table "pgbench_history" does not exist, skipping
NOTICE: table "pgbench_tellers" does not exist, skipping
creating tables...
generating data (client-side)...
1000000 of 1000000 tuples (100%) done (elapsed 3.85 s, remaining 0.00 s)
vacuuming...
creating primary keys...
done in 6.37 s (drop tables 0.00 s, create tables 0.01 s, client-side generate 3.88 s, vacuum 1.70 s, primary keys 0.78 s).
5.手动执行checkpoint。
[postgres@localhost bin]$ ./psql -dpostgres -Upostgres
psql (14.0)
Type "help" for help.postgres=# checkpoint;
CHECKPOINT
6.执行一次基础备份。
[postgres@localhost bin]$ ./pg_basebackup -D /opt/pg14/data_001 -Ft -Pv -Xf -z -Z5 -Upostgres
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/B000028 on timeline 1
1398512/1398512 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/B000138
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed
7.查看已执行的检查点。
[postgres@localhost bin]$ ./pg_controldata /opt/pg14/data/
pg_control version number: 1300
Catalog version number: 202107181
Database system identifier: 7124568253379589423
Database cluster state: in production
pg_control last modified: 2022年07月29日 星期五 10时35分58秒
Latest checkpoint location: 0/C000098
Latest checkpoint's REDO location: 0/C000060
Latest checkpoint's REDO WAL file: 00000001000000000000000C
Latest checkpoint's TimeLineID: 1
Latest checkpoint's PrevTimeLineID: 1
Latest checkpoint's full_page_writes: on
Latest checkpoint's NextXID: 0:744
Latest checkpoint's NextOID: 24576
Latest checkpoint's NextMultiXactId: 1
Latest checkpoint's NextMultiOffset: 0
Latest checkpoint's oldestXID: 726
Latest checkpoint's oldestXID's DB: 1
Latest checkpoint's oldestActiveXID: 744
Latest checkpoint's oldestMultiXid: 1
Latest checkpoint's oldestMulti's DB: 1
Latest checkpoint's oldestCommitTsXid:0
Latest checkpoint's newestCommitTsXid:0
Time of latest checkpoint: 2022年07月29日 星期五 10时35分58秒
Fake LSN counter for unlogged rels: 0/3E8
Minimum recovery ending location: 0/0
Min recovery ending loc's timeline: 0
Backup start location: 0/0
Backup end location: 0/0
End-of-backup record required: no
wal_level setting: replica
wal_log_hints setting: off
max_connections setting: 100
max_worker_processes setting: 8
max_wal_senders setting: 10
max_prepared_xacts setting: 0
max_locks_per_xact setting: 64
track_commit_timestamp setting: off
Maximum data alignment: 8
Database block size: 8192
Blocks per segment of large relation: 131072
WAL block size: 8192
Bytes per WAL segment: 16777216
Maximum length of identifiers: 64
Maximum columns in an index: 32
Maximum size of a TOAST chunk: 1996
Size of a large-object chunk: 2048
Date/time type storage: 64-bit integers
Float8 argument passing: by value
Data page checksum version: 0
Mock authentication nonce: e1a6f65f24a06728e67c2721e591f58bb283cf993aead4b554912cf56c7da786
8.查看归档及WAL日志文件。
[postgres@localhost bin]$ ll /opt/pg14/archivedir/
总用量 180228
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000001
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000002
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000003
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000004
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000005
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000006
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000007
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000008
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000009
-rw-------. 1 postgres postgres 16777216 7月 29 10:34 00000001000000000000000A
-rw-------. 1 postgres postgres 16777216 7月 29 10:35 00000001000000000000000B
-rw-------. 1 postgres postgres 338 7月 29 10:35 00000001000000000000000B.00000028.backup
[postgres@localhost bin]$ ll /opt/pg14/data/pg_wal/
总用量 163844
-rw-------. 1 postgres postgres 338 7月 29 10:35 00000001000000000000000B.00000028.backup
-rw-------. 1 postgres postgres 16777216 7月 29 10:36 00000001000000000000000C
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 00000001000000000000000D
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 00000001000000000000000E
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 00000001000000000000000F
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000010
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000011
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000012
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000013
-rw-------. 1 postgres postgres 16777216 7月 29 10:34 000000010000000000000014
-rw-------. 1 postgres postgres 16777216 7月 29 10:35 000000010000000000000015
drwx------. 2 postgres postgres 59 7月 29 10:35 archive_status
[postgres@localhost bin]$ ll /opt/pg14/data/pg_wal/archive_status/
总用量 0
-rw-------. 1 postgres postgres 0 7月 29 10:35 00000001000000000000000B.00000028.backup.done
9.删除000000010000000000000003之前的归档日志文件。
[postgres@localhost bin]$ ./pg_archivecleanup -d -n /opt/pg14/archivedir/ 000000010000000000000003
pg_archivecleanup: keeping WAL file "/opt/pg14/archivedir//000000010000000000000003" and later
/opt/pg14/archivedir//000000010000000000000001
pg_archivecleanup: file "/opt/pg14/archivedir//000000010000000000000001" would be removed
/opt/pg14/archivedir//000000010000000000000002
pg_archivecleanup: file "/opt/pg14/archivedir//000000010000000000000002" would be removed
[postgres@localhost bin]$ ./pg_archivecleanup -d /opt/pg14/archivedir/ 000000010000000000000003
pg_archivecleanup: keeping WAL file "/opt/pg14/archivedir//000000010000000000000003" and later
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000001"
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000002"
[postgres@localhost bin]$ ll /opt/pg14/archivedir/
总用量 147460
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000003
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000004
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000005
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000006
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000007
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000008
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000009
-rw-------. 1 postgres postgres 16777216 7月 29 10:34 00000001000000000000000A
-rw-------. 1 postgres postgres 16777216 7月 29 10:35 00000001000000000000000B
-rw-------. 1 postgres postgres 338 7月 29 10:35 00000001000000000000000B.00000028.backup
10.删除00000001000000000000000B.00000028.backup之前的归档日志文件。
[postgres@localhost bin]$ ll /opt/pg14/archivedir/
总用量 147460
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000003
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000004
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000005
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000006
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000007
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000008
-rw-------. 1 postgres postgres 16777216 7月 29 10:30 000000010000000000000009
-rw-------. 1 postgres postgres 16777216 7月 29 10:34 00000001000000000000000A
-rw-------. 1 postgres postgres 16777216 7月 29 10:35 00000001000000000000000B
-rw-------. 1 postgres postgres 338 7月 29 10:35 00000001000000000000000B.00000028.backup
[postgres@localhost bin]$ ./pg_archivecleanup -d /opt/pg14/archivedir/ 00000001000000000000000B.00000028.backup
pg_archivecleanup: keeping WAL file "/opt/pg14/archivedir//00000001000000000000000B" and later
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000003"
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000004"
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000005"
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000006"
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000007"
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000008"
pg_archivecleanup: removing file "/opt/pg14/archivedir//000000010000000000000009"
pg_archivecleanup: removing file "/opt/pg14/archivedir//00000001000000000000000A"
[postgres@localhost bin]$ ll /opt/pg14/archivedir/
总用量 16388
-rw-------. 1 postgres postgres 16777216 7月 29 10:35 00000001000000000000000B
-rw-------. 1 postgres postgres 338 7月 29 10:35 00000001000000000000000B.00000028.backup
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
