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