DEV Community

Pawan Kukreja
Pawan Kukreja

Posted on • Updated on

[Summary] Chapter#09 "The Internals of PostgreSQL" Write Ahead Logging (WAL) (Part-02)

Writing of XLOG Records

Ready to understand writing XLOG records.
Following each line of pseudo code will explained for understanding of XLOG records

  • The function ExtendCLOG() writes statement of this transaction.
  • The function heap_insert() insert a heap tuple into target page on shared buffer pool.
  • The function XLoginsert() writes the XLog.
  • Function finish_xact_command() commits the transaction.
  • Function XLogWrite() writes and flushes all XLOG records on the WAL buffer to WAL segment file.
  • Function TransactionIdCommitTree() changes the state of this transaction.

WAL Writer Process
WAL writer is a background process to check the WAL buffer periodically and write all unwritten XLOG records into the WAL segment. If this process has not been enable the writing of XLOG records might have been bottlenecked when a large amount of data at one time.

Checkpoint Processing in PostgreSQL
One of the following occurs when process starts

  • The interval time set for checkpoint_timeout from the previous checkpoint has been gone over.
  • In version 9.4 or earlier Number of WAL segment file set for checkpoint_segment has been consumed since previous checkpoint
  • In version 9.5 or later total size of WAL segment files in the pg_xlog has exceeded the value of the parameter max_wal_size.
  • PostgreSQL server stops in smart or fast mode.

Database Recovery in PostgreSQL
PostgreSQL implements redo log based recovery feature. PostgreSQL restore database cluster by sequentially replaying the XLOG records in the WAL segment file from REDO point.
The first step towards recovery is stat up of pg_control file.

WAL segment Files Management
PostgreSQL writes XLOG records to one of the WAL segment files stored. in the pg_xlog subdirectory and switches for a new one if the old one has been filled up.

WAL segment Switches

  • WAL segment has been filled up
  • The function pg_switch_xlog has been issued. -archive_mode is enable and the time set to archive_timeout has been exceeded.

Continuous Archiving and Archive Logs
Feature that copy WAL segment files to archival area at the time when WAL segment switches and perform archiver process. The copied file called archive log.

Reference

Top comments (0)