Write-Ahead Logging (WAL) in PostgreSQL
Write-Ahead Logging (WAL) is PostgreSQL's mechanism for ensuring durability, crash recovery, and replication.
The core principle is:
Before changing the actual data page on disk, PostgreSQL first writes a record describing the change to the WAL.
This follows the ACID property of Durability.
Why WAL Is Needed
Imagine an account table:
Suppose:
New balance:
Now consider:
Without WAL:
Database corruption or inconsistency can occur.
WAL Principle
Instead of immediately writing the table page:
PostgreSQL guarantees:
Hence the name:
Internal Flow
Suppose:
Step 1
Data page loaded into shared buffers.
Step 2
Update performed in memory.
The actual disk page is still unchanged.
Step 3
Generate WAL record.
Example (simplified):
Step 4
Write WAL record into WAL buffer.
Step 5
COMMIT
Before PostgreSQL returns:
it flushes WAL to disk.
directory.
Step 6
Actual table page written later.
may write table pages minutes later.
Crash Recovery Example
Before Crash
Table:
Memory:
WAL:
WAL already flushed.
Power Failure
Memory lost.
Startup Recovery
PostgreSQL reads WAL:
and replays the operation.
Result:
is restored.
WAL Records
WAL does not store full tables.
Instead it stores changes.
Example:
Everything important is logged.
WAL Files
Location:
Example:
Typically:
per WAL segment.
Check:
WAL and Checkpoints
Without checkpoints:
Very slow.
PostgreSQL periodically performs checkpoints.
Checkpoint
A checkpoint writes dirty pages from memory to disk.
and records:
in WAL.
Recovery After Checkpoint
Instead of replaying all WAL:
Much faster.
WAL and COMMIT
A common interview question:
Does COMMIT write table data?
No.
Not necessarily table pages.
Table pages can be written later.
This makes PostgreSQL much faster.
WAL Buffers
Parameter:
Example:
Purpose:
before flushing.
WAL Levels
Check:
Possible values:
minimal
Least WAL generation.
Used for:
replica
Default in modern PostgreSQL.
Supports:
logical
Supports:
Captures enough information to reconstruct row-level changes.
WAL and Replication
Primary server:
Standby server:
Applies the same changes.
Architecture:
This is PostgreSQL Streaming Replication.
WAL and Point-In-Time Recovery (PITR)
One of PostgreSQL's strongest features.
Example:
Need recovery to:
Restore:
Replay until:
Table restored.
Important WAL Parameters
wal_level
Usually:
max_wal_size
Example:
Controls WAL growth before checkpoint.
min_wal_size
Example:
WAL retained after checkpoints.
checkpoint_timeout
Default:
Maximum interval between checkpoints.
synchronous_commit
Values:
Performance Trade-off
Safer.
Faster but may lose a few transactions during crash.
Oracle Comparison
Oracle also has a similar mechanism.
| PostgreSQL | Oracle |
|---|---|
| WAL | Redo Logs |
| WAL Writer | LGWR |
| Checkpoint | Checkpoint |
| Crash Recovery | Instance Recovery |
| PITR | RMAN Recovery |
| WAL Streaming | Data Guard Redo Shipping |
For Oracle DBAs:
PostgreSQL WAL is conceptually equivalent to Oracle Redo Logs.
Interview Answer
Write-Ahead Logging (WAL) is PostgreSQL's durability mechanism. Every database change is first recorded in the WAL and flushed to disk before the transaction commit is acknowledged. If the server crashes before data pages are written, PostgreSQL replays the WAL during recovery to restore committed transactions.
One-Line Summary
Data page later, WAL first. PostgreSQL always writes the change record to WAL before writing the actual table page, enabling crash recovery, replication, and point-in-time recovery.
No comments:
Post a Comment