签到成功

知道了

CNDBA社区CNDBA社区

PostgreSQL 配置 归档模式

2022-12-12 13:08 2944 0 原创 PostgreSQL
作者: dave

1 查看当前状态

cndba=# show data_directory;
     data_directory
------------------------
 /var/lib/pgsql/14/data
(1 row)

cndba=# show archive_mode;
 archive_mode
--------------
 off
(1 row)

2 启用归档模式

2.1 创建归档目录

-bash-4.2$ cd /data/postgres/
-bash-4.2$ mkdir archivelog
-bash-4.2$ ll
total 0
drwxr-xr-x. 2 postgres postgres  6 Aug 13 00:37 archivelog
drwx------. 3 postgres postgres 29 Aug 10 05:55 data
-bash-4.2$

2.2 修改归档参数

在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

2.3 重启PG

-bash-4.2$ ps -ef|grep pg
postgres  3559     1  0 Aug09 ?        00:00:21 /usr/pgsql-14/bin/postgres -D /var/lib/pgsql/14/data
postgres 25214 11533  0 00:53 pts/3    00:00:00 grep --color=auto pg

-bash-4.2$ pg_ctl -D /var/lib/pgsql/14/data restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2022-08-13 00:53:40.977 CST [25461] LOG:  redirecting log output to logging collector process
2022-08-13 00:53:40.977 CST [25461] HINT:  Future log output will appear in directory "log".
 done
server started

-bash-4.2$ ps -ef|grep pg
postgres 25461     1  0 00:53 ?        00:00:00 /usr/pgsql-14/bin/postgres -D /var/lib/pgsql/14/data
postgres 25556 11533  0 00:53 pts/3    00:00:00 grep --color=auto pg
-bash-4.2$

2.4 查看归档

cndba=# show archive_mode;
 archive_mode
--------------
 on
(1 row)

查看wal日志列表

cndba=# 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:04+08
(4 rows)

进行切换

cndba=# checkpoint;
CHECKPOINT
cndba=# select pg_switch_wal();
 pg_switch_wal
---------------
 0/50000F0
(1 row)

再次查看wal日志列表http://www.cndba.cn/cndba/dave/article/116385

cndba=# 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
(5 rows)

cndba=#

2.5 查看归档文件

-bash-4.2$ cd /data/postgres/archivelog/
-bash-4.2$ pwd
/data/postgres/archivelog
-bash-4.2$ ll
total 0
drwx------. 2 postgres postgres 134 Aug 13 01:01 20220813
-bash-4.2$ cd 20220813/
-bash-4.2$ ll
total 65536
-rw-------. 1 postgres postgres 16777216 Aug 13 01:01 000000010000000000000002
-rw-------. 1 postgres postgres 16777216 Aug 13 01:01 000000010000000000000003
-rw-------. 1 postgres postgres 16777216 Aug 13 01:01 000000010000000000000004
-rw-------. 1 postgres postgres 16777216 Aug 13 01:01 000000010000000000000005
-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
-bash-4.2$

3 通过脚本配置归档定期删除

archive_command : 可以传入一个shell命令或者一个复杂的shell脚本。

  1. %p : 表示将要归档的wal文件包含完整路径的信息的文件名(就是需要归档的临时文件)
  2. %f : 代表不包含路径信息的wal文件的文件名
  3. %% : 表示%

当启用归档后,会生成大量得归档文件,如果没有及时删除,会导致磁盘空间撑满。 所以我们可以在archive_command目录中,传入脚本,在归档得同时,删除之前得脚本:

创建shell脚本:http://www.cndba.cn/cndba/dave/article/116385

-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
-bash-4.2$

修改postgresql.conf文件:

http://www.cndba.cn/cndba/dave/article/116385

# 以下为archive_command
archive_command = '/var/lib/pgsql/14/data/arch.sh %f %p'

重启库:

-bash-4.2$ pg_ctl -D /var/lib/pgsql/14/data restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2022-08-13 01:19:52.566 CST [14752] LOG:  redirecting log output to logging collector process
2022-08-13 01:19:52.566 CST [14752] HINT:  Future log output will appear in directory "log".
 done
server started
-bash-4.2$

切换归档:

cndba=# select pg_switch_wal();
 pg_switch_wal
---------------
 0/70000F0
(1 row)

此时就直接生成了:

[dave@www.cndba.cn archivelog]# pwd
/data/postgres/archivelog
[dave@www.cndba.cn archivelog]# ls
20220813
[dave@www.cndba.cn archivelog]# ll
total 16384
-rw-------. 1 postgres postgres 16777216 Aug 13 01:21 000000010000000000000007
drwx------. 2 postgres postgres      166 Aug 13 01:19 20220813
[dave@www.cndba.cn archivelog]#

4 手工删除归档文件

cndba=# 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
(5 rows)

wal日志位置:$PGDATA/pg_wal

[dave@www.cndba.cn pg_wal]# pwd
/var/lib/pgsql/14/data/pg_wal
[dave@www.cndba.cn pg_wal]# 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
[dave@www.cndba.cn pg_wal]#

wal日志文件命名规则:000000010000000000000008

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

查看检查点以前的WAL文件http://www.cndba.cn/cndba/dave/article/116385

-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命令进行:http://www.cndba.cn/cndba/dave/article/116385

-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"

5 手工删除PG_WAL 文件

正常情况下,在配置PG 归档时会配置archive_command,如下:http://www.cndba.cn/cndba/dave/article/116385http://www.cndba.cn/cndba/dave/article/116385

archive_command = ‘DATE=date +%Y%m%d;DIR=”/data/postgres/archivelog/$DATE”;(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f’http://www.cndba.cn/cndba/dave/article/116385

该命令会将WAL 复制到到归档目录,然后被清理掉。 http://www.cndba.cn/cndba/dave/article/116385

自动清理WAL的场景:

  1. 做检查点的时候。
  2. 数据库启动时,或者修改了相关参数重启数据库时。

如果开启了归档,则在归档路径下的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命令。http://www.cndba.cn/cndba/dave/article/116385

-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$

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2283
    原创
  • 3
    翻译
  • 579
    转载
  • 196
    评论
  • 访问:8174473次
  • 积分:4428
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