DEV Community

Pawan Kukreja
Pawan Kukreja

Posted on • Edited on

1

[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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay