PostgreSQL Postmaster and Background Processes
PostgreSQL has a multi-process architecture. Unlike Oracle (which is largely multi-threaded), PostgreSQL creates multiple OS processes.
1. Postmaster (Main PostgreSQL Process)
The Postmaster is the parent process of all PostgreSQL processes.
When PostgreSQL starts:
or
the first process created is:
Its responsibilities:
- Listen for client connections
- Spawn backend processes
- Start background processes
- Monitor child processes
- Perform crash recovery
- Restart failed workers
Architecture:
2. Backend Processes
Every client connection gets its own backend process.
Example:
Query:
Each row corresponds to a backend process.
Important Background Processes
3. Background Writer (BgWriter)
Purpose:
When users modify data:
These pages stay in memory.
The Background Writer gradually writes dirty pages to disk.
Without it:
causing sudden I/O spikes.
View:
4. WAL Writer
One of the most important processes.
Purpose:
Flow:
Example:
Without WAL Writer:
5. Checkpointer
Purpose:
Checkpoint means:
Flow:
Checkpoint record also goes into WAL.
Benefits:
Check settings:
Monitor:
6. Autovacuum Launcher
Purpose:
It doesn't vacuum itself.
Rather:
Architecture:
Check:
7. Autovacuum Workers
Responsibilities:
- Remove dead tuples
- Update statistics
- Freeze old tuples
- Prevent XID wraparound
Example:
Shows currently running workers.
8. Statistics Collector (Older Versions)
In PostgreSQL before v14:
maintained statistics used by:
In modern PostgreSQL versions:
replaces much of the old collector architecture.
9. Archiver Process
Used when:
Purpose:
Used for:
Check:
Example:
10. Startup Process
Runs during startup and recovery.
Responsibilities:
Example:
Oracle equivalent:
11. WAL Sender Process
Used in Streaming Replication.
Primary server:
Check:
12. WAL Receiver Process
Runs on standby.
Purpose:
Apply changes continuously.
Check:
13. Logical Replication Launcher
Used for:
Launches logical replication workers.
Example:
Shared Memory Interaction
All background processes interact with shared memory.
Crash Scenario Example
Suppose:
Step 1
Backend updates shared buffer.
Step 2
Backend creates WAL record.
Step 3
WAL Writer flushes WAL.
Step 4
Checkpointer eventually writes data page.
Step 5
Server crashes.
Startup Process:
Database becomes consistent again.
PostgreSQL vs Oracle Background Processes
| PostgreSQL | Oracle Equivalent |
|---|---|
| Postmaster | PMON + Listener coordination |
| Backend Process | Dedicated Server Process |
| WAL Writer | LGWR |
| Checkpointer | CKPT |
| Background Writer | DBWR |
| Startup Process | SMON Recovery |
| Autovacuum | No direct equivalent |
| WAL Sender | Data Guard Redo Transport |
| WAL Receiver | Managed Recovery Process |
| Archiver | ARCn |
Most Common Interview Question
Explain PostgreSQL Process Architecture
Answer:
PostgreSQL uses a process-based architecture where the Postmaster acts as the parent process. For every client connection, Postmaster creates a dedicated backend process. Several background processes support database operations, including the WAL Writer for durability, Checkpointer for writing dirty pages to disk, Background Writer for smoothing I/O, Autovacuum Workers for MVCC cleanup and freeze operations, Archiver for WAL archiving, and WAL Sender/Receiver processes for streaming replication. Together they ensure performance, crash recovery, and data consistency.
No comments:
Post a Comment