Thank you Craig for your suggestion.

I followed the clue and spent the whole day digging into the code.

Finally I figured out how the WAL receiver exits and restarts.

Question-1. How the WAL receiver process exits
When the network connection is broken, WAL receiver couldn't communicate
with the WAL sender.  For a long time (timer:wal_receiver_timeout), the WAL
receiver  gets nothing from the WAL sender, the WAL receiver process exits
by calling "ereport(ERROR,...)".

Calling ereport(ERROR,...) causes the current process exit, but calling
ereport(LOG,...) doesn't.

            len = walrcv_receive(NAPTIME_PER_CYCLE, &buf);
            if (len != 0)
                    if (wal_receiver_timeout > 0)
                            if (now >= timeout)
                                        (errmsg("terminating walreceiver
due to timeout")));

Question-2. How  WAL receiver process starts again

At the Standby side, the startup process is responsible for recovery
processing. If streaming replication is configured and the startup process
finds that the WAL receiver process is not running, it notify the
Postmaster to start the WAL receiver process.Note: This is also how the WAL
receiver process starts for the first time!

(1) startup process notify Postmaster to start the WAL receiver by sending

        if (launch)
                kill(PostmasterPid, SIGUSR1);

(2) Postmaster gets SIGUSR1 and starts the WAL receiver process.

        WalReceiverPID = StartWalReceiver();

Please let me know if my understanding is incorrect.

Rui Hai

Reply via email to