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  bool binary; /* Ask publisher to use binary */
181  bool streaming; /* Streaming of large transactions */
182  } logical;
183  } proto;
185 
186 struct WalReceiverConn;
188 
189 /*
190  * Status of walreceiver query execution.
191  *
192  * We only define statuses that are currently used.
193  */
194 typedef enum
195 {
196  WALRCV_ERROR, /* There was error when executing the query. */
197  WALRCV_OK_COMMAND, /* Query executed utility or replication
198  * command. */
199  WALRCV_OK_TUPLES, /* Query returned tuples. */
200  WALRCV_OK_COPY_IN, /* Query started COPY FROM. */
201  WALRCV_OK_COPY_OUT, /* Query started COPY TO. */
202  WALRCV_OK_COPY_BOTH /* Query started COPY BOTH replication
203  * protocol. */
205 
206 /*
207  * Return value for walrcv_exec, returns the status of the execution and
208  * tuples if any.
209  */
210 typedef struct WalRcvExecResult
211 {
213  char *err;
217 
218 /* WAL receiver - libpqwalreceiver hooks */
219 
220 /*
221  * walrcv_connect_fn
222  *
223  * Establish connection to a cluster. 'logical' is true if the
224  * connection is logical, and false if the connection is physical.
225  * 'appname' is a name associated to the connection, to use for example
226  * with fallback_application_name or application_name. Returns the
227  * details about the connection established, as defined by
228  * WalReceiverConn for each WAL receiver module. On error, NULL is
229  * returned with 'err' including the error generated.
230  */
231 typedef WalReceiverConn *(*walrcv_connect_fn) (const char *conninfo,
232  bool logical,
233  const char *appname,
234  char **err);
235 
236 /*
237  * walrcv_check_conninfo_fn
238  *
239  * Parse and validate the connection string given as of 'conninfo'.
240  */
241 typedef void (*walrcv_check_conninfo_fn) (const char *conninfo);
242 
243 /*
244  * walrcv_get_conninfo_fn
245  *
246  * Returns a user-displayable conninfo string. Note that any
247  * security-sensitive fields should be obfuscated.
248  */
249 typedef char *(*walrcv_get_conninfo_fn) (WalReceiverConn *conn);
250 
251 /*
252  * walrcv_get_senderinfo_fn
253  *
254  * Provide information of the WAL sender this WAL receiver is connected
255  * to, as of 'sender_host' for the host of the sender and 'sender_port'
256  * for its port.
257  */
259  char **sender_host,
260  int *sender_port);
261 
262 /*
263  * walrcv_identify_system_fn
264  *
265  * Run IDENTIFY_SYSTEM on the cluster connected to and validate the
266  * identity of the cluster. Returns the system ID of the cluster
267  * connected to. 'primary_tli' is the timeline ID of the sender.
268  */
269 typedef char *(*walrcv_identify_system_fn) (WalReceiverConn *conn,
270  TimeLineID *primary_tli);
271 
272 /*
273  * walrcv_server_version_fn
274  *
275  * Returns the version number of the cluster connected to.
276  */
278 
279 /*
280  * walrcv_readtimelinehistoryfile_fn
281  *
282  * Fetch from cluster the timeline history file for timeline 'tli'.
283  * Returns the name of the timeline history file as of 'filename', its
284  * contents as of 'content' and its 'size'.
285  */
287  TimeLineID tli,
288  char **filename,
289  char **content,
290  int *size);
291 
292 /*
293  * walrcv_startstreaming_fn
294  *
295  * Start streaming WAL data from given streaming options. Returns true
296  * if the connection has switched successfully to copy-both mode and false
297  * if the server received the command and executed it successfully, but
298  * didn't switch to copy-mode.
299  */
302 
303 /*
304  * walrcv_endstreaming_fn
305  *
306  * Stop streaming of WAL data. Returns the next timeline ID of the cluster
307  * connected to in 'next_tli', or 0 if there was no report.
308  */
310  TimeLineID *next_tli);
311 
312 /*
313  * walrcv_receive_fn
314  *
315  * Receive a message available from the WAL stream. 'buffer' is a pointer
316  * to a buffer holding the message received. Returns the length of the data,
317  * 0 if no data is available yet ('wait_fd' is a socket descriptor which can
318  * be waited on before a retry), and -1 if the cluster ended the COPY.
319  */
321  char **buffer,
322  pgsocket *wait_fd);
323 
324 /*
325  * walrcv_send_fn
326  *
327  * Send a message of size 'nbytes' to the WAL stream with 'buffer' as
328  * contents.
329  */
331  const char *buffer,
332  int nbytes);
333 
334 /*
335  * walrcv_create_slot_fn
336  *
337  * Create a new replication slot named 'slotname'. 'temporary' defines
338  * if the slot is temporary. 'snapshot_action' defines the behavior wanted
339  * for an exported snapshot (see replication protocol for more details).
340  * 'lsn' includes the LSN position at which the created slot became
341  * consistent. Returns the name of the exported snapshot for a logical
342  * slot, or NULL for a physical slot.
343  */
344 typedef char *(*walrcv_create_slot_fn) (WalReceiverConn *conn,
345  const char *slotname,
346  bool temporary,
347  CRSSnapshotAction snapshot_action,
348  XLogRecPtr *lsn);
349 
350 /*
351  * walrcv_get_backend_pid_fn
352  *
353  * Returns the PID of the remote backend process.
354  */
356 
357 /*
358  * walrcv_exec_fn
359  *
360  * Send generic queries (and commands) to the remote cluster. 'nRetTypes'
361  * is the expected number of returned attributes, and 'retTypes' an array
362  * including their type OIDs. Returns the status of the execution and
363  * tuples if any.
364  */
365 typedef WalRcvExecResult *(*walrcv_exec_fn) (WalReceiverConn *conn,
366  const char *query,
367  const int nRetTypes,
368  const Oid *retTypes);
369 
370 /*
371  * walrcv_disconnect_fn
372  *
373  * Disconnect with the cluster.
374  */
376 
378 {
395 
397 
398 #define walrcv_connect(conninfo, logical, appname, err) \
399  WalReceiverFunctions->walrcv_connect(conninfo, logical, appname, err)
400 #define walrcv_check_conninfo(conninfo) \
401  WalReceiverFunctions->walrcv_check_conninfo(conninfo)
402 #define walrcv_get_conninfo(conn) \
403  WalReceiverFunctions->walrcv_get_conninfo(conn)
404 #define walrcv_get_senderinfo(conn, sender_host, sender_port) \
405  WalReceiverFunctions->walrcv_get_senderinfo(conn, sender_host, sender_port)
406 #define walrcv_identify_system(conn, primary_tli) \
407  WalReceiverFunctions->walrcv_identify_system(conn, primary_tli)
408 #define walrcv_server_version(conn) \
409  WalReceiverFunctions->walrcv_server_version(conn)
410 #define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size) \
411  WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
412 #define walrcv_startstreaming(conn, options) \
413  WalReceiverFunctions->walrcv_startstreaming(conn, options)
414 #define walrcv_endstreaming(conn, next_tli) \
415  WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)
416 #define walrcv_receive(conn, buffer, wait_fd) \
417  WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)
418 #define walrcv_send(conn, buffer, nbytes) \
419  WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)
420 #define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn) \
421  WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
422 #define walrcv_get_backend_pid(conn) \
423  WalReceiverFunctions->walrcv_get_backend_pid(conn)
424 #define walrcv_exec(conn, exec, nRetTypes, retTypes) \
425  WalReceiverFunctions->walrcv_exec(conn, exec, nRetTypes, retTypes)
426 #define walrcv_disconnect(conn) \
427  WalReceiverFunctions->walrcv_disconnect(conn)
428 
429 static inline void
431 {
432  if (!walres)
433  return;
434 
435  if (walres->err)
436  pfree(walres->err);
437 
438  if (walres->tuplestore)
439  tuplestore_end(walres->tuplestore);
440 
441  if (walres->tupledesc)
442  FreeTupleDesc(walres->tupledesc);
443 
444  pfree(walres);
445 }
446 
447 /* prototypes for functions in walreceiver.c */
448 extern void WalReceiverMain(void) pg_attribute_noreturn();
449 extern void ProcessWalRcvInterrupts(void);
450 
451 /* prototypes for functions in walreceiverfuncs.c */
452 extern Size WalRcvShmemSize(void);
453 extern void WalRcvShmemInit(void);
454 extern void ShutdownWalRcv(void);
455 extern bool WalRcvStreaming(void);
456 extern bool WalRcvRunning(void);
457 extern void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr,
458  const char *conninfo, const char *slotname,
459  bool create_temp_slot);
461 extern XLogRecPtr GetWalRcvWriteRecPtr(void);
462 extern int GetReplicationApplyDelay(void);
463 extern int GetReplicationTransferLatency(void);
464 extern void WalRcvForceReply(void);
465 
466 #endif /* _WALRECEIVER_H */
walrcv_identify_system_fn walrcv_identify_system
Definition: walreceiver.h:383
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:384
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:386
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:231
void(* walrcv_disconnect_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:375
WalRcvExecStatus
Definition: walreceiver.h:194
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:1257
void WalRcvShmemInit(void)
WalRcvState
Definition: walreceiver.h:44
walrcv_receive_fn walrcv_receive
Definition: walreceiver.h:388
pg_atomic_uint64 writtenUpto
Definition: walreceiver.h:151
#define NAMEDATALEN
WalRcvData * WalRcv
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:430
void pfree(void *pointer)
Definition: mcxt.c:1057
#define NI_MAXHOST
Definition: getaddrinfo.h:88
struct WalRcvExecResult WalRcvExecResult
TupleDesc tupledesc
Definition: walreceiver.h:215
walrcv_get_backend_pid_fn walrcv_get_backend_pid
Definition: walreceiver.h:391
void WalRcvForceReply(void)
Definition: walreceiver.c:1280
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:330
walrcv_send_fn walrcv_send
Definition: walreceiver.h:389
char *(* walrcv_create_slot_fn)(WalReceiverConn *conn, const char *slotname, bool temporary, CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)
Definition: walreceiver.h:344
XLogRecPtr startpoint
Definition: walreceiver.h:168
unsigned int uint32
Definition: c.h:374
int pgsocket
Definition: port.h:31
pid_t(* walrcv_get_backend_pid_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:355
TimeLineID receiveStartTLI
Definition: walreceiver.h:74
void(* walrcv_get_senderinfo_fn)(WalReceiverConn *conn, char **sender_host, int *sender_port)
Definition: walreceiver.h:258
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:365
void(* walrcv_check_conninfo_fn)(const char *conninfo)
Definition: walreceiver.h:241
static TimeLineID receiveTLI
Definition: xlog.c:214
void(* walrcv_readtimelinehistoryfile_fn)(WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *size)
Definition: walreceiver.h:286
int(* walrcv_receive_fn)(WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)
Definition: walreceiver.h:320
bool ready_to_display
Definition: walreceiver.h:132
bool(* walrcv_startstreaming_fn)(WalReceiverConn *conn, const WalRcvStreamOptions *options)
Definition: walreceiver.h:300
walrcv_get_conninfo_fn walrcv_get_conninfo
Definition: walreceiver.h:381
walrcv_disconnect_fn walrcv_disconnect
Definition: walreceiver.h:393
TimestampTz latestWalEndTime
Definition: walreceiver.h:104
walrcv_endstreaming_fn walrcv_endstreaming
Definition: walreceiver.h:387
int wal_receiver_status_interval
Definition: walreceiver.c:88
XLogRecPtr latestWalEnd
Definition: walreceiver.h:103
Tuplestorestate * tuplestore
Definition: walreceiver.h:214
walrcv_readtimelinehistoryfile_fn walrcv_readtimelinehistoryfile
Definition: walreceiver.h:385
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalRcvExecStatus status
Definition: walreceiver.h:212
walrcv_get_senderinfo_fn walrcv_get_senderinfo
Definition: walreceiver.h:382
void WalReceiverMain(void) pg_attribute_noreturn()
Definition: walreceiver.c:178
void(* walrcv_endstreaming_fn)(WalReceiverConn *conn, TimeLineID *next_tli)
Definition: walreceiver.h:309
size_t Size
Definition: c.h:473
Size WalRcvShmemSize(void)
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:453
walrcv_exec_fn walrcv_exec
Definition: walreceiver.h:392
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:91
walrcv_create_slot_fn walrcv_create_slot
Definition: walreceiver.h:390
bool WalRcvRunning(void)
CRSSnapshotAction
Definition: walsender.h:20
TimeLineID startpointTLI
Definition: walreceiver.h:174
walrcv_check_conninfo_fn walrcv_check_conninfo
Definition: walreceiver.h:380
XLogRecPtr receiveStart
Definition: walreceiver.h:73
XLogRecPtr flushedUpto
Definition: walreceiver.h:83
char *(* walrcv_get_conninfo_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:249
int(* walrcv_server_version_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:277
walrcv_connect_fn walrcv_connect
Definition: walreceiver.h:379
Definition: pg_list.h:50
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
unsigned char bool
Definition: c.h:324
char *(* walrcv_identify_system_fn)(WalReceiverConn *conn, TimeLineID *primary_tli)
Definition: walreceiver.h:269