1 /*-------------------------------------------------------------------------
2  *
3  * walsender_private.h
4  *	  Private definitions from replication/walsender.c.
5  *
6  * Portions Copyright (c) 2010-2020, PostgreSQL Global Development Group
7  *
8  * src/include/replication/walsender_private.h
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef _WALSENDER_PRIVATE_H
13 #define _WALSENDER_PRIVATE_H
14 
15 #include "access/xlog.h"
16 #include "nodes/nodes.h"
17 #include "replication/syncrep.h"
18 #include "storage/latch.h"
19 #include "storage/shmem.h"
20 #include "storage/spin.h"
21 
22 typedef enum WalSndState
23 {
24 	WALSNDSTATE_STARTUP = 0,
25 	WALSNDSTATE_BACKUP,
26 	WALSNDSTATE_CATCHUP,
27 	WALSNDSTATE_STREAMING,
28 	WALSNDSTATE_STOPPING
29 } WalSndState;
30 
31 /*
32  * Each walsender has a WalSnd struct in shared memory.
33  *
34  * This struct is protected by its 'mutex' spinlock field, except that some
35  * members are only written by the walsender process itself, and thus that
36  * process is free to read those members without holding spinlock.  pid and
37  * needreload always require the spinlock to be held for all accesses.
38  */
39 typedef struct WalSnd
40 {
41 	pid_t		pid;			/* this walsender's PID, or 0 if not active */
42 
43 	WalSndState state;			/* this walsender's state */
44 	XLogRecPtr	sentPtr;		/* WAL has been sent up to this point */
45 	bool		needreload;		/* does currently-open file need to be
46 								 * reloaded? */
47 
48 	/*
49 	 * The xlog locations that have been written, flushed, and applied by
50 	 * standby-side. These may be invalid if the standby-side has not offered
51 	 * values yet.
52 	 */
53 	XLogRecPtr	write;
54 	XLogRecPtr	flush;
55 	XLogRecPtr	apply;
56 
57 	/* Measured lag times, or -1 for unknown/none. */
58 	TimeOffset	writeLag;
59 	TimeOffset	flushLag;
60 	TimeOffset	applyLag;
61 
62 	/*
63 	 * The priority order of the standby managed by this WALSender, as listed
64 	 * in synchronous_standby_names, or 0 if not-listed.
65 	 */
66 	int			sync_standby_priority;
67 
68 	/* Protects shared variables shown above. */
69 	slock_t		mutex;
70 
71 	/*
72 	 * Pointer to the walsender's latch. Used by backends to wake up this
73 	 * walsender when it has work to do. NULL if the walsender isn't active.
74 	 */
75 	Latch	   *latch;
76 
77 	/*
78 	 * Timestamp of the last message received from standby.
79 	 */
80 	TimestampTz replyTime;
81 } WalSnd;
82 
83 extern WalSnd *MyWalSnd;
84 
85 /* There is one WalSndCtl struct for the whole database cluster */
86 typedef struct
87 {
88 	/*
89 	 * Synchronous replication queue with one queue per request type.
90 	 * Protected by SyncRepLock.
91 	 */
92 	SHM_QUEUE	SyncRepQueue[NUM_SYNC_REP_WAIT_MODE];
93 
94 	/*
95 	 * Current location of the head of the queue. All waiters should have a
96 	 * waitLSN that follows this value. Protected by SyncRepLock.
97 	 */
98 	XLogRecPtr	lsn[NUM_SYNC_REP_WAIT_MODE];
99 
100 	/*
101 	 * Are any sync standbys defined?  Waiting backends can't reload the
102 	 * config file safely, so checkpointer updates this value as needed.
103 	 * Protected by SyncRepLock.
104 	 */
105 	bool		sync_standbys_defined;
106 
107 	WalSnd		walsnds[FLEXIBLE_ARRAY_MEMBER];
108 } WalSndCtlData;
109 
110 extern WalSndCtlData *WalSndCtl;
111 
112 
113 extern void WalSndSetState(WalSndState state);
114 
115 /*
116  * Internal functions for parsing the replication grammar, in repl_gram.y and
117  * repl_scanner.l
118  */
119 extern int	replication_yyparse(void);
120 extern int	replication_yylex(void);
121 extern void replication_yyerror(const char *str) pg_attribute_noreturn();
122 extern void replication_scanner_init(const char *query_string);
123 extern void replication_scanner_finish(void);
124 
125 extern Node *replication_parse_result;
126 
127 #endif							/* _WALSENDER_PRIVATE_H */
128