PostgreSQL Architecture
PostgreSQL Architecture
Postmaster
• The Postmaster is the main controller process that manages connections, initiates
child processes, and oversees the overall database system.
• Postmaster process acts as the main server process and functions as the
listener for client connections.
2. Shared Memory
PostgreSQL uses shared memory for caching and storing critical information. It includes:
Background Processes
1. WAL Writer: Writes changes from the Write-Ahead Logging (WAL)
buffer to disk.
2. Checkpointer: Periodically saves dirty buffers to disk to ensure data
persistence.
3. Background Writer: Flushes dirty pages to disk, reducing the load on
checkpoints.
4. Archiver: Handles WAL file archiving for backups.
5. Logger process: is responsible for handling all logging operations. It
collects and writes error messages, activity logs, and other notices
generated by the database server to log files.
6. Autovacuum: Cleans up dead tuples and updates table statistics.
Control logging behaviour through various settings in the postgresql.conf file, such as:
wal_level parameter controls the amount of Write-Ahead Logging (WAL) data that
is generated by the database server. Especially concerning replication and point-in-time
recovery.
wal_level Settings
1. minimal:
o Only enough WAL is generated to ensure the durability of transactions.
o This level does not support replication or point-in-time recovery.
2. replica (default for most installations):
o Generates additional WAL for replication.
o This level allows for streaming replication and point-in-time recovery.
3. logical:
o Includes all the information needed for logical replication.
o This level allows changes to be captured in a format that can be sent to other
systems (like logical decoding).
max_wal_size
• Definition: Maximum size that WAL files can grow before forcing a checkpoint.
• Default: Typically 1 GB.
• Purpose: Controls disk space used by WAL; limits accumulation to improve
performance and reduce I/O.
min_wal_size
• Definition: Minimum size for WAL files that PostgreSQL tries to maintain.
• Default: Typically 80 MB.
• Purpose: Ensures sufficient WAL space is available to minimize overhead from
creating/removing WAL files.
Checkpoint Trigger:
• When the size of the WAL files exceeds the max_wal_size limit, PostgreSQL will
automatically trigger a checkpoint.
• The checkpoint_timeout being reached.
2. logging_collector: If enabled, it captures log messages and directs them to log files.
o Default: off
logging_collector = on
under PGDATA/pg_log
7. log_line_prefix: Prefix to add to each log line, useful for adding timestamps, user
info, etc.
log_line_prefix = '%m [%p]: [%l-1] user=%u,db=%d '
8. log_rotation_age: Determines how often log files are rotated based on age.
log_rotation_age = 1d
10. log_checkpoints provides valuable insight into the database's checkpointing behavior,
Default Value: off
Connection options:
Autovacuum is a background process that manages dead tuples and reclaims storage
space automatically. This process ensures that table statistics are up-to-date and prevents
table bloat. Key configuration options include:
Autovacuum works with the Free Space Map (FSM) to track and reuse free space
efficiently, reducing storage bloat and maintaining performance.