6.4. Removing Unnecessary Clog Files

The clog, described in Section 5.4, stores transaction statuses. PostgreSQL attempts to remove unnecessary clog files whenever it updates pg_database.datfrozenxid. Note that this process also removes the corresponding clog pages.

Figure 6.9 shows an example of this process. If the minimum pg_database.datfrozenxid exists in the clog file ‘0002’, the system can remove the older files (‘0000’ and ‘0001’). This is possible because all transactions in those files are treated as frozen txids across the entire database cluster.

Figure 6.9. Removing unnecessary clog files and pages.
pg_database.datfrozenxid and the clog file

The following shows the actual output of pg_database.datfrozenxid and the clog files:

$ psql testdb -c "SELECT datname, datfrozenxid FROM pg_database"
  datname  | datfrozenxid
-----------+--------------
 template1 |      7308883
 template0 |      7556347
 postgres  |      7339732
 testdb    |      7506298
(4 rows)
$ ls -la -h data/pg_xact/	# In versions 9.6 or earlier, "ls -la -h data/pg_clog/"
total 316K
drwx------  2 postgres postgres   28 Dec 29 17:15 .
drwx------ 20 postgres postgres 4.0K Dec 29 17:13 ..
-rw-------  1 postgres postgres 256K Dec 29 17:15 0006
-rw-------  1 postgres postgres  56K Dec 29 17:15 0007