1 /*-------------------------------------------------------------------------
2  *
3  * syncrep.h
4  *	  Exports from replication/syncrep.c.
5  *
6  * Portions Copyright (c) 2010-2018, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  *		src/include/replication/syncrep.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef _SYNCREP_H
14 #define _SYNCREP_H
15 
16 #include "access/xlogdefs.h"
17 #include "utils/guc.h"
18 
19 #define SyncRepRequested() \
20 	(max_wal_senders > 0 && synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)
21 
22 /* SyncRepWaitMode */
23 #define SYNC_REP_NO_WAIT		(-1)
24 #define SYNC_REP_WAIT_WRITE		0
25 #define SYNC_REP_WAIT_FLUSH		1
26 #define SYNC_REP_WAIT_APPLY		2
27 
28 #define NUM_SYNC_REP_WAIT_MODE	3
29 
30 /* syncRepState */
31 #define SYNC_REP_NOT_WAITING		0
32 #define SYNC_REP_WAITING			1
33 #define SYNC_REP_WAIT_COMPLETE		2
34 
35 /* syncrep_method of SyncRepConfigData */
36 #define SYNC_REP_PRIORITY		0
37 #define SYNC_REP_QUORUM		1
38 
39 /*
40  * SyncRepGetCandidateStandbys returns an array of these structs,
41  * one per candidate synchronous walsender.
42  */
43 typedef struct SyncRepStandbyData
44 {
45 	/* Copies of relevant fields from WalSnd shared-memory struct */
46 	pid_t		pid;
47 	XLogRecPtr	write;
48 	XLogRecPtr	flush;
49 	XLogRecPtr	apply;
50 	int			sync_standby_priority;
51 	/* Index of this walsender in the WalSnd shared-memory array */
52 	int			walsnd_index;
53 	/* This flag indicates whether this struct is about our own process */
54 	bool		is_me;
55 } SyncRepStandbyData;
56 
57 /*
58  * Struct for the configuration of synchronous replication.
59  *
60  * Note: this must be a flat representation that can be held in a single
61  * chunk of malloc'd memory, so that it can be stored as the "extra" data
62  * for the synchronous_standby_names GUC.
63  */
64 typedef struct SyncRepConfigData
65 {
66 	int			config_size;	/* total size of this struct, in bytes */
67 	int			num_sync;		/* number of sync standbys that we need to
68 								 * wait for */
69 	uint8		syncrep_method; /* method to choose sync standbys */
70 	int			nmembers;		/* number of members in the following list */
71 	/* member_names contains nmembers consecutive nul-terminated C strings */
72 	char		member_names[FLEXIBLE_ARRAY_MEMBER];
73 } SyncRepConfigData;
74 
75 extern SyncRepConfigData *SyncRepConfig;
76 
77 /* communication variables for parsing synchronous_standby_names GUC */
78 extern SyncRepConfigData *syncrep_parse_result;
79 extern char *syncrep_parse_error_msg;
80 
81 /* user-settable parameters for synchronous replication */
82 extern char *SyncRepStandbyNames;
83 
84 /* called by user backend */
85 extern void SyncRepWaitForLSN(XLogRecPtr lsn, bool commit);
86 
87 /* called at backend exit */
88 extern void SyncRepCleanupAtProcExit(void);
89 
90 /* called by wal sender */
91 extern void SyncRepInitConfig(void);
92 extern void SyncRepReleaseWaiters(void);
93 
94 /* called by wal sender and user backend */
95 extern int	SyncRepGetCandidateStandbys(SyncRepStandbyData **standbys);
96 
97 /* obsolete, do not use in new code */
98 extern List *SyncRepGetSyncStandbys(bool *am_sync);
99 
100 /* called by checkpointer */
101 extern void SyncRepUpdateSyncStandbysDefined(void);
102 
103 /* GUC infrastructure */
104 extern bool check_synchronous_standby_names(char **newval, void **extra, GucSource source);
105 extern void assign_synchronous_standby_names(const char *newval, void *extra);
106 extern void assign_synchronous_commit(int newval, void *extra);
107 
108 /*
109  * Internal functions for parsing synchronous_standby_names grammar,
110  * in syncrep_gram.y and syncrep_scanner.l
111  */
112 extern int	syncrep_yyparse(void);
113 extern int	syncrep_yylex(void);
114 extern void syncrep_yyerror(const char *str);
115 extern void syncrep_scanner_init(const char *query_string);
116 extern void syncrep_scanner_finish(void);
117 
118 #endif							/* _SYNCREP_H */
119