How long does a PostgreSQL cluster stay without a primary when the primary dies without warning? Not a clean shutdown, not a switchover: the whole node gone at once while an application keeps writing.

We measured it on a real Patroni cluster with the default settings. The application went 24.8 seconds without being able to write, and then writes came back on their own on another node, with no manual action.

▶️ Video (1:40):

The setup

  • 3 PostgreSQL 16 nodes (pg1, pg2, pg3), managed by Patroni 4.1 with etcd as the consensus store (one etcd member per node).
  • Asynchronous streaming replication, one primary and two replicas.
  • Patroni defaults: ttl: 30, loop_wait: 10, retry_timeout: 10.
  • Cluster deployed and monitored with the HighAvailabilityData console (free).
  • Lab, not production: the three nodes ran on a single server, as separate processes with their own data directories, ports and etcd members. There was no load balancer in this test.

The application connects with a libpq multi-host connection string, so it always looks for whichever node accepts writes:

host=pg1,pg2,pg3 port=5441,5442,5443 target_session_attrs=read-write connect_timeout=1 

It inserts about ten rows per second and, on any error, closes the connection and retries every 200 ms:

import psycopg, time conn = None while True: try: if conn is None: conn = psycopg.connect(DSN, autocommit=True) conn.execute("INSERT INTO orders DEFAULT VALUES") log_ok(conn.info.port) # which node took the write except Exception: if conn: conn.close() conn = None log_error() time.sleep(0.2) continue time.sleep(0.1) 

The number we report is the gap between the last write confirmed on the old primary and the first write confirmed on the new one. That is what your application actually feels.

The "power cut"

To simulate a node that disappears, we killed every process of pg1 at once with kill -9: Patroni, PostgreSQL and its etcd member. Nothing gets a chance to say goodbye: Patroni does not release the leader key and nobody is told that the primary is gone.

What happened, second by second

Time Event
0.0 s pg1 killed. The next INSERT fails.
0 – 24.8 s Every attempt fails: pg2 and pg3 are still read-only replicas, so target_session_attrs=read-write finds no node to write to.
~24.7 s The leader key in etcd expires. The replicas notice on their next loop, pg3 wins the race for the key and promotes itself.
24.8 s The application's next retry lands on pg3. Writes are back.

Later, when pg1 came back, we started it from the console and it rejoined as a replica of pg3 (Patroni uses pg_rewind if needed). The cluster was healthy again without rebuilding anything by hand.

Why ~25 seconds and not 2

Because Patroni is doing exactly what it should. The primary holds a leader key in etcd with a TTL of 30 seconds and renews it every loop_wait (10 s). When the primary dies, nobody can take over until that key expires. Depending on when the last renewal happened, that is somewhere between ttl - loop_wait and ttl seconds after the crash (20 to 30 s with the defaults), plus a moment for the replicas to notice and race for the key.

That wait is not wasted time. It is what guarantees that there are never two primaries at the same time (split brain). A replica cannot promote itself just because it lost contact with the primary: it has to win the key in etcd, and etcd only grants it once the old leader's lease is gone. If the old primary were still alive but cut off from the rest of the cluster, it would fail to renew its key and demote itself.

Can you make it faster?

Yes, by lowering ttl and loop_wait in the Patroni configuration. Patroni requires loop_wait + 2 × retry_timeout ≤ ttl, so you cannot shrink one without the others. Shorter values mean faster failover but also more chances of a false failover when the network or etcd has a hiccup of a few seconds. For most teams the defaults, about half a minute of write unavailability for an unplanned crash, are a reasonable trade-off. A planned switchover, by contrast, takes only a few seconds because the old primary hands over the leader key cleanly.

What this test does not show

  • A real network. All nodes were on one server, so there was no network latency or partition. On separate machines the numbers depend on your network and etcd.
  • A load balancer. With HAProxy in front, the application only sees the virtual IP, and you add HAProxy's health-check interval to the gap. That is where the 26–49 s range with default settings comes from.
  • Synchronous replication. We used asynchronous replication, so writes confirmed on pg1 in its last moment could be missing on pg3. If you cannot lose any committed transaction, use synchronous replication.
  • The console's own safety net. The HighAvailabilityData console can promote a replica itself if Patroni does not elect one in time. For this test we set it to wait longer than the TTL, so that the number above is Patroni's own election.

Reproduce it

  1. Build a 3-node Patroni + etcd cluster with the default ttl / loop_wait (we used the free HighAvailabilityData console, but any Patroni setup works).
  2. Run a write loop like the one above with a multi-host read-write connection string and log the node of every successful write.
  3. kill -9 Patroni, PostgreSQL and the etcd member of the primary at the same moment.
  4. Measure the gap between the last and the first successful write.

If you get a very different number, we would like to hear about it.

Disclosure: I work on HighAvailabilityData, a free on-premise console from NUTRADELIS S.L. for Patroni clusters.

More on how Patroni and repmgr decide failover: Patroni vs repmgr. Methodology of our HAProxy-based tests: failover tests.