Skip to content

Commit d67354d

Browse files
committed
Fix limitations on what SQL commands can be issued to a walsender.
In logical replication mode, a WalSender is supposed to be able to execute any regular SQL command, as well as the special replication commands. Poor design of the replication-command parser caused it to fail in various cases, notably: * semicolons embedded in a command, or multiple SQL commands sent in a single message; * dollar-quoted literals containing odd numbers of single or double quote marks; * commands starting with a comment. The basic problem here is that we're trying to run repl_scanner.l across the entire input string even when it's not a replication command. Since repl_scanner.l does not understand all of the token types known to the core lexer, this is doomed to have failure modes. We certainly don't want to make repl_scanner.l as big as scan.l, so instead rejigger stuff so that we only lex the first token of a non-replication command. That will usually look like an IDENT to repl_scanner.l, though a comment would end up getting reported as a '-' or '/' single-character token. If the token is a replication command keyword, we push it back and proceed normally with repl_gram.y parsing. Otherwise, we can drop out of exec_replication_command() without examining the rest of the string. (It's still theoretically possible for repl_scanner.l to fail on the first token; but that could only happen if it's an unterminated single- or double-quoted string, in which case you'd have gotten largely the same error from the core lexer too.) In this way, repl_gram.y isn't involved at all in handling general SQL commands, so we can get rid of the SQLCmd node type. (In the back branches, we can't remove it because renumbering enum NodeTag would be an ABI break; so just leave it sit there unused.) I failed to resist the temptation to clean up some other sloppy coding in repl_scanner.l while at it. The only externally-visible behavior change from that is it now accepts \r and \f as whitespace, same as the core lexer. Per bug #17379 from Greg Rychlewski. Back-patch to all supported branches. Discussion: https://postgr.es/m/17379-6a5c6cfb3f1f5e77@postgresql.org
1 parent c94c661 commit d67354d

File tree

4 files changed

+97
-58
lines changed

4 files changed

+97
-58
lines changed

src/backend/replication/repl_gram.y

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
/* Result of the parsing is returned here */
2626
Node *replication_parse_result;
2727

28-
static SQLCmd *make_sqlcmd(void);
29-
3028

3129
/*
3230
* Bison doesn't allocate anything that needs to live across parser calls,
@@ -59,7 +57,6 @@ static SQLCmd *make_sqlcmd(void);
5957
%token <str> SCONST IDENT
6058
%token <uintval> UCONST
6159
%token <recptr> RECPTR
62-
%token T_WORD
6360

6461
/* Keyword tokens. */
6562
%token K_BASE_BACKUP
@@ -93,7 +90,7 @@ static SQLCmd *make_sqlcmd(void);
9390
%type <node> command
9491
%type <node> base_backup start_replication start_logical_replication
9592
create_replication_slot drop_replication_slot identify_system
96-
timeline_history show sql_cmd
93+
timeline_history show
9794
%type <list> base_backup_opt_list
9895
%type <defelt> base_backup_opt
9996
%type <uintval> opt_timeline
@@ -126,7 +123,6 @@ command:
126123
| drop_replication_slot
127124
| timeline_history
128125
| show
129-
| sql_cmd
130126
;
131127

132128
/*
@@ -413,25 +409,6 @@ plugin_opt_arg:
413409
| /* EMPTY */ { $$ = NULL; }
414410
;
415411

416-
sql_cmd:
417-
IDENT { $$ = (Node *) make_sqlcmd(); }
418-
;
419412
%%
420413

421-
static SQLCmd *
422-
make_sqlcmd(void)
423-
{
424-
SQLCmd *cmd = makeNode(SQLCmd);
425-
int tok;
426-
427-
/* Just move lexer to the end of command. */
428-
for (;;)
429-
{
430-
tok = yylex();
431-
if (tok == ';' || tok == 0)
432-
break;
433-
}
434-
return cmd;
435-
}
436-
437414
#include "repl_scanner.c"

src/backend/replication/repl_scanner.l

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ fprintf_to_ereport(const char *fmt, const char *msg)
3131
/* Handle to the buffer that the lexer uses internally */
3232
static YY_BUFFER_STATE scanbufhandle;
3333

