46

Can someone suggest the steps to check pgsql replication status and how to identify if the replication is not happening properly?

We use streaming replication with pgsql9.0 and pgsql9.4

1

3 Answers 3

67

I use following SQL queries to check status on Postgres v11 usually.

On master:

select * from pg_stat_replication;

On replica (streaming replication in my case):

select * from pg_stat_wal_receiver;
3
  • 11
    For vertical output replace ; with \x\g\x.
    – x-yuri
    Oct 24, 2020 at 6:38
  • 4
    Could you explain what we are supposed to see if everything is going well or if something is wrong ? sync_state : sync on the primary is a good sign ? Mar 8, 2022 at 9:48
  • 1
    sync_state refers to how the replication was configured. state is the field to check in pg_stat_replication.
    – gerardw
    Apr 21, 2022 at 8:22
29

On your master, pg_stat_replication provides data about ongoing replication:

select client_addr, state, sent_location, write_location,
        flush_location, replay_location from pg_stat_replication;

On postgresql v10:

select client_addr, state, sent_lsn, write_lsn,
    flush_lsn, replay_lsn from pg_stat_replication;
3
  • 3
    Ok, what's wrong then if the above query returns 0 rows?
    – gvasquez
    Jul 17, 2017 at 15:58
  • 1
    0 rows means you have no replication configured at your server. When configured correctly should get a row(s) but that doesn't mean you have replication either as the slave may not be connecting (check 'state' column)
    – Reinsbrain
    Jul 18, 2017 at 0:28
  • mmmm...we had replication working fine and it failed, now it shows no rows, please see: dba.stackexchange.com/questions/180062/…
    – gvasquez
    Jul 18, 2017 at 19:22
26

Show replication status in PostgreSQL

on server

postgres=# select usename,application_name,client_addr,backend_start,state,sync_state from pg_stat_replication ;

usename   | application_name |  client_addr   |         backend_start         |   state   | sync_state 
------------+------------------+----------------+-------------------------------+-----------+------------
replicator | walreceiver      | 192.168.10.132 | 2018-07-06 06:12:20.786918+03 | streaming | async
(1 row)

on client

postgres=# select pg_is_in_recovery();
 pg_is_in_recovery 
-------------------
 t
 (1 row)


postgres=# select pg_last_xlog_receive_location();
 pg_last_xlog_receive_location 
-------------------------------
 0/540C1DB8


postgres=# select pg_last_xlog_replay_location();
 pg_last_xlog_replay_location 
------------------------------
 0/540C1DB8
 (1 row)

postgres=#    SELECT CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location()
                  THEN 0
                ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())
              END AS log_delay;
 log_delay 
-----------
 0
 (1 row)
3
  • 7
    on postgres 10, this is not working entirely but SELECT pg_last_xact_replay_timestamp(); gives useful information!
    – user39950
    Jul 12, 2018 at 9:06
  • 2
    Compare master and slave statuses in a single query: select pg_last_xlog_receive_location(), pg_last_xlog_replay_location(), pg_last_xact_replay_timestamp(), CASE WHEN pg_last_xlog_receive_location() = pg_last_xlog_replay_location() THEN 0 ELSE EXTRACT(EPOCH FROM now() - pg_last_xact_replay_timestamp()) END AS log_delay; Apr 14, 2020 at 12:04
  • To check master IP from slave, dba.stackexchange.com/a/45062/123063 May 27, 2020 at 5:15

Your Answer

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

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