PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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 "fmgr.h"
18 #include "storage/latch.h"
19 #include "storage/spin.h"
20 #include "pgtime.h"
21 
22 /* user-settable parameters */
24 extern int wal_receiver_timeout;
25 extern bool hot_standby_feedback;
26 
27 /*
28  * MAXCONNINFO: maximum size of a connection string.
29  *
30  * XXX: Should this move to pg_config_manual.h?
31  */
32 #define MAXCONNINFO 1024
33 
34 /* Can we allow the standby to accept replication connection from another standby? */
35 #define AllowCascadeReplication() (EnableHotStandby && max_wal_senders > 0)
36 
37 /*
38  * Values for WalRcv->walRcvState.
39  */
40 typedef enum
41 {
42  WALRCV_STOPPED, /* stopped and mustn't start up again */
43  WALRCV_STARTING, /* launched, but the process hasn't
44  * initialized yet */
45  WALRCV_STREAMING, /* walreceiver is streaming */
46  WALRCV_WAITING, /* stopped streaming, waiting for orders */
47  WALRCV_RESTARTING, /* asked to restart streaming */
48  WALRCV_STOPPING /* requested to stop, but still running */
49 } WalRcvState;
50 
51 /* Shared memory area for management of walreceiver process */
52 typedef struct
53 {
54  /*
55  * PID of currently active walreceiver process, its current state and
56  * start time (actually, the time at which it was requested to be
57  * started).
58  */
59  pid_t pid;
62 
63  /*
64  * receiveStart and receiveStartTLI indicate the first byte position and
65  * timeline that will be received. When startup process starts the
66  * walreceiver, it sets these to the point where it wants the streaming to
67  * begin.
68  */
71 
72  /*
73  * receivedUpto-1 is the last byte position that has already been
74  * received, and receivedTLI is the timeline it came from. At the first
75  * startup of walreceiver, these are set to receiveStart and
76  * receiveStartTLI. After that, walreceiver updates these whenever it
77  * flushes the received WAL to disk.
78  */
81 
82  /*
83  * latestChunkStart is the starting byte position of the current "batch"
84  * of received WAL. It's actually the same as the previous value of
85  * receivedUpto before the last flush to disk. Startup process can use
86  * this to detect whether it's keeping up or not.
87  */
89 
90  /*
91  * Time of send and receive of any message received.
92  */
95 
96  /*
97  * Latest reported end of WAL on the sender
98  */
101 
102  /*
103  * connection string; initially set to connect to the primary, and later
104  * clobbered to hide security-sensitive fields.
105  */
106  char conninfo[MAXCONNINFO];
107 
108  /*
109  * replication slot name; is also used for walreceiver to connect with the
110  * primary
111  */
112  char slotname[NAMEDATALEN];
113 
114  slock_t mutex; /* locks shared variables shown above */
115 
116  /*
117  * force walreceiver reply? This doesn't need to be locked; memory
118  * barriers for ordering are sufficient.
119  */
121 
122  /* set true once conninfo is ready to display (obfuscated pwds etc) */
124 
125  /*
126  * Latch used by startup process to wake up walreceiver after telling it
127  * where to start streaming (after setting receiveStart and
128  * receiveStartTLI), and also to tell it to send apply feedback to the
129  * primary whenever specially marked commit records are applied.
130  * This is normally mapped to procLatch when walreceiver is running.
131  */
133 } WalRcvData;
134 
135 extern WalRcvData *WalRcv;
136 
137 typedef struct
138 {
139  bool logical; /* True if this is logical
140  replication stream, false if
141  physical stream. */
142  char *slotname; /* Name of the replication slot
143  or NULL. */
144  XLogRecPtr startpoint; /* LSN of starting point. */
145 
146  union
147  {
148  struct
149  {
150  TimeLineID startpointTLI; /* Starting timeline */
151  } physical;
152  struct
153  {
154  uint32 proto_version; /* Logical protocol version */
155  List *publication_names; /* String list of publications */
156  } logical;
157  } proto;
159 
160 struct WalReceiverConn;
162 
163 /* libpqwalreceiver hooks */
164 typedef WalReceiverConn *(*walrcv_connect_fn) (const char *conninfo, bool logical,
165  const char *appname,
166  char **err);
167 typedef void (*walrcv_check_conninfo_fn) (const char *conninfo);
168 typedef char *(*walrcv_get_conninfo_fn) (WalReceiverConn *conn);
169 typedef char *(*walrcv_identify_system_fn) (WalReceiverConn *conn,
170  TimeLineID *primary_tli,
171  int *server_version);
173  TimeLineID tli,
174  char **filename,
175  char **content, int *size);
179  TimeLineID *next_tli);
180 typedef int (*walrcv_receive_fn) (WalReceiverConn *conn, char **buffer,
181  pgsocket *wait_fd);
182 typedef void (*walrcv_send_fn) (WalReceiverConn *conn, const char *buffer,
183  int nbytes);
184 typedef char *(*walrcv_create_slot_fn) (WalReceiverConn *conn,
185  const char *slotname, bool temporary,
186  XLogRecPtr *lsn);
187 typedef bool (*walrcv_command_fn) (WalReceiverConn *conn, const char *cmd,
188  char **err);
190 
192 {
206 
208 
209 #define walrcv_connect(conninfo, logical, appname, err) \
210  WalReceiverFunctions->walrcv_connect(conninfo, logical, appname, err)
211 #define walrcv_check_conninfo(conninfo) \
212  WalReceiverFunctions->walrcv_check_conninfo(conninfo)
213 #define walrcv_get_conninfo(conn) \
214  WalReceiverFunctions->walrcv_get_conninfo(conn)
215 #define walrcv_identify_system(conn, primary_tli, server_version) \
216  WalReceiverFunctions->walrcv_identify_system(conn, primary_tli, server_version)
217 #define walrcv_readtimelinehistoryfile(conn, tli, filename, content, size) \
218  WalReceiverFunctions->walrcv_readtimelinehistoryfile(conn, tli, filename, content, size)
219 #define walrcv_startstreaming(conn, options) \
220  WalReceiverFunctions->walrcv_startstreaming(conn, options)
221 #define walrcv_endstreaming(conn, next_tli) \
222  WalReceiverFunctions->walrcv_endstreaming(conn, next_tli)
223 #define walrcv_receive(conn, buffer, wait_fd) \
224  WalReceiverFunctions->walrcv_receive(conn, buffer, wait_fd)
225 #define walrcv_send(conn, buffer, nbytes) \
226  WalReceiverFunctions->walrcv_send(conn, buffer, nbytes)
227 #define walrcv_create_slot(conn, slotname, temporary, lsn) \
228  WalReceiverFunctions->walrcv_create_slot(conn, slotname, temporary, lsn)
229 #define walrcv_command(conn, cmd, err) \
230  WalReceiverFunctions->walrcv_command(conn, cmd, err)
231 #define walrcv_disconnect(conn) \
232  WalReceiverFunctions->walrcv_disconnect(conn)
233 
234 /* prototypes for functions in walreceiver.c */
235 extern void WalReceiverMain(void) pg_attribute_noreturn();
236 
237 /* prototypes for functions in walreceiverfuncs.c */
238 extern Size WalRcvShmemSize(void);
239 extern void WalRcvShmemInit(void);
240 extern void ShutdownWalRcv(void);
241 extern bool WalRcvStreaming(void);
242 extern bool WalRcvRunning(void);
243 extern void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr,
244  const char *conninfo, const char *slotname);
246 extern int GetReplicationApplyDelay(void);
247 extern int GetReplicationTransferLatency(void);
248 extern void WalRcvForceReply(void);
249 
250 #endif /* _WALRECEIVER_H */
walrcv_identify_system_fn walrcv_identify_system
Definition: walreceiver.h:196
int slock_t
Definition: s_lock.h:888
XLogRecPtr GetWalRcvWriteRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
bool hot_standby_feedback
Definition: walreceiver.c:75
bool WalRcvStreaming(void)
uint32 TimeLineID
Definition: xlogdefs.h:45
int64 pg_time_t
Definition: pgtime.h:23
void ShutdownWalRcv(void)
slock_t mutex
Definition: walreceiver.h:114
walrcv_startstreaming_fn walrcv_startstreaming
Definition: walreceiver.h:198
PGDLLIMPORT WalReceiverFunctionsType * WalReceiverFunctions
Definition: walreceiver.c:79
walrcv_command_fn walrcv_command
Definition: walreceiver.h:203
WalRcvState walRcvState
Definition: walreceiver.h:60
WalReceiverConn *(* walrcv_connect_fn)(const char *conninfo, bool logical, const char *appname, char **err)
Definition: walreceiver.h:164
TimeLineID receivedTLI
Definition: walreceiver.h:80
void(* walrcv_check_conninfo_fn)(const char *conninfo)
Definition: walreceiver.h:167
#define pg_attribute_noreturn()
Definition: c.h:648
char *(* walrcv_identify_system_fn)(WalReceiverConn *conn, TimeLineID *primary_tli, int *server_version)
Definition: walreceiver.h:169
TimestampTz lastMsgReceiptTime
Definition: walreceiver.h:94
TimestampTz lastMsgSendTime
Definition: walreceiver.h:93
pg_time_t startTime
Definition: walreceiver.h:61
char *(* walrcv_get_conninfo_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:168
int GetReplicationApplyDelay(void)
#define PGDLLIMPORT
Definition: c.h:1062
char bool
Definition: c.h:199
void WalRcvShmemInit(void)
WalRcvState
Definition: walreceiver.h:40
walrcv_receive_fn walrcv_receive
Definition: walreceiver.h:200
#define NAMEDATALEN
WalRcvData * WalRcv
double TimestampTz
Definition: timestamp.h:51
void(* walrcv_send_fn)(WalReceiverConn *conn, const char *buffer, int nbytes)
Definition: walreceiver.h:182
void(* walrcv_disconnect_fn)(WalReceiverConn *conn)
Definition: walreceiver.h:189
void WalRcvForceReply(void)
Definition: walreceiver.c:1314
PGconn * conn
Definition: streamutil.c:45
pid_t pid
Definition: walreceiver.h:59
#define MAXCONNINFO
Definition: walreceiver.h:32
XLogRecPtr latestChunkStart
Definition: walreceiver.h:88
int wal_receiver_timeout
Definition: walreceiver.c:74
int GetReplicationTransferLatency(void)
void(* walrcv_endstreaming_fn)(WalReceiverConn *conn, TimeLineID *next_tli)
Definition: walreceiver.h:178
Definition: latch.h:110
walrcv_send_fn walrcv_send
Definition: walreceiver.h:201
void(* walrcv_readtimelinehistoryfile_fn)(WalReceiverConn *conn, TimeLineID tli, char **filename, char **content, int *size)
Definition: walreceiver.h:172
XLogRecPtr startpoint
Definition: walreceiver.h:144
unsigned int uint32
Definition: c.h:265
int pgsocket
Definition: port.h:22
XLogRecPtr receivedUpto
Definition: walreceiver.h:79
char *(* walrcv_create_slot_fn)(WalReceiverConn *conn, const char *slotname, bool temporary, XLogRecPtr *lsn)
Definition: walreceiver.h:184
TimeLineID receiveStartTLI
Definition: walreceiver.h:70
Latch * latch
Definition: walreceiver.h:132
static char ** options
bool force_reply
Definition: walreceiver.h:120
static TimeLineID receiveTLI
Definition: xlog.c:199
bool ready_to_display
Definition: walreceiver.h:123
walrcv_get_conninfo_fn walrcv_get_conninfo
Definition: walreceiver.h:195
walrcv_disconnect_fn walrcv_disconnect
Definition: walreceiver.h:204
TimestampTz latestWalEndTime
Definition: walreceiver.h:100
bool(* walrcv_startstreaming_fn)(WalReceiverConn *conn, const WalRcvStreamOptions *options)
Definition: walreceiver.h:176
walrcv_endstreaming_fn walrcv_endstreaming
Definition: walreceiver.h:199
int wal_receiver_status_interval
Definition: walreceiver.c:73
XLogRecPtr latestWalEnd
Definition: walreceiver.h:99
void RequestXLogStreaming(TimeLineID tli, XLogRecPtr recptr, const char *conninfo, const char *slotname)
walrcv_readtimelinehistoryfile_fn walrcv_readtimelinehistoryfile
Definition: walreceiver.h:197
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void WalReceiverMain(void) pg_attribute_noreturn()
Definition: walreceiver.c:187
static int server_version
Definition: pg_dumpall.c:81
size_t Size
Definition: c.h:352
Size WalRcvShmemSize(void)
struct WalReceiverFunctionsType WalReceiverFunctionsType
int(* walrcv_receive_fn)(WalReceiverConn *conn, char **buffer, pgsocket *wait_fd)
Definition: walreceiver.h:180
static char * filename
Definition: pg_dumpall.c:84
walrcv_create_slot_fn walrcv_create_slot
Definition: walreceiver.h:202
bool WalRcvRunning(void)
TimeLineID startpointTLI
Definition: walreceiver.h:150
walrcv_check_conninfo_fn walrcv_check_conninfo
Definition: walreceiver.h:194
XLogRecPtr receiveStart
Definition: walreceiver.h:69
walrcv_connect_fn walrcv_connect
Definition: walreceiver.h:193
Definition: pg_list.h:45
bool(* walrcv_command_fn)(WalReceiverConn *conn, const char *cmd, char **err)
Definition: walreceiver.h:187