34+
/* Pushed-back token (we only handle one) */
35+
static int repl_pushed_back_token;
36+
37+
/* Work area for collecting literals */
3438
static StringInfoData litbuf;
3539

3640
static void startlit(void);
@@ -51,7 +55,18 @@ static void addlitchar(unsigned char ychar);
5155
%option warn
5256
%option prefix="replication_yy"
5357

54-
%x xq xd
58+
/*
59+
* Exclusive states:
60+
* <xd> delimited identifiers (double-quoted identifiers)
61+
* <xq> standard single-quoted strings
62+
*/
63+
%x xd
64+
%x xq
65+
66+
space [ \t\n\r\f]
67+
68+
quote '
69+
quotestop {quote}
5570

5671
/* Extended quote
5772
* xqdouble implements embedded quote, ''''
@@ -69,11 +84,8 @@ xdstop {dquote}
6984
xddouble {dquote}{dquote}
7085
xdinside [^"]+
7186

72-
digit [0-9]+
73-
hexdigit [0-9A-Za-z]+
74-
75-
quote '
76-
quotestop {quote}
87+
digit [0-9]
88+
hexdigit [0-9A-Fa-f]
7789

7890
ident_start [A-Za-z\200-\377_]
7991
ident_cont [A-Za-z\200-\377_0-9\$]
@@ -82,6 +94,19 @@ identifier {ident_start}{ident_cont}*
8294

8395
%%
8496

97+
%{
98+
/* This code is inserted at the start of replication_yylex() */
99+
100+
/* If we have a pushed-back token, return that. */
101+
if (repl_pushed_back_token)
102+
{
103+
int result = repl_pushed_back_token;
104+
105+
repl_pushed_back_token = 0;
106+
return result;
107+
}
108+
%}
109+
85110
BASE_BACKUP { return K_BASE_BACKUP; }
86111
FAST { return K_FAST; }
87112
IDENTIFY_SYSTEM { return K_IDENTIFY_SYSTEM; }
@@ -110,14 +135,7 @@ WAIT { return K_WAIT; }
110135
MANIFEST { return K_MANIFEST; }
111136
MANIFEST_CHECKSUMS { return K_MANIFEST_CHECKSUMS; }
112137

113-
"," { return ','; }
114-
";" { return ';'; }
115-
"(" { return '('; }
116-
")" { return ')'; }
117-
118-
[\n] ;
119-
[\t] ;
120-
" " ;
138+
{space}+ { /* do nothing */ }
121139

122140
{digit}+ {
123141
yylval.uintval = strtoul(yytext, NULL, 10);
@@ -179,16 +197,18 @@ MANIFEST_CHECKSUMS { return K_MANIFEST_CHECKSUMS; }
179197
return IDENT;
180198
}
181199

200+
. {
201+
/* Any char not recognized above is returned as itself */
202+
return yytext[0];
203+
}
204+
182205
<xq,xd><<EOF>> { yyerror("unterminated quoted string"); }
183206

184207

185208
<<EOF>> {
186209
yyterminate();
187210
}
188211

189-
. {
190-
return T_WORD;
191-
}
192212
%%
193213

194214
/* LCOV_EXCL_STOP */
@@ -248,6 +268,7 @@ replication_scanner_init(const char *str)
248268

249269
/* Make sure we start in proper state */
250270
BEGIN(INITIAL);
271+
repl_pushed_back_token = 0;
251272
}
252273

253274
void
@@ -256,3 +277,34 @@ replication_scanner_finish(void)
256277
yy_delete_buffer(scanbufhandle);
257278
scanbufhandle = NULL;
258279
}
280+
281+
/*
282+
* Check to see if the first token of a command is a WalSender keyword.
283+
*
284+
* To keep repl_scanner.l minimal, we don't ask it to know every construct
285+
* that the core lexer knows. Therefore, we daren't lex more than the
286+
* first token of a general SQL command. That will usually look like an
287+
* IDENT token here, although some other cases are possible.
288+
*/
289+
bool
290+
replication_scanner_is_replication_command(void)
291+
{
292+
int first_token = replication_yylex();
293+
294+
switch (first_token)
295+
{
296+
case K_IDENTIFY_SYSTEM:
297+
case K_BASE_BACKUP:
298+
case K_START_REPLICATION:
299+
case K_CREATE_REPLICATION_SLOT:
300+
case K_DROP_REPLICATION_SLOT:
301+
case K_TIMELINE_HISTORY:
302+
case K_SHOW:
303+
/* Yes; push back the first token so we can parse later. */
304+
repl_pushed_back_token = first_token;
305+
return true;
306+
default:
307+
/* Nope; we don't bother to push back the token. */
308+
return false;
309+
}
310+
}

