Sub topic 6
Running Read-Only Queries on a PostgreSQL Standby
Assume you know basic PostgreSQL administration, can use psql, and already have streaming replication configured. The primary is accepting writes. The standby is receiving and replaying WAL records. This chapter focuses on using that standby for read-only work.
How a hot standby serves queries
A hot standby is a standby server that accepts connections while it replays WAL. Set hot_standby = on in the standby's configuration:
# postgresql.conf on the standby
hot_standby = onWhen recovery is active, PostgreSQL applies the primary's changes locally. A client can run SELECT statements against the data that has already been replayed.
The standby can be slightly behind the primary. A query therefore might not see a row that was committed on the primary a moment ago. The query sees a consistent snapshot of the standby's local state.
What the standby allows
A connection to a hot standby can run ordinary read queries:
SELECT customer_id, total
FROM invoices
WHERE status = 'open'
ORDER BY total DESC
LIMIT 10;
SELECT count(*) AS open_invoices
FROM invoices
WHERE status = 'open';The standby does not accept changes to replicated data. These statements fail while the server is in recovery:
INSERT INTO invoices (customer_id, total, status)
VALUES (42, 125.00, 'open');
UPDATE invoices
SET status = 'paid'
WHERE invoice_id = 1001;
CREATE INDEX invoices_status_idx ON invoices (status);This restriction protects replay. If a standby allowed local changes to replicated tables, its data would no longer match the primary.
| Operation | Hot standby behavior |
|---|---|
| `SELECT` from replicated tables | Allowed |
| `INSERT`, `UPDATE`, or `DELETE` on replicated tables | Rejected |
| DDL such as `CREATE TABLE` or `CREATE INDEX` | Rejected |
| Write transactions on the primary | Unaffected |
You can check whether the current server is still a standby:
SELECT pg_is_in_recovery();The function returns true during recovery and false after the server has been promoted. Applications should use this check carefully. Promotion changes the server's role, so a connection pool must also enforce which endpoints are allowed for reads and writes.
Choosing standby workloads
Standbys are useful for workloads that tolerate replication delay, such as:
- Reporting queries
- Search pages that do not require the newest commit
- Dashboards
- Read-only analytics
- Backup or inspection tasks
Avoid sending latency-sensitive reads to a standby when the result must include the latest primary commit. Route those reads to the primary, or track replay position and wait until the standby has caught up.
Long-running queries can also conflict with WAL replay. PostgreSQL may cancel a query when replay needs to remove or change rows that the query still needs. The next chapter covers these conflicts and their settings.
Exercise
Why does a hot standby reject an UPDATE even though it can execute SELECT statements?
Show answer
WAL replay must keep the standby's replicated data consistent with the primary. A local UPDATE would create a change that is not part of the primary's WAL stream, so PostgreSQL rejects it.