Postgresql – How to streaming replication be kept lagged by a certain ‘delay’ time

postgresqlpostgresql-9.3replication

Postgres has had streaming replication since 9.0 and since 9.1 it is possible to pause and resume the application of the streamed WAL records using pg_xlog_replay_pause() and pg_xlog_replay_resume(). As far as I can tell, there is no built-in way of keeping the application of WAL deliberately lagged by a specific time period (eg 24h).

Have I missed a built-in way of achieving this and if not, how should I approach doing it some other way?

Best Answer

Streaming replication cannot do this (but see Craig Ringer's comment above about a possibly upcoming feature).

The good news is, if you make the standby getting the WAL files and apply them, you can apply an arbitrary delay. We push the WALs to a NFS volume and the restore command copies them from there to the local WAL dir, checking if the files are old enough. Then the normal restore process applies them as usual.

Of course, the delay will match the preset value only approximately, as a single WAL file may contain changes from quite different time intervals, depending on the master's workload.

What you propose with pausing the restore process depending on the value returned by pg_last_xact_replay_timestamp() may work if run from a scheduled job (probably you want to schedule it to run quite frequently). So far I haven't seen such a setup and am wondering if it can be a production-grade solution.