src/backend/replication/walsender.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,8 @@ exec_replication_command(const char *cmd_string)
15351535
*/
15361536
if (MyWalSnd->state == WALSNDSTATE_STOPPING)
15371537
ereport(ERROR,
1538-
(errmsg("cannot execute new commands while WAL sender is in stopping mode")));
1538+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1539+
errmsg("cannot execute new commands while WAL sender is in stopping mode")));
15391540

15401541
/*
15411542
* CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next
@@ -1546,41 +1547,49 @@ exec_replication_command(const char *cmd_string)
15461547
CHECK_FOR_INTERRUPTS();
15471548

15481549
/*
1549-
* Parse the command.
1550+
* Prepare to parse and execute the command.
15501551
*/
15511552
cmd_context = AllocSetContextCreate(CurrentMemoryContext,
15521553
"Replication command context",
15531554
ALLOCSET_DEFAULT_SIZES);
15541555
old_context = MemoryContextSwitchTo(cmd_context);
15551556

15561557
replication_scanner_init(cmd_string);
1557-
parse_rc = replication_yyparse();
1558-
if (parse_rc != 0)
1559-
ereport(ERROR,
1560-
(errcode(ERRCODE_SYNTAX_ERROR),
1561-
errmsg_internal("replication command parser returned %d",
1562-
parse_rc)));
1563-
replication_scanner_finish();
1564-
1565-
cmd_node = replication_parse_result;
15661558

15671559
/*
1568-
* If it's a SQL command, just clean up our mess and return false; the
1569-
* caller will take care of executing it.
1560+
* Is it a WalSender command?
15701561
*/
1571-
if (IsA(cmd_node, SQLCmd))
1562+
if (!replication_scanner_is_replication_command())
15721563
{
1573-
if (MyDatabaseId == InvalidOid)
1574-
ereport(ERROR,
1575-
(errmsg("cannot execute SQL commands in WAL sender for physical replication")));
1564+
/* Nope; clean up and get out. */
1565+
replication_scanner_finish();
15761566

15771567
MemoryContextSwitchTo(old_context);
15781568
MemoryContextDelete(cmd_context);
15791569

1570+
/* XXX this is a pretty random place to make this check */
1571+
if (MyDatabaseId == InvalidOid)
1572+
ereport(ERROR,
1573+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1574+
errmsg("cannot execute SQL commands in WAL sender for physical replication")));
1575+
15801576
/* Tell the caller that this wasn't a WalSender command. */
15811577
return false;
15821578
}
15831579

1580+
/*
1581+
* Looks like a WalSender command, so parse it.
1582+
*/
1583+
parse_rc = replication_yyparse();
1584+
if (parse_rc != 0)
1585+
ereport(ERROR,
1586+
(errcode(ERRCODE_SYNTAX_ERROR),
1587+
errmsg_internal("replication command parser returned %d",
1588+
parse_rc)));
1589+
replication_scanner_finish();
1590+
1591+
cmd_node = replication_parse_result;
1592+
15841593
/*
15851594
* Report query to various monitoring facilities. For this purpose, we
15861595
* report replication commands just like SQL commands.

src/include/replication/walsender_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ extern int replication_yylex(void);
121121
extern void replication_yyerror(const char *str) pg_attribute_noreturn();
122122
extern void replication_scanner_init(const char *query_string);
123123
extern void replication_scanner_finish(void);
124+
extern bool replication_scanner_is_replication_command(void);
124125

125126
extern Node *replication_parse_result;
126127

0 commit comments

Comments
 (0)