Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto clean pg_xlog

Tags:

Does anyone know how to clean up pg_xlog in PostgreSQL 9.5 automatically? Since I enabled replication disk has been filled up to 200G for 2 weeks. So I cleaned it manually with the following commands:

sudo service portgesql stop
sudo /usr/lib/postgresql/9.5/bin/pg_controldata /var/lib/postgresql/9.5/main/ | grep Next | grep -v Multi
sudo -u postgres /usr/lib/postgresql/9.5/bin/pg_resetxlog -o 44842 -x 575323138 -f /var/lib/postgresql/9.5/main/
sudo service portgesql start

Of course I can automate it with bash script, but the problem here is that it is necessary to stop PotgreSQL. Is there any other ways to clean this without stopping PostgreSQL?

like image 591
Ilya Avatar asked Sep 16 '16 07:09

Ilya


People also ask

Can I delete Pg_xlog?

These logs are also used for binary replication. Unlike pg_log, you may not freely delete, move, or compress files in this directory. You may not even move the directory without symlinking it back to its original location. Deleting pg_xlog files may result in unrecoverable database corruption.

Can I delete files in pg_wal?

You can never delete pg_wal files. pg_wal is the directory that contains the primary WAL files, and there will never be a *. backup file in it.

What is pg_wal?

pg_wal (in PostgreSQL 9.6 and earlier: pg_xlog ) earlier is the subdirectory of the main PostgreSQL data directory where WAL files are stored. It also contains the subdirectory archive_status .

What is the importance of PG XLOG directory?

Running out of disk space in the pg_xlog directory is a fairly common Postgres problem. This important directory holds the WAL (Write Ahead Log) files. (WAL files contain a record of all changes made to the database—​see the link for more details).


1 Answers

Slightly worrying that you're removing files from pg_xlog, they're imperative to the recovery of your database. Please don't tell me this is production :)

Anyway, there's a more fundamental reason why WAL's are gathering at that kind of rate which is due to the newly introduced "replication slots" in PostgreSQL.

The replication slot will keep WAL logs until they've been applied to the DR so your WALS are not being applied to the standby. Chances are if you've been removing WALS then they never will be applied :) and thus comes the viscious cycle.

You can also control the amount of WALS in that directory by the parameted of "wal_keep_segments" though I'm not sure how much of a role this has in 9.5 with a replica and a replication slot.

Either way you certainly need to stop deleting WAL's and get your DR rebuilt with a fresh sync of your master.

like image 90
d1ll1nger Avatar answered Sep 25 '22 16:09

d1ll1nger