Fix pg_stat_wal_receiver to show CONNECTING status - Mailing list pgsql-hackers

From Chao Li
Subject Fix pg_stat_wal_receiver to show CONNECTING status
Date
Msg-id EF91FF76-1E2B-4F3B-9162-290B4DC517FF@gmail.com
Whole thread
Responses Re: Fix pg_stat_wal_receiver to show CONNECTING status
List pgsql-hackers
Hi,

I just tested "Add WALRCV_CONNECTING state to the WAL receiver” and found an issue.

Commit a36164e74 added the feature, and the commit message says:
```
...
the WAL receiver is ready to stream changes. This change is useful for
monitoring purposes, especially in environments with a high latency
where a connection could take some time to be established, giving some
room between the [re]start phase and the streaming activity.
```

However, I failed to see the CONNECTING status. To simulate a high-latency primary connection, I shut down the real
primaryserver and created a fake socket server: 
```
chaol@ChaodeMacBook-Air ~ % perl -MIO::Socket::INET -e '
  $s = IO::Socket::INET->new(
    LocalAddr => "127.0.0.1",
    LocalPort => 5432,
    Listen => 1,
    ReuseAddr => 1
  ) or die $!;
  $c = $s->accept;
  sleep 600;
'
```

Then pg_stat_wal_receiver only shows an empty result:
```
evantest=# SELECT * FROM pg_stat_wal_receiver;
 pid | status | receive_start_lsn | receive_start_tli | written_lsn | flushed_lsn | received_tli | last_msg_send_time |
last_msg_receipt_time| latest_end_lsn | latest_end_time | slot_name | sender_host | sender_port | conninfo 

-----+--------+-------------------+-------------------+-------------+-------------+--------------+--------------------+-----------------------+----------------+-----------------+-----------+-------------+-------------+----------
(0 rows)
```

I also tried restarting the standby server, and the result was the same.

The problem is that pg_stat_wal_receiver is gated by WalRcv->ready_to_display, and when the status is CONNECTING,
WalRcv->ready_to_displayis false. 

Given that the original commit message explicitly mentions “monitoring purposes”, I think hiding this status during the
connectingphase is a bug. I tried to fix it by showing only the PID and CONNECTING status when WalRcv->ready_to_display
isfalse, like this: 
```
evantest=# SELECT * FROM pg_stat_wal_receiver;
 pid  |   status   | receive_start_lsn | receive_start_tli | written_lsn | flushed_lsn | received_tli |
last_msg_send_time| last_msg_receipt_time | latest_end_lsn | latest_end_time | slot_name | sender_host | sender_port |
conninfo

------+------------+-------------------+-------------------+-------------+-------------+--------------+--------------------+-----------------------+----------------+-----------------+-----------+-------------+-------------+----------
 3256 | connecting |                   |                   |             |             |              |
  |                       |                |                 |           |             |             | 
(1 row)
```

See the attached patch for details.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/


Attachment

pgsql-hackers by date:

Previous
From: vellaipandiyan sm
Date:
Subject: Review observations for partial REFRESH MATERIALIZED VIEW patc
Next
From: Jim Jones
Date:
Subject: Re: PSQL - prevent describe listing tables that are already in listed schemas