Skip to content

cannot execute INSERT in a read-only transaction: you hit a replica

PostgreSQLtested on PostgreSQL 18.7, CloudNativePG 1.30.2

Reading works, writing doesn't: the connection has landed on a hot standby. A standby accepts read queries but never writes. The only question is which path – port, service or pooler – led there.

Symptom

kubectl exec -n tag14 psql-client -- psql -h pg-notes-ro \
  -c 'INSERT INTO notes (text) VALUES (1)'
ERROR:  cannot execute INSERT in a read-only transaction
command terminated with exit code 1

In an application the same message shows up in the log, with SQLSTATE 25006. The application starts, reads – and only fails on the first write.

Diagnosis

On the same connection, ask whether the server is in recovery and which address it has.

kubectl exec -n tag14 psql-client -- psql -h pg-notes-ro \
  -Atc 'SELECT count(*), pg_is_in_recovery(), inet_server_addr() FROM notes'
1001|t|10.0.1.85

Causes

Hot standbyConnected straight to the standby

Hot standby means: read queries yes, writes never. In the course the standby runs on pg2 on port 5433 – a wrong port is enough.

sudo -u postgres psql -p 5433 -d tag10 -c "INSERT INTO ping (txt) VALUES ('von pg2')"
ERROR:  cannot execute INSERT in a read-only transaction

Service -ro / -rCloudNativePG service for readers

-ro spreads read load across the replicas, -r includes the primary. Writes only work through -rw. The EndpointSlices show which Pods are behind which service.

kubectl get endpointslices -n tag14 \
  -o custom-columns=NAME:.metadata.name,ADDR:.endpoints[*].addresses[0]
NAME                ADDR
pg-notes-r-tvs8n    10.0.1.139,10.0.2.53,10.0.1.85
pg-notes-ro-ktk62   10.0.2.53,10.0.1.85
pg-notes-rw-2rxv7   10.0.1.139

Pooler type roThe pooler is called -rw but is ro

The name is just a name. type: ro attaches the CNPG pooler to the -ro service – PgBouncer then connects to the replicas.

kubectl get pooler shop-pooler-rw -n tag19
kubectl exec -n tag19 deploy/shop-pooler-rw -c pgbouncer -- grep '^\* =' \
  /controller/configs/pgbouncer.ini
NAME             AGE    CLUSTER   TYPE   PHASE
shop-pooler-rw   4m9s   shop      ro     active
* = host=shop-ro

Fix

Point writing connections at the primary: in CloudNativePG the -rw service, for the pooler type: rw. The operator rewrites the PgBouncer configuration without replacing the pooler Pod.
kubectl patch pooler shop-pooler-rw -n tag19 --type merge -p '{"spec":{"type":"rw"}}'
kubectl exec -n tag19 deploy/shop-pooler-rw -c pgbouncer -- grep '^\* =' \
  /controller/configs/pgbouncer.ini
pooler.postgresql.cnpg.io/shop-pooler-rw patched
* = host=shop-rw

Don't: Trusting the name of a pooler or service. What is behind it shows in type, the EndpointSlices and pg_is_in_recovery().

▶
Break it yourself and find it – free
PostgreSQL hands-on, day 14: install CloudNativePG, create a first cluster, connect through -rw and -ro and watch a failover.
Go to day 14 →

FAQ

What is the difference between -ro and -r?

-ro only contains the replicas, -r all instances including the primary. Both are meant for readers.

How can I tell in SQL that I am on a replica?

SELECT pg_is_in_recovery() returns t on a standby and f on the primary.

Related error patterns

More