postgresqlkubernetesdatabase-replication

How to perform read queries on read replica in postgres?


I deployed a PostgreSQL instance in k8s with a master pod and a single replica pod. I want to know how can I perform read queries on the read replica and write queries on the master? Currently, all the requests I send to the DB are served by the master (after connecting to the master). Do I need to explicitly connect to the read replica for queries to go there or is there some mechanism in Postgres which can route traffic to the read replica?


Solution

  • There is no reliable way to automatically redirect read requests to the standby, since in PostgreSQL you cannot reliably tell if an SQL statement writes or not (example: you can create a function that deletes rows and call it in a SEKECT statement). You will have to teach your application to connect to the standby to perform read requests.

    Note that horizontal scaling in that fashion comes with some problems: