归档模式管理

一、启用归档模式
查看当前状态
show data_directory;

show archive_mode;


创建归档目录
mkdir archivelog


在postgresql.conf文件中修改如下三个参数:
# 打开归档模式
archive_mode = on

# 配置归档命令
archive_command = 'DATE=`date +%Y%m%d`;DIR="/data/postgres/archivelog/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f'

wal_level = replica


重启PG
pg_ctl -D /var/lib/pgsql/14/data restart


查看归档
show archive_mode;


查看wal日志列表
select * from pg_ls_waldir() order by modification asc;


切换归档日志
checkpoint;

select pg_switch_wal();


再次查看wal日志列表
select * from pg_ls_waldir() order by modification asc;


查看归档文件

cd /data/postgres/archivelog/
-bash-4.2$ ll -lh
total 64M
-rw-------. 1 postgres postgres 16M Aug 13 01:01 000000010000000000000002
-rw-------. 1 postgres postgres 16M Aug 13 01:01 000000010000000000000003
-rw-------. 1 postgres postgres 16M Aug 13 01:01 000000010000000000000004
-rw-------. 1 postgres postgres 16M Aug 13 01:01 000000010000000000000005



二、通过脚本配置归档定期删除
archive_command : 可以传入一个shell命令或者一个复杂的shell脚本。

%p : 表示将要归档的wal文件包含完整路径的信息的文件名(就是需要归档的临时文件)
%f : 代表不包含路径信息的wal文件的文件名
%% : 表示%
当启用归档后,会生成大量得归档文件,如果没有及时删除,会导致磁盘空间撑满。 所以我们可以在archive_command目录中,传入脚本,在归档得同时,删除之前得脚本:

创建shell脚本:
-bash-4.2$ pwd
/var/lib/pgsql/14/data
-bash-4.2$ cat arch.sh
#!/bin/sh
test ! -f /data/postgres/archivelog/$1 && cp --preserve=timestamps $2 /data/postgres/archivelog/$1 ; find /data/postgres/archivelog/ -type f -mtime +7 -exec rm -f {} /;
-bash-4.2$ chmod a+x arch.sh


修改postgresql.conf文件:
# 以下为archive_command
archive_command = '/var/lib/pgsql/14/data/arch.sh %f %p'


重启数据库:
pg_ctl -D /var/lib/pgsql/14/data restart


切换归档:
select pg_switch_wal();



三、手工删除归档文件
select * from pg_ls_waldir() order by modification asc;
           name           |   size   |      modification
--------------------------+----------+------------------------
 000000010000000000000002 | 16777216 | 2022-08-13 00:57:33+08
 000000010000000000000003 | 16777216 | 2022-08-13 00:58:21+08
 000000010000000000000004 | 16777216 | 2022-08-13 01:01:04+08
 000000010000000000000005 | 16777216 | 2022-08-13 01:01:52+08
 000000010000000000000006 | 16777216 | 2022-08-13 01:01:53+08
 
 
wal日志位置:$PGDATA/pg_wal 
wal日志文件命名规则:000000010000000000000008

其中前8位:00000001表示timeline;
中间8位:00000000表示logid;
最后8位:00000008表示logseg

查看检查点以前的WAL文件

-bash-4.2$ pg_controldata $PGDATA
pg_control version number:            1300
Catalog version number:               202107181
Database system identifier:           7129316018383056294
Database cluster state:               in production
pg_control last modified:             Sat 13 Aug 2022 01:24:53 AM CST
Latest checkpoint location:           0/8000098
Latest checkpoint's REDO location:    0/8000060
Latest checkpoint's REDO WAL file:    000000010000000000000008
Latest checkpoint's TimeLineID:       1
Latest checkpoint's PrevTimeLineID:   1
Latest checkpoint's full_page_writes: on
……
这里表示000000010000000000000008 之前得文件都可以删除,在系统层面删除这些归档文件即可,可以手工执行rm 也可以通过pg_archivecleanup命令进行:

-bash-4.2$ cd /data/postgres/archivelog/
-bash-4.2$ ll
total 16384
-rw-------. 1 postgres postgres 16777216 Aug 13 01:21 000000010000000000000007
drwx------. 2 postgres postgres      166 Aug 13 01:19 20220813

-bash-4.2$ pg_archivecleanup -d /data/postgres/archivelog/  000000010000000000000008
pg_archivecleanup: keeping WAL file "/data/postgres/archivelog//000000010000000000000008" and later
pg_archivecleanup: removing file "/data/postgres/archivelog//000000010000000000000007"


四、手工删除PG_WAL 文件
正常情况下,在配置PG 归档时会配置archive_command,如下:

archive_command = ‘DATE=date +%Y%m%d;DIR=”/data/postgres/archivelog/$DATE”;(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f’

该命令会将WAL 复制到到归档目录,然后被清理掉。

自动清理WAL的场景:

做检查点的时候。
数据库启动时,或者修改了相关参数重启数据库时。
如果开启了归档,则在归档路径下的archive_status目录里,会有一个类似xxx.ready和xxx.done的文件。
-bash-4.2$ cd /var/lib/pgsql/14/data/pg_wal
-bash-4.2$ ll
total 81920
-rw-------. 1 postgres postgres 16777216 Aug 13 01:24 000000010000000000000008
-rw-------. 1 postgres postgres 16777216 Aug 13 01:01 000000010000000000000009
-rw-------. 1 postgres postgres 16777216 Aug 13 01:01 00000001000000000000000A
-rw-------. 1 postgres postgres 16777216 Aug 13 01:19 00000001000000000000000B
-rw-------. 1 postgres postgres 16777216 Aug 13 01:21 00000001000000000000000C
drwx------. 2 postgres postgres        6 Aug 13 01:24 archive_status
-bash-4.2$

.ready表示WAL已经写满,可以调用归档命令了。
.done表示已归档完成。
开启归档以后,只有归档成功的wal才可以被清理。

如果数据库配置不当, 比如你配置了archive_mode=on,但是没有配置archive_command,那么WAL文件会一直堆积,因为没有配置archive_command,也就是说不会触发归档命令,所以一直都不会写.done,从而导致pg_wal文件堆积,占用大量得磁盘空间。

此时可以使用pg_archivecleanup命令来清理。

查看检查点以前的WAL文件
-bash-4.2$ pg_controldata $PGDATA
pg_control version number:            1300
Catalog version number:               202107181
Database system identifier:           7129316018383056294
Database cluster state:               in production
pg_control last modified:             Sat 13 Aug 2022 01:24:53 AM CST
Latest checkpoint location:           0/8000098
Latest checkpoint's REDO location:    0/8000060
Latest checkpoint's REDO WAL file:    000000010000000000000008
Latest checkpoint's TimeLineID:       1
Latest checkpoint's PrevTimeLineID:   1
Latest checkpoint's full_page_writes: on
……

这里表示000000010000000000000008 之前的文件都可以删除,然后执行 pg_archivecleanup命令。

-bash-4.2$ pg_archivecleanup -d $PGDATA/pg_wal 000000010000000000000008
pg_archivecleanup: keeping WAL file "/var/lib/pgsql/14/data/pg_wal/000000010000000000000008" and later
-bash-4.2$

 

posted @ 2025-06-17 15:31  屠魔的少年  阅读(31)  评论(0)    收藏  举报