]> granicus.if.org Git - postgresql/blob - src/backend/replication/README
Adjust signature of walrcv_receive hook.
[postgresql] / src / backend / replication / README
1 src/backend/replication/README
2
3 Walreceiver - libpqwalreceiver API
4 ----------------------------------
5
6 The transport-specific part of walreceiver, responsible for connecting to
7 the primary server, receiving WAL files and sending messages, is loaded
8 dynamically to avoid having to link the main server binary with libpq.
9 The dynamically loaded module is in libpqwalreceiver subdirectory.
10
11 The dynamically loaded module implements four functions:
12
13
14 bool walrcv_connect(char *conninfo, XLogRecPtr startpoint)
15
16 Establish connection to the primary, and starts streaming from 'startpoint'.
17 Returns true on success.
18
19 int walrcv_receive(char **buffer, pgsocket *wait_fd)
20
21 Retrieve any message available without blocking through the
22 connection.  If a message was successfully read, returns its
23 length. If the connection is closed, returns -1.  Otherwise returns 0
24 to indicate that no data is available, and sets *wait_fd to a socket
25 descriptor which can be waited on before trying again.  On success, a
26 pointer to the message payload is stored in *buffer. The returned
27 buffer is valid until the next call to walrcv_* functions, and the
28 caller should not attempt to free it.
29
30 void walrcv_send(const char *buffer, int nbytes)
31
32 Send a message to XLOG stream.
33
34 void walrcv_disconnect(void);
35
36 Disconnect.
37
38
39 This API should be considered internal at the moment, but we could open it
40 up for 3rd party replacements of libpqwalreceiver in the future, allowing
41 pluggable methods for receiving WAL.
42
43 Walreceiver IPC
44 ---------------
45
46 When the WAL replay in startup process has reached the end of archived WAL,
47 restorable using restore_command, it starts up the walreceiver process
48 to fetch more WAL (if streaming replication is configured).
49
50 Walreceiver is a postmaster subprocess, so the startup process can't fork it
51 directly. Instead, it sends a signal to postmaster, asking postmaster to launch
52 it. Before that, however, startup process fills in WalRcvData->conninfo
53 and WalRcvData->slotname, and initializes the starting point in
54 WalRcvData->receiveStart.
55
56 As walreceiver receives WAL from the master server, and writes and flushes
57 it to disk (in pg_xlog), it updates WalRcvData->receivedUpto and signals
58 the startup process to know how far WAL replay can advance.
59
60 Walreceiver sends information about replication progress to the master server
61 whenever it either writes or flushes new WAL, or the specified interval elapses.
62 This is used for reporting purpose.
63
64 Walsender IPC
65 -------------
66
67 At shutdown, postmaster handles walsender processes differently from regular
68 backends. It waits for regular backends to die before writing the
69 shutdown checkpoint and terminating pgarch and other auxiliary processes, but
70 that's not desirable for walsenders, because we want the standby servers to
71 receive all the WAL, including the shutdown checkpoint, before the master
72 is shut down. Therefore postmaster treats walsenders like the pgarch process,
73 and instructs them to terminate at PM_SHUTDOWN_2 phase, after all regular
74 backends have died and checkpointer has issued the shutdown checkpoint.
75
76 When postmaster accepts a connection, it immediately forks a new process
77 to handle the handshake and authentication, and the process initializes to
78 become a backend. Postmaster doesn't know if the process becomes a regular
79 backend or a walsender process at that time - that's indicated in the
80 connection handshake - so we need some extra signaling to let postmaster
81 identify walsender processes.
82
83 When walsender process starts up, it marks itself as a walsender process in
84 the PMSignal array. That way postmaster can tell it apart from regular
85 backends.
86
87 Note that no big harm is done if postmaster thinks that a walsender is a
88 regular backend; it will just terminate the walsender earlier in the shutdown
89 phase. A walsender will look like a regular backend until it's done with the
90 initialization and has marked itself in PMSignal array, and at process
91 termination, after unmarking the PMSignal slot.
92
93 Each walsender allocates an entry from the WalSndCtl array, and tracks
94 information about replication progress. User can monitor them via
95 statistics views.
96
97
98 Walsender - walreceiver protocol
99 --------------------------------
100
101 See manual.