5.4. Commit Log (clog)

PostgreSQL holds the statuses of transactions in the Commit Log. The Commit Log, often called the clog, is allocated to shared memory and is used throughout transaction processing.

This section describes the the status of transactions in PostgreSQL, how the clog operates, and maintenance of the clog.

5.4.1 Transaction Status

PostgreSQL defines four transaction states: IN_PROGRESS, COMMITTED, ABORTED, and SUB_COMMITTED.

The first three statuses are self-explanatory. For example, when a transaction is in progress, its status is IN_PROGRESS.

SUB_COMMITTED is for sub-transactions, and its description is omitted in this document.

5.4.2. How Clog Performs

The clog comprises one or more 8 KB pages in shared memory. It logically forms an array, where the indices of the array correspond to the respective transaction ids, and each item in the array holds the status of the corresponding transaction id. Figure 5.7 shows the clog and how it operates.

T1:txid 200 commits; the status of txid 200 is changed from IN_PROGRESS to COMMITTED.
T2:txid 201 aborts;the status of txid 201 is changed from IN_PROGRESS to ABORTED.
Fig. 5.7. How the clog operates.

T1:txid 200 commits; the status of txid 200 is changed from IN_PROGRESS to COMMITTED.
T2:txid 201 aborts;the status of txid 201 is changed from IN_PROGRESS to ABORTED.

When the current txid advances and the clog can no longer store it, a new page is appended.

When the status of a transaction is needed, internal functions are invoked. Those functions read the clog and return the status of the requested transaction. (See also ‘Hint Bits’ in Section 5.7.1.)

5.4.3. Maintenance of the Clog

When PostgreSQL shuts down or whenever the checkpoint process runs, the data of the clog are written into files stored in the pg_xact subdirectory. (Note that pg_xact was called pg_clog in versions 9.6 or earlier.) These files are named ‘0000’, ‘0001’, and so on.

The maximum file size is 256 KB. For example, if the clog uses eight pages (the first page to the eighth page, the total size is 64 KB), its data are written into ‘0000’ (64 KB). If the clog uses 37 pages (296 KB), its data are written into ‘0000’ and ‘0001’, which are 256 KB and 40 KB in size, respectively.

When PostgreSQL starts up, the data stored in the pg_xact files are loaded to initialize the clog.

The size of the clog continuously increases because a new page is appended whenever the clog is filled up. However, not all data in the clog are necessary. Vacuum processing, described in Chapter 6, regularly removes such old data (both the clog pages and files).

Details about removing the clog data are described in Section 6.4.