🐘 PostgreSQL Intermediate

What is streaming replication in PostgreSQL?

Answer

Streaming replication is PostgreSQL's built-in mechanism for maintaining standby servers that are continuously updated from a primary. The primary streams WAL records to standbys in real time. Setup: configure wal_level = replica, max_wal_senders on primary; use pg_basebackup to create the initial standby; configure primary_conninfo on standby. Standbys are by default hot standbys — they accept read-only queries, offloading reads from the primary. Synchronous replication: primary waits for standby to confirm WAL receipt before acknowledging commit — zero data loss but higher latency. Asynchronous replication: primary doesn't wait — potential for small lag but better performance. Monitor replication lag: SELECT now() - pg_last_xact_replay_timestamp() AS replication_lag FROM pg_stat_replication;.