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 primary server 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_display is false.

Given that the original commit message explicitly mentions “monitoring 
purposes”, I think hiding this status during the connecting phase is a bug. I 
tried to fix it by showing only the PID and CONNECTING status when 
WalRcv->ready_to_display is false, 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: v1-0001-Fix-pg_stat_wal_receiver-to-show-CONNECTING-statu.patch
Description: Binary data

Reply via email to