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"
20 #include "replication/walsender.h"
21 #include "storage/latch.h"
22 #include "storage/spin.h"
23 #include "utils/tuplestore.h"
24 
25 /* user-settable parameters */
27 extern int wal_receiver_timeout;
28 extern bool hot_standby_feedback;
29 
30 /*
31  * MAXCONNINFO: maximum size of a connection string.
32  *
33  * XXX: Should this move to pg_config_manual.h?
34  */
35 #define MAXCONNINFO 1024
36 
37 /* Can we allow the standby to accept replication connection from another standby? */
38 #define AllowCascadeReplication() (EnableHotStandby && max_wal_senders > 0)
39 
40 /*
41  * Values for WalRcv->walRcvState.
42  */
43 typedef enum
44 {
45  WALRCV_STOPPED, /* stopped and mustn't start up again */
46  WALRCV_STARTING, /* launched, but the process hasn't
47  * initialized yet */
48  WALRCV_STREAMING, /* walreceiver is streaming */
49  WALRCV_WAITING, /* stopped streaming, waiting for orders */
50  WALRCV_RESTARTING, /* asked to restart streaming */
51  WALRCV_STOPPING /* requested to stop, but still running */
52 } WalRcvState;
53 
54 /* Shared memory area for management of walreceiver process */
55 typedef struct
56 {
57  /*
58  * PID of currently active walreceiver process, its current state and
59  * start time (actually, the time at which it was requested to be
60  * started).
61  */
62  pid_t pid;
65 
66  /*
67  * receiveStart and receiveStartTLI indicate the first byte position and
68  * timeline that will be received. When startup process starts the
69  * walreceiver, it sets these to the point where it wants the streaming to
70  * begin.
71  */
74 
75  /*
76  * receivedUpto-1 is the last byte position that has already been
77  * received, and receivedTLI is the timeline it came from. At the first
78  * startup of walreceiver, these are set to receiveStart and
79  * receiveStartTLI. After that, walreceiver updates these whenever it
80  * flushes the received WAL to disk.
81  */
84 
85  /*
86  * latestChunkStart is the starting byte position of the current "batch"
87  * of received WAL. It's actually the same as the previous value of
88  * receivedUpto before the last flush to disk. Startup process can use
89  * this to detect whether it's keeping up or not.
90  */
92 
93  /*
94  * Time of send and receive of any message received.
95  */
98 
99  /*
100  * Latest reported end of WAL on the sender
101  */
104 
105  /*
106  * connection string; initially set to connect to the primary, and later
107  * clobbered to hide security-sensitive fields.
108  */
109  char conninfo[MAXCONNINFO];
110 
111  /*
112  * Host name (this can be a host name, an IP address, or a directory path)
113  * and port number of the active replication connection.
114  */
115  char sender_host[NI_MAXHOST];
117 
118  /*
119  * replication slot name; is also used for walreceiver to connect with the
120  * primary
121  */
122  char slotname[NAMEDATALEN];
123 
124  /*
125  * If it's a temporary replication slot, it needs to be recreated when
126  * connecting.
127  */
129 
130  /* set true once conninfo is ready to display (obfuscated pwds etc) */
132 
133  /*
134  * Latch used by startup process to wake up walreceiver after telling it
135  * where to start streaming (after setting receiveStart and
136  * receiveStartTLI), and also to tell it to send apply feedback to the
137  * primary whenever specially marked commit records are applied. This is
138  * normally mapped to procLatch when walreceiver is running.
139  */
141 
142  slock_t mutex; /* locks shared variables shown above */
143 
144  /*
145  * force walreceiver reply? This doesn't need to be locked; memory
146  * barriers for ordering are sufficient. But we do need atomic fetch and
147  * store semantics, so use sig_atomic_t.
148  */
149  sig_atomic_t force_reply; /* used as a bool */
150 } WalRcvData;
151 
152 extern WalRcvData *WalRcv;
153 
154 typedef struct
155 {
156  bool logical; /* True if this is logical replication stream,
157  * false if physical stream. */
158  char *slotname; /* Name of the replication slot or NULL. */
159  XLogRecPtr startpoint; /* LSN of starting point. */
160 
161  union
162  {
163  struct
164  {
165  TimeLineID startpointTLI; /* Starting timeline */
166  } physical;
167  struct
168  {
169  uint32 proto_version; /* Logical protocol version */
170  List *publication_names; /* String list of publications */
171  } logical;
172  } proto;
174 
175 struct WalReceiverConn;
177 
178 /*
179  * Status of walreceiver query execution.
180  *
181  * We only define statuses that are currently used.
182  */
183 typedef enum
184 {
185  WALRCV_ERROR, /* There was error when executing the query. */
186  WALRCV_OK_COMMAND, /* Query executed utility or replication
187  * command. */
188  WALRCV_OK_TUPLES, /* Query returned tuples. */
189  WALRCV_OK_COPY_IN, /* Query started COPY FROM. */
190  WALRCV_OK_COPY_OUT, /* Query started COPY TO. */
191  WALRCV_OK_COPY_BOTH /* Query started COPY BOTH replication
192  * protocol. */
194 
195 /*
196  * Return value for walrcv_exec, returns the status of the execution and
197  * tuples if any.
198  */
199 typedef struct WalRcvExecResult
200 {
202  char *err;
206 
207 /* libpqwalreceiver hooks */
208 typedef WalReceiverConn *(*walrcv_connect_fn) (const char *conninfo, bool logical,
209  const char *appname,
210  char **err);
211 typedef void (*walrcv_check_conninfo_fn) (const char *conninfo);
212 typedef char *(*walrcv_get_conninfo_fn) (WalReceiverConn *conn);
214  char **sender_host,
215  int *sender_port);
216 typedef char *(*walrcv_identify_system_fn) (WalReceiverConn *conn,
217  TimeLineID *primary_tli);
220  TimeLineID tli,
221  char **filename,
222  char **content, int *size);
226  TimeLineID *next_tli);
227 typedef int (*walrcv_receive_fn) (WalReceiverConn *conn, char **buffer,
228  pgsocket *wait_fd);
229 typedef void (*walrcv_send_fn) (WalReceiverConn *conn, const char *buffer,
230  int nbytes);
231 typedef char *(*walrcv_create_slot_fn) (WalReceiverConn *conn,
232  const char *slotname, bool temporary,
233  CRSSnapshotAction snapshot_action,
234  XLogRecPtr *lsn);
236 typedef WalRcvExecResult *(*walrcv_exec_fn) (WalReceiverConn *conn,
237  const char *query,
238  const int nRetTypes,
239  const Oid *retTypes);
241 
243 {
260 
262 
263 #define walrcv_connect(conninfo, logical, appname, err) \
264  WalReceiverFunctions->walrcv_connect(conninfo, logical, appname, err)
265 #define walrcv_check_conninfo(conninfo) \
266  WalReceiverFunctions->walrcv_check_conninfo(conninfo)
267 #define walrcv_get_conninfo(conn) \
268  WalReceiverFunctions->walrcv_get_conninfo(conn)
269 #define walrcv_get_senderinfo(conn, sender_host, sender_port) \
270  WalReceiverFunctions->walrcv_get_senderinfo(conn, sender_host, sender_port)
271 #define walrcv_identify_system(conn, primary_tli) \
272  WalReceiverFunctions->walrcv_identify_system(conn, primary_tli)
273 #define walrcv_server_version(conn) \
274  WalReceiverFunctions->walrcv_server_version(conn)
275 #define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size) \
276  WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
277 #define walrcv_startstreaming(conn, options) \
278  WalReceiverFunctions->walrcv_startstreaming(conn, options)
279 #define walrcv_endstreaming(conn, next_tli) \
280  WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)
281 #define walrcv_receive(conn, buffer, wait_fd) \
282  WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)
283 #define walrcv_send(conn, buffer, nbytes) \
284  WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)
285 #define walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn) \
286  WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, snapshot_action, lsn)
287 #define walrcv_get_backend_pid(conn) \
288  WalReceiverFunctions->walrcv_get_backend_pid(conn)
289 #define walrcv_exec(conn, exec, nRetTypes, retTypes) \
290  WalReceiverFunctions->walrcv_exec(conn, exec, nRetTypes, retTypes)
291 #define walrcv_disconnect(conn) \
292  WalReceiverFunctions->walrcv_disconnect(conn)
293 
294 static inline void
296 {
297  if (!walres)
298  return;
299 
300  if (walres->err)
301  pfree(walres->err);
302 
303  if (walres->tuplestore)
304  tuplestore_end(walres->tuplestore);
305 
306  if (walres->tupledesc)
307  FreeTupleDesc(walres->tupledesc);
308 
309  pfree(walres);
310 }
311 
312 /* prototypes for functions in walreceiver.c */
313 extern void WalReceiverMain(void) pg_attribute_noreturn();
314 extern void ProcessWalRcvInterrupts(void);
315 
316 /* prototypes for functions in walreceiverfuncs.c */
317 extern Size WalRcvShmemSize(void);
318 extern void WalRcvShmemInit(void);
319 extern void ShutdownWalRcv(void);
320 extern bool WalRcvStreaming(void);
321 extern bool WalRcvRunning(void);
322 extern void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr,
323  const char *conninfo, const char *slotname,
324  bool create_temp_slot);
326 extern int GetReplicationApplyDelay(void);
327 extern int GetReplicationTransferLatency(void);
328 extern void WalRcvForceReply(void);
329 
330 #endif /* _WALRECEIVER_H */
walrcv_identify_system_fn walrcv_identify_system
Definition: walreceiver.h:248
int sender_port
Definition: walreceiver.h:116
int slock_t
Definition: s_lock.h:934
XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool hot_standby_feedback
Definition: walreceiver.c:89
bool WalRcvStreaming(void)
walrcv_server_version_fn walrcv_server_version
Definition: walreceiver.h:249
uint32 TimeLineID
Definition: xlogdefs.h:52
int64 pg_time_t
Definition: pgtime.h:23
void ShutdownWalRcv(void)
slock_t mutex
Definition: walreceiver.h:142
walrcv_startstreaming_fn walrcv_startstreaming
Definition: walreceiver.h:251
PGDLLIMPORT WalReceiverFunctionsType * WalReceiverFunctions
Definition: walreceiver.c:93
int64 TimestampTz
Definition: timestamp.h:39
sig_atomic_t force_reply
Definition: walreceiver.h:149
WalRcvState walRcvState
Definition: walreceiver.h:63
WalReceiverConn *(* walrcv_connect_fn)(const char *conninfo, bool logical, const char *appname, char **err)
Definition: walreceiver.h:208
void(* walrcv_disconnect_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:240
WalRcvExecStatus
Definition: walreceiver.h:183
TimeLineID receivedTLI
Definition: walreceiver.h:83
void ProcessWalRcvInterrupts(void)
Definition: walreceiver.c:156
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:97
TimestampTz lastMsgSendTime
Definition: walreceiver.h:96
pg_time_t startTime
Definition: walreceiver.h:64
int GetReplicationApplyDelay(void)
#define PGDLLIMPORT
Definition: c.h:1280
void WalRcvShmemInit(void)
WalRcvState
Definition: walreceiver.h:43
walrcv_receive_fn walrcv_receive
Definition: walreceiver.h:253
#define NAMEDATALEN
WalRcvData * WalRcv
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:295
void pfree(void *pointer)
Definition: mcxt.c:1056
#define NI_MAXHOST
Definition: getaddrinfo.h:88
struct WalRcvExecResult WalRcvExecResult
TupleDesc tupledesc
Definition: walreceiver.h:204
walrcv_get_backend_pid_fn walrcv_get_backend_pid
Definition: walreceiver.h:256
void WalRcvForceReply(void)
Definition: walreceiver.c:1292
PGconn * conn
Definition: streamutil.c:54
pid_t pid
Definition: walreceiver.h:62
#define MAXCONNINFO
Definition: walreceiver.h:35
XLogRecPtr latestChunkStart
Definition: walreceiver.h:91
int wal_receiver_timeout
Definition: walreceiver.c:88
int GetReplicationTransferLatency(void)
Definition: latch.h:110
void(* walrcv_send_fn)(WalReceiverConn *conn, const char *buffer, int nbytes)
Definition: walreceiver.h:229
walrcv_send_fn walrcv_send
Definition: walreceiver.h:254
char *(* walrcv_create_slot_fn)(WalReceiverConn *conn, const char *slotname, bool temporary, CRSSnapshotAction snapshot_action, XLogRecPtr *lsn)
Definition: walreceiver.h:231
XLogRecPtr startpoint
Definition: walreceiver.h:159
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:235
XLogRecPtr receivedUpto
Definition: walreceiver.h:82
TimeLineID receiveStartTLI
Definition: walreceiver.h:73
void(* walrcv_get_senderinfo_fn)(WalReceiverConn *conn, char **sender_host, int *sender_port)
Definition: walreceiver.h:213
Latch * latch
Definition: walreceiver.h:140
static char ** options
WalRcvExecResult *(* walrcv_exec_fn)(WalReceiverConn *conn, const char *query, const int nRetTypes, const Oid *retTypes)
Definition: walreceiver.h:236
void(* walrcv_check_conninfo_fn)(const char *conninfo)
Definition: walreceiver.h:211
static TimeLineID receiveTLI
Definition: xlog.c:210
void(* walrcv_readtimelinehistoryfile_fn)(WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *size)
Definition: walreceiver.h:219
int(* walrcv_receive_fn)(WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)
Definition: walreceiver.h:227
bool ready_to_display
Definition: walreceiver.h:131
bool(* walrcv_startstreaming_fn)(WalReceiverConn *conn, const WalRcvStreamOptions *options)
Definition: walreceiver.h:223
walrcv_get_conninfo_fn walrcv_get_conninfo
Definition: walreceiver.h:246
walrcv_disconnect_fn walrcv_disconnect
Definition: walreceiver.h:258
TimestampTz latestWalEndTime
Definition: walreceiver.h:103
walrcv_endstreaming_fn walrcv_endstreaming
Definition: walreceiver.h:252
int wal_receiver_status_interval
Definition: walreceiver.c:87
XLogRecPtr latestWalEnd
Definition: walreceiver.h:102
Tuplestorestate * tuplestore
Definition: walreceiver.h:203
walrcv_readtimelinehistoryfile_fn walrcv_readtimelinehistoryfile
Definition: walreceiver.h:250
uint64 XLogRecPtr
Definition: xlogdefs.h:21
WalRcvExecStatus status
Definition: walreceiver.h:201
walrcv_get_senderinfo_fn walrcv_get_senderinfo
Definition: walreceiver.h:247
void WalReceiverMain(void) pg_attribute_noreturn()
Definition: walreceiver.c:177
void(* walrcv_endstreaming_fn)(WalReceiverConn *conn, TimeLineID *next_tli)
Definition: walreceiver.h:225
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:257
struct WalReceiverFunctionsType WalReceiverFunctionsType
bool is_temp_slot
Definition: walreceiver.h:128
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:313
#define pg_attribute_noreturn()
Definition: c.h:145
static char * filename
Definition: pg_dumpall.c:90
walrcv_create_slot_fn walrcv_create_slot
Definition: walreceiver.h:255
bool WalRcvRunning(void)
CRSSnapshotAction
Definition: walsender.h:20
TimeLineID startpointTLI
Definition: walreceiver.h:165
walrcv_check_conninfo_fn walrcv_check_conninfo
Definition: walreceiver.h:245
XLogRecPtr receiveStart
Definition: walreceiver.h:72
char *(* walrcv_get_conninfo_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:212
int(* walrcv_server_version_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:218
walrcv_connect_fn walrcv_connect
Definition: walreceiver.h:244
Definition: pg_list.h:50
unsigned char bool
Definition: c.h:317
char *(* walrcv_identify_system_fn)(WalReceiverConn *conn, TimeLineID *primary_tli)
Definition: walreceiver.h:216