Problem:
The client experienced a failover event in their PostgreSQL cluster managed by Patroni.
Process:
Step 1: Initial Investigation
The initial PostgreSQL logs revealed regular query activity. This included frequent queries from monitoring tools (pg_stat_all_tables, pg_locks, etc.), checkpoint logging, and client connection messages. There were no immediate signs of crash or shutdown events (PANIC, FATAL, or database system is shutting down), nor evidence of role transitions.
This absence of failover indicators led to a request for additional data: Patroni logs, thread dumps, and logs from both the primary and standby nodes, including those surrounding the suspected failover window.
Step 2: Detailed Analysis of Patroni and PostgreSQL Logs
A review of the Patroni and PostgreSQL logs from the failover window made the issue clear.
At the start of the incident, Node 1 (the primary) abruptly closed its connection to PostgreSQL. This was immediately followed by:
- Multiple failed attempts to re-establish a connection to PostgreSQL (
psycopg2.OperationalError: timeout expired); - Inability to update the leader lock in etcd:
EtcdKeyNotFound; - Failure of
last_operation()checks inside Patroni, which ultimately triggered a self-demotion (immediate-nolockbehavior); - The node entered recovery and attempted to reconnect as a standby, but connection issues persisted for nearly an hour.
Meanwhile, logs from Node 2 showed that about 30 seconds later the standby assumed leadership:
I am (<node-2>), the leader with the lock.
This transition confirms that the cluster’s HA mechanism worked as intended: when the primary failed to maintain its lock, the standby took over.
Step 3: PostgreSQL Behavior Under Load and Replication Failures
PostgreSQL logs around the same time showed intense activity, including:
- Mass
DELETEandINSERToperations; - Continuous
VACUUMprocesses (manual and autovacuum); CURSOR,BEGIN, and high-frequencyUPDATEstatements to a job control table;
Around the same time, a duplicate key violation occurred during an INSERT, indicating a concurrency issue:
ERROR: duplicate key value violates unique constraint "<table>_pkey"
Later, a replication failure occurred on the standby:
ERROR: requested WAL segment <segment> has already been removed
This means the replica lagged behind, and the required WAL segment had already been purged, likely due to default wal_keep_size being too low or the replica being under high I/O pressure.
This did not impact service directly but indicates that replication was temporarily broken and would require reinitialization or archived WAL recovery.
Solution:
The failover was caused by connectivity issues between Patroni and PostgreSQL on Node 1, leading to a DCS (etcd) lock failure and automatic demotion. High query volume may have worsened CPU or I/O load. To prevent this, adjust Patroni and PostgreSQL settings, ensure WAL retention and replication stability, address application concurrency and batch job bottlenecks, and improve monitoring to track failovers and Patroni state changes.
Conclusion:
The PostgreSQL cluster experienced a leader transition initiated by a loss of connectivity between Patroni and PostgreSQL on the original primary. The system correctly demoted the problematic node and promoted the standby, ensuring service continuity. However, the incident highlighted risks associated with high load, default configuration limits (especially WAL retention), and the need for fine-tuned HA settings in Patroni and PostgreSQL.
By addressing both infrastructure-level timeouts and application query patterns, the client can significantly improve the stability and resilience of their PostgreSQL HA setup.