SoFunction
Updated on 2025-04-08

How to trigger archives manually by pgsql

method:

Before pg10.0:

select pg_switch_xlog();

After pg10.0:

select pg_switch_wal();

Note: After executing pg_switch_xlog(), WAL will switch to the new log, and the old WAL log will be archived.

In addition to manually triggering archives, what other situations will pg archive?

Two situations:

① The WAL log is full and the archive is triggered.

Archive will be triggered after the wal log is full. The wal log is 16MB by default. This value can be set through the parameter "--with-wal-segsize" when compiling PostgreSQL. It cannot be modified after compilation.

②archive_timeout time control.

You can set the archive timeout parameter archive_timeout. If archive_timeout=60 is set, WAL log switching will be triggered every 60s and log archive will be triggered. There is an implicit assumption here: There are still unarchived WALs in the current WAL log.

Supplement: Postgresql enables archive logs

Step 1:

Modify postgresql configuration file()

wal_level=hot_standby
archive_mode =on 
archive_command ='DATE=`date +%Y%m%d`;DIR="/home/postgres/arch/$DATE";(test -d $DIR || mkdir -p $DIR)&& cp %p $DIR/%f'

ps: %p refers to the relative path %f refers to the file name

Step 2:

Create an archive path

mkdir -p /home/postgres/arch
chown -R postgres:postgres /home/postgres/arch

Step 3:

Restart the database

Step 4:

Verify that the archive is normal

postgres=# checkpoint;
 CHECKPOINT
  postgres=# select pg_switch_xlog();
  pg_switch_xlog 
   ----------------
  1/760000E8
  (1 row)
 postgres@ubuntu:~$ cd /home/postgres/data/data_1999/arch/
 postgres@ubuntu:~/data/data_1999/arch$ ls
 20150603
 postgres@ubuntu:~/data/data_1999/arch$ cd 20150603/
 postgres@ubuntu:~/data/data_1999/arch/20150603$ ls
000000010000000100000074 000000010000000100000075 000000010000000100000076

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.