PostgreSQL之pg_wal目录中的“*.ready”和“*.done”文件是什么?

pg中,如果你开启了归档,即设置了archive_mode=on, 你可能在$PGDATA/pg_wal/archive_status中看到一些“*.ready”和“*.done”文件。 那么“*.done”和“*.ready”分别代表什么含义呢? 接下来我...

pg中,如果你开启了归档,即设置了archive_mode=on, 你可能$PGDATA/pg_wal/archive_status中看到一些“*.ready”和“*.done”文件。

attachments-2021-07-KcIgcDVg60fe4e33a3030,jpg

那么“*.done”和“*.ready”分别代表什么含义呢?

接下来我来演示下当开启归档时,archive_status文件夹下文件的变化。

1、首先开启归档,修改$PGDATA/data/postgresql.conf文件,设置archive_mode=on,archive_command = 'test ! -f /tmp/archived_wal/%f && cp %p /tmp/archived_wal/%f' 。

# - Archiving - archive_mode = on # enables archiving; off, on, or always

                                                      # (change requires restart)

archive_command = 'test ! -f /tmp/archived_wal/%f && cp %p /tmp/archived_wal/%f'  # command to use to archive a logfile segment

                                             # placeholders: %p = path of file to archive

                                            # %f = file name only

                                            # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'

其中archive_command的含义表示当文件标识 /tmp/archived_wal/%f 存在时,则会运行之后的归档命令: cp %p /tmp/archived_wal/%f。

如果是第一次开启需要先创建目录/tmp/archived_wal

mkdir /tmp/archived_wal

当前,我的服务器$PGDATA/pg_wal/和$PGDATA/pg_wal/archive_status目录下当前已有的文件如下,可以看到此时$PGDATA/pg_wal/archive_status文件夹下为空。

attachments-2021-07-negs89BL60fe5022bba58,jpg

/tmp/archive_wal文件夹下已有文件如下(有文件是因为我之前测试过几次):

attachments-2021-07-MB6cnKHH60fe5035e6e48,jpg

接下来重启服务器(因为archive_mode的修改,需要重启服务器才能生效)。

 /usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/data/logfile restart

2、登陆服务器,执行“select pg_switch_wal();”命令。

postgres=# select pg_switch_wal();

 pg_switch_wal

---------------

 0/7000160

(1 row)

3、再次查看$PGDATA/pg_wal/archive_status目录和/tmp/archive_wal目录。


$PGDATA/pg_wal/archive_status目录

[postgres@test pg_wal]$ ls

000000010000000000000007 000000010000000000000008 000000010000000000000009 archive_status [postgres@test pg_wal]$ cd archive_status [postgres@test archive_status]$ ls 000000010000000000000007.done


/tmp/archive_wal目录


[postgres@test archive_status]$ ls -la /tmp/archived_wal/

total 98312

drwxrwxr-x   2 postgres postgres     4096 Jul 25 14:19 .

drwxrwxrwt. 10 root     root         4096 Jul 25 14:11 ..

-rw-------   1 postgres postgres 16777216 Jul 25 13:46 000000010000000000000002

-rw-------   1 postgres postgres 16777216 Jul 25 14:00 000000010000000000000003

-rw-------   1 postgres postgres 16777216 Jul 25 14:00 000000010000000000000004

-rw-------   1 postgres postgres 16777216 Jul 25 14:07 000000010000000000000005

-rw-------   1 postgres postgres 16777216 Jul 25 14:08 000000010000000000000006

-rw-------   1 postgres postgres 16777216 Jul 25 14:19 000000010000000000000007


可以看到:

$PGDATA/pg_wal/archive_status文件夹下多了000000010000000000000007.done文件。/tmp/archived_wal/文件夹下多了000000010000000000000007


所以,可以说每当一个segement被成功归档时,我们就会得到相应的*.done文件


那么 *.ready文件是什么时候生成的呢?


4、修改archive_command为 archive_command='/bin/false'

postgres=# alter system set archive_command='/bin/false'; ALTER SYSTEM postgres=# select pg_reload_conf(); pg_reload_conf ---------------- t (1 row) postgres=# select pg_switch_wal(); pg_switch_wal --------------- 0/8000160 (1 row)


5、查看$PGDATA/pg_wal/archive_status目录。


[postgres@test archive_status]$ ls

000000010000000000000008.ready

可以看到当archive_command='/bin/false'时,archive_status目录下生成了*.ready文件。


所以说,那么 *.ready文件是什么时候生成的呢?


答案是 当段的归档失败时。


6、修改archive_command为archive_command = 'test ! -f /tmp/archived_wal/%f && cp %p /tmp/archived_wal/%f'

postgres=# alter system set archive_command='test ! -f /tmp/archived_wal/%f && cp %p /tmp/archived_wal/%f';

ALTER SYSTEM

postgres=# select pg_reload_conf();

 pg_reload_conf

----------------

 t

(1 row)


7、再次查看$PGDATA/pg_wal/archive_status目录。

[postgres@test archive_status]$ ls 000000010000000000000008.done


可以看到:

000000010000000000000008.ready

变为000000010000000000000008.done。


总结:


当服务器开启了归档,每当一个segement被成功归档时,我们会在archive_wal目录得到相应的*.done文件。而当segement归档失败时,则会得到相应的*.ready文件。



  • 发表于 2021-07-26 13:55
  • 阅读 ( 38 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
石天
石天

437 篇文章

作家榜 »

  1. shitian 662 文章
  2. 石天 437 文章
  3. 每天惠23 33 文章
  4. 小A 29 文章