0

I have a setup with asynchronous streaming replication so that PSQL1 is primary and PSQL2 is hot standby. Since PSQL2 is running in hot standby mode I can do run queries against it.

Is it possible on PSQL2 to tell whether PSQL1 is still active or already shutdown? I tried select pg_is_in_recovery(); but it returns true regardless whether PSQL1 is running or not.

1
  • 1
    Be warned that that would not be a good way to determine if you should fail over. That is way more complicated. Commented Feb 1, 2021 at 7:42

1 Answer 1

3

For streaming replication, you can check the pg_stat_wal_receiver view. There will be one line here when wal receiver is streaming.

Another question is why you need such information. For monitoring purposes, the amount of replication delay is much more important. Async replica may be significant lagged, but primary and wal receiver still alive (e.g. while reach the network bandwidth limit). Usually on hot standby:

select now() - pg_last_xact_replay_timestamp();

and pg_stat_replication on primary.

1
  • Thank's - this is exactly what I'am looking for. pg_last_xact_replay_timestamp() is a good hint too. However I discovered that now() - pg_last_xact_replay_timestamp() can yield a negative interval in case the replay happens just when the query is executed. Therefore I changed it to select -(pg_last_xact_replay_timestamp() - now())
    – Udo
    Commented Feb 1, 2021 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.