2 /*-------------------------------------------------------------------------
4 * syncrep_gram.y - Parser for synchronous_standby_names
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/replication/syncrep_gram.y
13 *-------------------------------------------------------------------------
17 #include "nodes/pg_list.h"
18 #include "replication/syncrep.h"
20 #include "syncrep_gram.h"
22 /* Result of parsing is returned in one of these two variables */
23 SyncRepConfigData
*syncrep_parse_result
;
24 char *syncrep_parse_error_msg
;
26 static SyncRepConfigData
*create_syncrep_config
(const char *num_sync
,
27 List
*members
, uint8 syncrep_method
);
29 /* silence -Wmissing-variable-declarations */
30 extern
int syncrep_yychar
;
31 extern
int syncrep_yynerrs
;
34 * Bison doesn't allocate anything that needs to live across parser calls,
35 * so we can easily have it use palloc instead of malloc. This prevents
36 * memory leaks if we error out during parsing.
38 #define YYMALLOC palloc
44 %name
-prefix
="syncrep_yy"
50 SyncRepConfigData
*config
;
53 %token
<str
> NAME NUM JUNK ANY FIRST
55 %type
<config
> result standby_config
56 %type
<list
> standby_list
57 %type
<str
> standby_name
63 standby_config
{ syncrep_parse_result
= $1; }
67 standby_list
{ $$
= create_syncrep_config
("1", $1, SYNC_REP_PRIORITY
); }
68 | NUM
'(' standby_list
')' { $$
= create_syncrep_config
($1, $3, SYNC_REP_PRIORITY
); }
69 | ANY NUM
'(' standby_list
')' { $$
= create_syncrep_config
($2, $4, SYNC_REP_QUORUM
); }
70 | FIRST NUM
'(' standby_list
')' { $$
= create_syncrep_config
($2, $4, SYNC_REP_PRIORITY
); }
74 standby_name
{ $$
= list_make1
($1); }
75 | standby_list
',' standby_name
{ $$
= lappend
($1, $3); }
84 static SyncRepConfigData
*
85 create_syncrep_config
(const char *num_sync
, List
*members
, uint8 syncrep_method
)
87 SyncRepConfigData
*config
;
92 /* Compute space needed for flat representation */
93 size
= offsetof
(SyncRepConfigData
, member_names
);
96 char *standby_name
= (char *) lfirst
(lc
);
98 size
+= strlen
(standby_name
) + 1;
101 /* And transform the data into flat representation */
102 config
= (SyncRepConfigData
*) palloc
(size
);
104 config
->config_size
= size
;
105 config
->num_sync
= atoi
(num_sync
);
106 config
->syncrep_method
= syncrep_method
;
107 config
->nmembers
= list_length
(members
);
108 ptr
= config
->member_names
;
111 char *standby_name
= (char *) lfirst
(lc
);
113 strcpy
(ptr
, standby_name
);
114 ptr
+= strlen
(standby_name
) + 1;