PostgreSQL Source Code  git master
walreceiver.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * walreceiver.h
4  * Exports from replication/walreceiverfuncs.c.
5  *
6  * Portions Copyright (c) 2010-2020, PostgreSQL Global Development Group
7  *
8  * src/include/replication/walreceiver.h
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef _WALRECEIVER_H
13 #define _WALRECEIVER_H
14 
15 #include "access/xlog.h"
16 #include "access/xlogdefs.h"
17 #include "getaddrinfo.h" /* for NI_MAXHOST */
18 #include "pgtime.h"
19 #include "port/atomics.h"
21 #include "replication/walsender.h"
22 #include "storage/latch.h"
23 #include "storage/spin.h"
24 #include "utils/tuplestore.h"
25 
26 /* user-settable parameters */
28 extern int wal_receiver_timeout;
29 extern bool hot_standby_feedback;
30 
31 /*
32  * MAXCONNINFO: maximum size of a connection string.
33  *
34  * XXX: Should this move to pg_config_manual.h?
35  */
36 #define MAXCONNINFO 1024
37 
38 /* Can we allow the standby to accept replication connection from another standby? */
39 #define AllowCascadeReplication() (EnableHotStandby && max_wal_senders > 0)
40 
41 /*
42  * Values for WalRcv->walRcvState.
43  */
44 typedef enum
45 {
46  WALRCV_STOPPED, /* stopped and mustn't start up again */
47  WALRCV_STARTING, /* launched, but the process hasn't
48  * initialized yet */
49  WALRCV_STREAMING, /* walreceiver is streaming */
50  WALRCV_WAITING, /* stopped streaming, waiting for orders */
51  WALRCV_RESTARTING, /* asked to restart streaming */
52  WALRCV_STOPPING /* requested to stop, but still running */
53 } WalRcvState;
54 
55 /* Shared memory area for management of walreceiver process */
56 typedef struct
57 {
58  /*
59  * PID of currently active walreceiver process, its current state and
60  * start time (actually, the time at which it was requested to be
61  * started).
62  */
63  pid_t pid;
66 
67  /*
68  * receiveStart and receiveStartTLI indicate the first byte position and
69  * timeline that will be received. When startup process starts the
70  * walreceiver, it sets these to the point where it wants the streaming to
71  * begin.
72  */
75 
76  /*
77  * flushedUpto-1 is the last byte position that has already been received,
78  * and receivedTLI is the timeline it came from. At the first startup of
79  * walreceiver, these are set to receiveStart and receiveStartTLI. After
80  * that, walreceiver updates these whenever it flushes the received WAL to
81  * disk.
82  */
85 
86  /*
87  * latestChunkStart is the starting byte position of the current "batch"
88  * of received WAL. It's actually the same as the previous value of
89  * flushedUpto before the last flush to disk. Startup process can use
90  * this to detect whether it's keeping up or not.
91  */
93 
94  /*
95  * Time of send and receive of any message received.
96  */
99 
100  /*
101  * Latest reported end of WAL on the sender
102  */
105 
106  /*
107  * connection string; initially set to connect to the primary, and later
108  * clobbered to hide security-sensitive fields.
109  */
110  char conninfo[MAXCONNINFO];
111 
112  /*
113  * Host name (this can be a host name, an IP address, or a directory path)
114  * and port number of the active replication connection.
115  */
116  char sender_host[NI_MAXHOST];
118 
119  /*
120  * replication slot name; is also used for walreceiver to connect with the
121  * primary
122  */
123  char slotname[NAMEDATALEN];
124 
125  /*
126  * If it's a temporary replication slot, it needs to be recreated when
127  * connecting.
128  */
130 
131  /* set true once conninfo is ready to display (obfuscated pwds etc) */
133 
134  /*
135  * Latch used by startup process to wake up walreceiver after telling it
136  * where to start streaming (after setting receiveStart and
137  * receiveStartTLI), and also to tell it to send apply feedback to the
138  * primary whenever specially marked commit records are applied. This is
139  * normally mapped to procLatch when walreceiver is running.
140  */
142 
143  slock_t mutex; /* locks shared variables shown above */
144 
145  /*
146  * Like flushedUpto, but advanced after writing and before flushing,
147  * without the need to acquire the spin lock. Data can be read by another
148  * process up to this point, but shouldn't be used for data integrity
149  * purposes.
150  */
152 
153  /*
154  * force walreceiver reply? This doesn't need to be locked; memory
155  * barriers for ordering are sufficient. But we do need atomic fetch and
156  * store semantics, so use sig_atomic_t.
157  */
158  sig_atomic_t force_reply; /* used as a bool */
159 } WalRcvData;
160 
161 extern WalRcvData *WalRcv;
162 
163 typedef struct
164 {
165  bool logical; /* True if this is logical replication stream,
166  * false if physical stream. */
167  char *slotname; /* Name of the replication slot or NULL. */
168  XLogRecPtr startpoint; /* LSN of starting point. */
169 
170  union
171  {
172  struct
173  {
174  TimeLineID startpointTLI; /* Starting timeline */
175  } physical;
176  struct
177  {
178  uint32 proto_version; /* Logical protocol version */
179  List *publication_names; /* String list of publications */
180  } logical;
181  } proto;
183 
184 struct WalReceiverConn;
186 
187 /*
188  * Status of walreceiver query execution.
189  *
190  * We only define statuses that are currently used.
191  */
192 typedef enum
193 {
194  WALRCV_ERROR, /* There was error when executing the query. */
195  WALRCV_OK_COMMAND, /* Query executed utility or replication
196  * command. */
197  WALRCV_OK_TUPLES, /* Query returned tuples. */
198  WALRCV_OK_COPY_IN, /* Query started COPY FROM. */
199  WALRCV_OK_COPY_OUT, /* Query started COPY TO. */
200  WALRCV_OK_COPY_BOTH /* Query started COPY BOTH replication
201  * protocol. */
203 
204 /*
205  * Return value for walrcv_exec, returns the status of the execution and
206  * tuples if any.
207  */
208 typedef struct WalRcvExecResult
209 {
211  char *err;
215 
216 /* WAL receiver - libpqwalreceiver hooks */
217 
218 /*
219  * walrcv_connect_fn
220  *
221  * Establish connection to a cluster. 'logical' is true if the
222  * connection is logical, and false if the connection is physical.
223  * 'appname' is a name associated to the connection, to use for example
224  * with fallback_application_name or application_name. Returns the
225  * details about the connection established, as defined by
226  * WalReceiverConn for each WAL receiver module. On error, NULL is
227  * returned with 'err' including the error generated.
228  */
229 typedef WalReceiverConn *(*walrcv_connect_fn) (const char *conninfo,
230  bool logical,
231  const char *appname,
232  char **err);
233 
234 /*
235  * walrcv_check_conninfo_fn
236  *
237  * Parse and validate the connection string given as of 'conninfo'.
238  */
239 typedef void (*walrcv_check_conninfo_fn) (const char *conninfo);
240 
241 /*
242  * walrcv_get_conninfo_fn
243  *
244  * Returns a user-displayable conninfo string. Note that any
245  * security-sensitive fields should be obfuscated.
246  */
247 typedef char *(*walrcv_get_conninfo_fn) (WalReceiverConn *conn);
248 
249 /*
250  * walrcv_get_senderinfo_fn
251  *
252  * Provide information of the WAL sender this WAL receiver is connected
253  * to, as of 'sender_host' for the host of the sender and 'sender_port'
254  * for its port.
255  */
257  char **sender_host,
258  int *sender_port);
259 
260 /*
261  * walrcv_identify_system_fn
262  *
263  * Run IDENTIFY_SYSTEM on the cluster connected to and validate the
264  * identity of the cluster. Returns the system ID of the cluster
265  * connected to. 'primary_tli' is the timeline ID of the sender.
266  */
267 typedef char *(*walrcv_identify_system_fn) (WalReceiverConn *conn,
268  TimeLineID *primary_tli);
269 
270 /*
271  * walrcv_server_version_fn
272  *
273  * Returns the version number of the cluster connected to.
274  */
276 
277 /*
278  * walrcv_readtimelinehistoryfile_fn
279  *
280  * Fetch from cluster the timeline history file for timeline 'tli'.
281  * Returns the name of the timeline history file as of 'filename', its
282  * contents as of 'content' and its 'size'.
283  */
285  TimeLineID tli,
286  char **filename,
287  char **content,
288  int *size);
289 
290 /*
291  * walrcv_startstreaming_fn
292  *
293  * Start streaming WAL data from given streaming options. Returns true
294  * if the connection has switched successfully to copy-both mode and false
295  * if the server received the command and executed it successfully, but
296  * didn't switch to copy-mode.
297  */
300 
301 /*
302  * walrcv_endstreaming_fn
303  *
304  * Stop streaming of WAL data. Returns the next timeline ID of the cluster
305  * connected to in 'next_tli', or 0 if there was no report.
306  */
308  TimeLineID *next_tli);
309 
310 /*
311  * walrcv_receive_fn
312  *
313  * Receive a message available from the WAL stream. 'buffer' is a pointer
314  * to a buffer holding the message received. Returns the length of the data,
315  * 0 if no data is available yet ('wait_fd' is a socket descriptor which can
316  * be waited on before a retry), and -1 if the cluster ended the COPY.
317  */
319  char **buffer,
320  pgsocket *wait_fd);
321 
322 /*
323  * walrcv_send_fn
324  *
325  * Send a message of size 'nbytes' to the WAL stream with 'buffer' as
326  * contents.
327  */
329  const char *buffer,
330  int nbytes);
331 
332 /*
333  * walrcv_create_slot_fn
334  *
335  * Create a new replication slot named 'slotname'. 'temporary' defines
336  * if the slot is temporary. 'snapshot_action' defines the behavior wanted
337  * for an exported snapshot (see replication protocol for more details).
338  * 'lsn' includes the LSN position at which the created slot became
339  * consistent. Returns the name of the exported snapshot for a logical
340  * slot, or NULL for a physical slot.
341  */
342 typedef char *(*walrcv_create_slot_fn) (WalReceiverConn *conn,
343  const char *slotname,
344  bool temporary,
345  CRSSnapshotAction snapshot_action,
346  XLogRecPtr *lsn);
347 
348 /*
349  * walrcv_get_backend_pid_fn
350  *
351  * Returns the PID of the remote backend process.
352  */
354 
355 /*
356  * walrcv_exec_fn
357  *
358  * Send generic queries (and commands) to the remote cluster. 'nRetTypes'
359  * is the expected number of returned attributes, and 'retTypes' an array
360  * including their type OIDs. Returns the status of the execution and
361  * tuples if any.
362  */
363 typedef WalRcvExecResult *(*walrcv_exec_fn) (WalReceiverConn *conn,
364  const char *query,
365  const int nRetTypes,
366  const Oid *retTypes);
367 
368 /*
369  * walrcv_disconnect_fn
370  *
371  * Disconnect with the cluster.
372  */
374 
376 {
393 
395 
396 #define walrcv_connect(conninfo, logical, appname, err) \
397  WalReceiverFunctions->walrcv_connect(conninfo, logical, appname, err)
398 #define walrcv_check_conninfo(conninfo) \
399  WalReceiverFunctions->walrcv_check_conninfo(conninfo)
400 #define walrcv_get_conninfo(conn) \
401  WalReceiverFunctions->walrcv_get_conninfo(conn)
402 #define walrcv_get_senderinfo(conn, sender_host, sender_port) \
403  WalReceiverFunctions->walrcv_get_senderinfo(conn, sender_host, sender_port)
404 #define walrcv_identify_system(conn, primary_tli) \
405  WalReceiverFunctions->walrcv_identify_system(conn, primary_tli)
406 #define walrcv_server_version(conn) \
407  WalReceiverFunctions->walrcv_server_version(conn)
408 #define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size) \
409  WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
410 #define walrcv_startstreaming(conn, options) \
411  WalReceiverFunctions->walrcv_startstreaming(conn, options)
412 #define walrcv_endstreaming(conn, next_tli) \
413  WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)
414 #define walrcv_receive(conn, buffer, wait_fd) \
415  WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)
416 #define walrcv_send(conn, buffer, nbytes) \
417  WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)
418 #define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn) \
419  WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
420 #define walrcv_get_backend_pid(conn) \
421  WalReceiverFunctions->walrcv_get_backend_pid(conn)
422 #define walrcv_exec(conn, exec, nRetTypes, retTypes) \
423  WalReceiverFunctions->walrcv_exec(conn, exec, nRetTypes, retTypes)
424 #define walrcv_disconnect(conn) \
425  WalReceiverFunctions->walrcv_disconnect(conn)
426 
427 static inline void
429 {
430  if (!walres)
431  return;
432 
433  if (walres->err)
434  pfree(walres->err);
435 
436  if (walres->tuplestore)
437  tuplestore_end(walres->tuplestore);
438 
439  if (walres->tupledesc)
440  FreeTupleDesc(walres->tupledesc);
441 
442  pfree(walres);
443 }
444 
445 /* prototypes for functions in walreceiver.c */
446 extern void WalReceiverMain(void) pg_attribute_noreturn();
447 extern void ProcessWalRcvInterrupts(void);
448 
449 /* prototypes for functions in walreceiverfuncs.c */
450 extern Size WalRcvShmemSize(void);
451 extern void WalRcvShmemInit(void);
452 extern void ShutdownWalRcv(void);
453 extern bool WalRcvStreaming(void);
454 extern bool WalRcvRunning(void);
455 extern void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr,
456  const char *conninfo, const char *slotname,
457  bool create_temp_slot);
459 extern XLogRecPtr GetWalRcvWriteRecPtr(void);
460 extern int GetReplicationApplyDelay(void);
461 extern int GetReplicationTransferLatency(void);
462 extern void WalRcvForceReply(void);
463 
464 #endif /* _WALRECEIVER_H */
walrcv_identify_system_fn walrcv_identify_system
Definition: walreceiver.h:381
int sender_port
Definition: walreceiver.h:117
int slock_t
Definition: s_lock.h:934
#define pg_attribute_noreturn()
Definition: c.h:145
bool hot_standby_feedback
Definition: walreceiver.c:90
bool WalRcvStreaming(void)
XLogRecPtr GetWalRcvWriteRecPtr(void)
walrcv_server_version_fn walrcv_server_version
Definition: walreceiver.h:382
uint32 TimeLineID
Definition: xlogdefs.h:52
int64 pg_time_t
Definition: pgtime.h:23
void ShutdownWalRcv(void)
slock_t mutex
Definition: walreceiver.h:143
walrcv_startstreaming_fn walrcv_startstreaming
Definition: walreceiver.h:384
PGDLLIMPORT WalReceiverFunctionsType * WalReceiverFunctions
Definition: walreceiver.c:94
int64 TimestampTz
Definition: timestamp.h:39
sig_atomic_t force_reply
Definition: walreceiver.h:158
WalRcvState walRcvState
Definition: walreceiver.h:64
WalReceiverConn *(* walrcv_connect_fn)(const char *conninfo, bool logical, const char *appname, char **err)
Definition: walreceiver.h:229
void(* walrcv_disconnect_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:373
WalRcvExecStatus
Definition: walreceiver.h:192
TimeLineID receivedTLI
Definition: walreceiver.h:84
void ProcessWalRcvInterrupts(void)
Definition: walreceiver.c:157
void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo, const char *slotname, bool create_temp_slot)
unsigned int Oid
Definition: postgres_ext.h:31
TimestampTz lastMsgReceiptTime
Definition: walreceiver.h:98
TimestampTz lastMsgSendTime
Definition: walreceiver.h:97
pg_time_t startTime
Definition: walreceiver.h:65
int GetReplicationApplyDelay(void)
#define PGDLLIMPORT
Definition: c.h:1279
void WalRcvShmemInit(void)
WalRcvState
Definition: walreceiver.h:44
walrcv_receive_fn walrcv_receive
Definition: walreceiver.h:386
pg_atomic_uint64 writtenUpto
Definition: walreceiver.h:151
#define NAMEDATALEN
WalRcvData * WalRcv
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:428
void pfree(void *pointer)
Definition: mcxt.c:1056
#define NI_MAXHOST
Definition: getaddrinfo.h:88
struct WalRcvExecResult WalRcvExecResult
TupleDesc tupledesc
Definition: walreceiver.h:213
walrcv_get_backend_pid_fn walrcv_get_backend_pid
Definition: walreceiver.h:389
void WalRcvForceReply(void)
Definition: walreceiver.c:1298
PGconn * conn
Definition: streamutil.c:54
pid_t pid
Definition: walreceiver.h:63
#define MAXCONNINFO
Definition: walreceiver.h:36
XLogRecPtr latestChunkStart
Definition: walreceiver.h:92
int wal_receiver_timeout
Definition: walreceiver.c:89
int GetReplicationTransferLatency(void)
Definition: latch.h:110
void(* walrcv_send_fn)(WalReceiverConn *conn, const char *buffer, int nbytes)
Definition: walreceiver.h:328
walrcv_send_fn walrcv_send
Definition: walreceiver.h:387
char *(* walrcv_create_slot_fn)(WalReceiverConn *conn, const char *slotname, bool temporary, CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)
Definition: walreceiver.h:342
XLogRecPtr startpoint
Definition: walreceiver.h:168
unsigned int uint32
Definition: c.h:367
int pgsocket
Definition: port.h:31
pid_t(* walrcv_get_backend_pid_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:353
TimeLineID receiveStartTLI
Definition: walreceiver.h:74
void(* walrcv_get_senderinfo_fn)(WalReceiverConn *conn, char **sender_host, int *sender_port)
Definition: walreceiver.h:256
Latch * latch
Definition: walreceiver.h:141
static char ** options
WalRcvExecResult *(* walrcv_exec_fn)(WalReceiverConn *conn, const char *query, const int nRetTypes, const Oid *retTypes)
Definition: walreceiver.h:363
void(* walrcv_check_conninfo_fn)(const char *conninfo)
Definition: walreceiver.h:239
static TimeLineID receiveTLI
Definition: xlog.c:213
void(* walrcv_readtimelinehistoryfile_fn)(WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *size)
Definition: walreceiver.h:284
int(* walrcv_receive_fn)(WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)
Definition: walreceiver.h:318
bool ready_to_display
Definition: walreceiver.h:132
bool(* walrcv_startstreaming_fn)(WalReceiverConn *conn, const WalRcvStreamOptions *options)
Definition: walreceiver.h:298
walrcv_get_conninfo_fn walrcv_get_conninfo
Definition: walreceiver.h:379
walrcv_disconnect_fn walrcv_disconnect
Definition: walreceiver.h:391
TimestampTz latestWalEndTime
Definition: walreceiver.h:104
walrcv_endstreaming_fn walrcv_endstreaming
Definition: walreceiver.h:385
int wal_receiver_status_interval
Definition: walreceiver.c:88
XLogRecPtr latestWalEnd
Definition: walreceiver.h:103
Tuplestorestate * tuplestore
Definition: walreceiver.h:212
walrcv_readtimelinehistoryfile_fn walrcv_readtimelinehistoryfile
Definition: walreceiver.h:383
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalRcvExecStatus status
Definition: walreceiver.h:210
walrcv_get_senderinfo_fn walrcv_get_senderinfo
Definition: walreceiver.h:380
void WalReceiverMain(void) pg_attribute_noreturn()
Definition: walreceiver.c:178
void(* walrcv_endstreaming_fn)(WalReceiverConn *conn, TimeLineID *next_tli)
Definition: walreceiver.h:307
size_t Size
Definition: c.h:466
Size WalRcvShmemSize(void)
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
walrcv_exec_fn walrcv_exec
Definition: walreceiver.h:390
struct WalReceiverFunctionsType WalReceiverFunctionsType
bool is_temp_slot
Definition: walreceiver.h:129
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:313
static char * filename
Definition: pg_dumpall.c:90
walrcv_create_slot_fn walrcv_create_slot
Definition: walreceiver.h:388
bool WalRcvRunning(void)
CRSSnapshotAction
Definition: walsender.h:20
TimeLineID startpointTLI
Definition: walreceiver.h:174
walrcv_check_conninfo_fn walrcv_check_conninfo
Definition: walreceiver.h:378
XLogRecPtr receiveStart
Definition: walreceiver.h:73
XLogRecPtr flushedUpto
Definition: walreceiver.h:83
char *(* walrcv_get_conninfo_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:247
int(* walrcv_server_version_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:275
walrcv_connect_fn walrcv_connect
Definition: walreceiver.h:377
Definition: pg_list.h:50
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
unsigned char bool
Definition: c.h:317
char *(* walrcv_identify_system_fn)(WalReceiverConn *conn, TimeLineID *primary_tli)
Definition: walreceiver.h:267