PostgreSQL Replication – SELECT Query Slowing Down WAL Replay

postgresqlpostgresql-9.5

While running a SELECT query (bit large query ~1GB of data output which took ~15minutes to execute) in hot standby replication host, its halts the wal archiving process. Once the Query done with the execution, the replication takes place and started replicating. Is that normal behaviour of postgres or need to tune system runtime configuration?

I have tried to change work_mem=64MB to work_mem=96MB, but still I couldn't solve the delay.

(using PG 9.5)

Best Answer

Replication can be delayed if a query on the standby needs data the master has removed. The WAL from the master with the resource removal cannot be replayed until the standby query finishes, or the standby query must be cancelled.

You can:

  • Let the standby cancel the query after a time limit - see the max_standby_archive_delay and max_standby_stremaing_delay parameters; or

  • Enable hot_standby_feedback on the standby so the master won't remove resources the standby needs while its queries are running. This isn't perfect, conflicts that delay replication or cause query cancels can still happen when the standby just recently started up. But it helps.