After a Patroni switchover, apps stay on a read-only node
After a planned switchover, apps get "read-only transaction" errors. Old connections stay on the old leader, now a replica. The fix with HAProxy and pgbouncer.
A planned switchover was done. patronictl list was clean: the new leader was up, the replicas were in sync. A few minutes later, write errors started piling up in the application logs.
Symptoms
- Apps get this error:
ERROR: cannot execute INSERT in a read-only transaction
- Reads work; only writes fail.
- The new primary is healthy. Connecting to it directly, writes work fine.
- The error doesn't show up on every request, only on some.
Why does it happen?
During a switchover, the old leader turns into a replica. PostgreSQL stays up and doesn't drop connections. But pooled connections (pgbouncer server connections, HAProxy's open TCP sessions) are still attached to the old node. The app keeps using the same connection, and when it tries to write, it is talking to a read-only node.
HAProxy sends new connections to the right node based on the health check result. But it doesn't close existing sessions on its own. That's why the error shows up on "some requests": newly opened connections go to the right place, old ones don't.
Diagnosis
How many clients are still on the old leader?
SELECT usename, application_name, client_addr, state, count(*)
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY 1, 2, 3, 4
ORDER BY 5 DESC;
In pgbouncer's admin console, check where the server connections go:
SHOW CLIENTS;
SHOW SERVERS; -- if the addr column shows the old leader, this is the problem
Open sessions per backend in HAProxy:
echo "show stat" | socat stdio /run/haproxy/admin.sock \
| cut -d, -f1,2,5,18 # pxname, svname, scur, status
Fix
Check the HAProxy primary backend through the Patroni REST API. When a node stops being primary, close its open sessions too:
listen pg_primary
bind *:5000
mode tcp
option httpchk GET /primary
http-check expect status 200
default-server inter 2s fall 2 rise 2 on-marked-down shutdown-sessions
server pg1 10.0.0.21:5432 check port 8008
server pg2 10.0.0.22:5432 check port 8008
server pg3 10.0.0.23:5432 check port 8008
The real work is done by the on-marked-down shutdown-sessions line. Without it, HAProxy marks the node "down" but the old connections live on.
For pgbouncer there are two ways:
- Point pgbouncer at HAProxy's primary port, not directly at the nodes.
- After a switchover, refresh the connections per database:
RECONNECT <veritabani>;
-- or, more forcefully:
KILL <veritabani>;
RESUME <veritabani>;
On the app side, the read-only error (SQLSTATE 25006) should be treated as retryable: drop the connection, get a new one, retry the transaction.
Watch out
Before calling a switchover "done", count the sessions left on the old leader:
psql -h 10.0.0.21 -U <izleme-kullanicisi> -d postgres -Atc \
"SELECT count(*) FROM pg_stat_activity WHERE backend_type = 'client backend' AND usename <> current_user;"
If the result isn't zero, apps are still on the old node. Add this check to the switchover runbook or automation.
Lesson
"Failover works" means clients reconnect too. patronictl list only shows the database layer. Test it with a real application, doing real writes.
Checklist
- [ ] Is the HAProxy primary backend checked with
GET /primary? - [ ] Is
on-marked-down shutdown-sessionsset? - [ ] Does pgbouncer point at HAProxy's primary port?
- [ ] Does the app retry on the read-only error?
- [ ] Are sessions on the old leader counted after a switchover?
- [ ] Was the failover tested with a real application?