Inside databases PostgreSQL
PostgreSQL
A database server that logs changes in its write-ahead log before its data files change, and keeps old row versions until VACUUM removes them. Here with Patroni, etcd and HAProxy for failover.
My work
A PostgreSQL cluster on Patroni with etcd and HAProxy. Monitoring of vacuum and resources is automated.
How it is built
A generalised layout from the PostgreSQL 17 documentation and the Patroni documentation as read on 2026-11-25. Patroni, HAProxy and etcd are tools beside PostgreSQL, not parts of it; one primary and one standby are an example.
After a successful failover
the route after the failure unavailable takes over
- The leader’s Patroni no longer renews the lease on the leader lock: ttl seconds after the last renewal (30 by default) it expires, and a leader race begins.
- If the configuration store is still reachable and the standby lags by no more than maximum_lag_on_failover, its Patroni may take part in the race. The member that takes the lock promotes its PostgreSQL to read/write; any others are configured to follow it.
- Once HAProxy’s checks of the Patroni REST API at / on that node get 200, which there only the primary holding the leader lock answers, HAProxy sends new connections there.
- With the default asynchronous replication, a failover can lose what the old primary had committed and not yet streamed: at worst maximum_lag_on_failover bytes of log plus what was written in the last ttl seconds. 6
7 9 10 13
Components
Press a component to read what it does.
Client SQL
An application that sends SQL. It connects to HAProxy, which leads it to the primary.
HAProxy one endpoint
One endpoint for the cluster’s leader. In the configuration Patroni provides, it health-checks each node’s Patroni REST API, expecting status 200, and sends new connections to the node that passes: the primary.
Patroni the leader
A tool beside PostgreSQL that holds the lease on the leader lock in the configuration store while this node is the primary. Only Patroni should start, stop and promote the PostgreSQL it runs.
Primary PostgreSQL
The server that takes writes. Its tables and indexes are in data files, which change only after the change has been logged in the WAL.
WAL pg_wal
The write-ahead log: records that describe changes, flushed to permanent storage before the data files change. Replayed after a crash, and streamed to the standby.
Patroni a replica
The same tool on the standby. While another node holds the leader lock, it keeps its PostgreSQL following that one.
Standby hot standby
A server that receives the WAL from the primary as it is generated and replays it. As a hot standby it can answer read-only queries.
etcd config store
The distributed configuration store where the leader lock lives, with its lease. Patroni acts on what the store says about the cluster; its documentation says the store has to run on 3 or 5 nodes, for proper consensus and fault tolerance.
-
Log first
A change is logged first: the data files change only after the WAL records describing it have been flushed to permanent storage.
In detail
WAL’s central concept is that changes to data files, where tables and indexes reside, are written only after those changes have been logged, that is, after the WAL records describing them have been flushed to permanent storage. Then data pages need not be flushed to disk at every commit: after a crash, changes not yet applied to them can be redone from the WAL records. 1
3 11 Where it breaks Replication slots keep the primary from removing WAL segments until the standbys using them have received them: so many can be kept that they fill the space allocated for pg_wal. By default (max_slot_wal_keep_size = -1) there is no limit; set, it limits what slots may keep at a checkpoint, and a standby that falls further behind may then be unable to continue replication.
-
Crash recovery
At a checkpoint every dirty data page is flushed to disk and a checkpoint record is written to the WAL. After a crash, recovery replays the WAL from the point that record names.
In detail
Checkpoints are points in the sequence of transactions at which the heap and index data files are guaranteed to hold everything written before them. At checkpoint time all dirty data pages are flushed to disk and a special checkpoint record is written to the WAL. After a crash, recovery looks at the latest checkpoint record to find the point in the WAL, the redo record, from which it starts the REDO operation. 2
Where it breaks Changes made to data files before the redo record are already on disk, so the WAL segments preceding the one that holds the redo record are no longer needed and can be recycled or removed (with WAL archiving on, once they have been archived). Flushing every dirty page at a checkpoint can be a heavy load of I/O.
-
Streaming replication
The standby connects to the primary, which streams WAL records to it as they are generated, and the standby replays them. By default the primary does not wait for it: replication is asynchronous.
In detail
The standby connects to the primary, which streams WAL records to the standby as they’re generated, without waiting for the WAL file to be filled. Streaming replication is asynchronous by default. A standby can also be used for read-only queries, and is then called a hot standby. Standby mode is exited and the server switches to normal operation when pg_ctl promote is run or pg_promote() is called. 3
Where it breaks Asynchronous means a small delay between a commit on the primary and the change becoming visible on the standby, typically under a second if the standby keeps up with the load: a commit the primary has confirmed may not have reached the standby yet.
-
Clean-up
An UPDATE or DELETE does not remove the old row version at once: other transactions may still see it. VACUUM removes the dead versions later, and autovacuum runs it.
My part Monitoring of vacuum and resources is automated.
In detail
Under MVCC reading never blocks writing and writing never blocks reading, and a row version must not be deleted while it is still potentially visible to other transactions. The standard VACUUM removes dead row versions in tables and indexes and marks the space available for reuse. It also marks old rows frozen: inserted by a transaction that committed far enough in the past to be visible to all current and future transactions. 4
5 Where it breaks Every table must be vacuumed at least once every two billion transactions. If the warnings are ignored, the server refuses to assign new transaction IDs once fewer than three million are left before wraparound.
-
The leader lock
Patroni on the primary holds a lease on the leader lock in etcd. HAProxy health-checks Patroni’s REST API on every node and sends new connections to the one answering 200: the primary.
My part A PostgreSQL cluster on Patroni with etcd and HAProxy. Patroni for the Superset database.
In detail
ttl sets the lease time on the leader lock in the configuration store, 30 seconds by default; the leader renews the lease during its HA cycles. GET / and GET /primary on the REST API return 200 only when the Patroni node is running as the primary with the leader lock; where the JSON document it returns is not needed, HEAD or OPTIONS may be used instead of GET. Patroni provides an HAProxy configuration that gives the application a single endpoint for the cluster’s leader: HAProxy checks each node’s REST API and expects status 200. 6
7 8 9 12 13 Where it breaks Patroni acts on the state of the configuration store: if the store loses its majority it becomes unresponsive, and, with failsafe_mode off (its default), Patroni demotes the current read/write node. With failsafe_mode on, the primary may keep running if it can reach every known member of the cluster through the REST API.
-
Failover
If the leader cannot renew its lease for longer than ttl, the lease expires and a leader race begins: the first member able to take the lock promotes itself to read/write.
In detail
When the lease expires, all members check whether they are the best candidates for taking over the leader role, partly by calling the REST API of the others, and those that are attempt to take the leader lock; a member that lags by more than maximum_lag_on_failover bytes does not take part in the election. The first Patroni member that takes the lock promotes itself to a read/write node, and the others are configured to follow it. 6
9 10 Where it breaks By default Patroni configures asynchronous replication, and committed transactions can be lost in a failover: at worst maximum_lag_on_failover bytes of log plus what was written in the last ttl seconds. Where that is not acceptable, synchronous_mode does not promote a standby unless it is certain that the standby holds every transaction that may have returned a successful commit to a client; writes can then be unavailable while servers are up, and transactions run with synchronous_commit off or local can still be lost.
Sources
- PostgreSQL 17: Write-Ahead Logging (WAL)
- PostgreSQL 17: WAL Configuration
- PostgreSQL 17: Log-Shipping Standby Servers
- PostgreSQL 17: Routine Vacuuming
- PostgreSQL 17: Concurrency Control, Introduction
- Patroni: Dynamic Configuration Settings
- Patroni: REST API
- Patroni: Introduction
- Patroni: FAQ
- Patroni: Replication modes
- PostgreSQL 17: Replication
- Patroni: DCS Failsafe Mode
- Patroni: haproxy.cfg
Next engine
Greenplum
A massively parallel database made of PostgreSQL instances: one master that plans, and segments that hold the data and do the work.