Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1efcc59

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 ableto execute any regular SQL command, as well as the specialreplication commands. Poor design of the replication-commandparser caused it to fail in various cases, notably:* semicolons embedded in a command, or multiple SQL commandssent in a single message;* dollar-quoted literals containing odd numbers of singleor double quote marks;* commands starting with a comment.The basic problem here is that we're trying to run repl_scanner.lacross the entire input string even when it's not a replicationcommand. Since repl_scanner.l does not understand all of thetoken types known to the core lexer, this is doomed to havefailure 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 ofa non-replication command. That will usually look like an IDENTto repl_scanner.l, though a comment would end up getting reportedas a '-' or '/' single-character token. If the token is a replicationcommand keyword, we push it back and proceed normally with repl_gram.yparsing. 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 onthe first token; but that could only happen if it's an unterminatedsingle- or double-quoted string, in which case you'd have gottenlargely the same error from the core lexer too.)In this way, repl_gram.y isn't involved at all in handling generalSQL commands, so we can get rid of the SQLCmd node type. (Inthe back branches, we can't remove it because renumbering enumNodeTag would be an ABI break; so just leave it sit there unused.)I failed to resist the temptation to clean up some other sloppycoding in repl_scanner.l while at it. The only externally-visiblebehavior 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 supportedbranches.Discussion:https://postgr.es/m/17379-6a5c6cfb3f1f5e77@postgresql.org
1 parentef9706b commit1efcc59

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>SCONSTIDENT
6058
%token<uintval>UCONST
6159
%token<recptr>RECPTR
62-
%tokenT_WORD
6360

6461
/* Keyword tokens.*/
6562
%tokenK_BASE_BACKUP
@@ -93,7 +90,7 @@ static SQLCmd *make_sqlcmd(void);
9390
%type<node>command
9491
%type<node>base_backupstart_replicationstart_logical_replication
9592
create_replication_slotdrop_replication_slotidentify_system
96-
timeline_historyshowsql_cmd
93+
timeline_historyshow
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+
staticintrepl_pushed_back_token;
36+
37+
/* Work area for collecting literals*/
3438
static StringInfoData litbuf;
3539

3640
staticvoidstartlit(void);
@@ -51,7 +55,18 @@ static void addlitchar(unsigned char ychar);
5155
%optionwarn
5256
%optionprefix="replication_yy"
5357

54-
%xxqxd
58+
/*
59+
* Exclusive states:
60+
* <xd> delimited identifiers (double-quoted identifiers)
61+
* <xq> standard single-quoted strings
62+
*/
63+
%xxd
64+
%xxq
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+
intresult = 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+
intfirst_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+
returntrue;
306+
default:
307+
/* Nope; we don't bother to push back the token. */
308+
returnfalse;
309+
}
310+
}

‎src/backend/replication/walsender.c‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,8 @@ exec_replication_command(const char *cmd_string)
15201520
*/
15211521
if (MyWalSnd->state==WALSNDSTATE_STOPPING)
15221522
ereport(ERROR,
1523-
(errmsg("cannot execute new commands while WAL sender is in stopping mode")));
1523+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1524+
errmsg("cannot execute new commands while WAL sender is in stopping mode")));
15241525

15251526
/*
15261527
* CREATE_REPLICATION_SLOT ... LOGICAL exports a snapshot until the next
@@ -1531,41 +1532,49 @@ exec_replication_command(const char *cmd_string)
15311532
CHECK_FOR_INTERRUPTS();
15321533

15331534
/*
1534-
*Parse the command.
1535+
*Prepare to parse and execute the command.
15351536
*/
15361537
cmd_context=AllocSetContextCreate(CurrentMemoryContext,
15371538
"Replication command context",
15381539
ALLOCSET_DEFAULT_SIZES);
15391540
old_context=MemoryContextSwitchTo(cmd_context);
15401541

15411542
replication_scanner_init(cmd_string);
1542-
parse_rc=replication_yyparse();
1543-
if (parse_rc!=0)
1544-
ereport(ERROR,
1545-
(errcode(ERRCODE_SYNTAX_ERROR),
1546-
errmsg_internal("replication command parser returned %d",
1547-
parse_rc)));
1548-
replication_scanner_finish();
1549-
1550-
cmd_node=replication_parse_result;
15511543

15521544
/*
1553-
* If it's a SQL command, just clean up our mess and return false; the
1554-
* caller will take care of executing it.
1545+
* Is it a WalSender command?
15551546
*/
1556-
if (IsA(cmd_node,SQLCmd))
1547+
if (!replication_scanner_is_replication_command())
15571548
{
1558-
if (MyDatabaseId==InvalidOid)
1559-
ereport(ERROR,
1560-
(errmsg("cannot execute SQL commands in WAL sender for physical replication")));
1549+
/* Nope; clean up and get out. */
1550+
replication_scanner_finish();
15611551

15621552
MemoryContextSwitchTo(old_context);
15631553
MemoryContextDelete(cmd_context);
15641554

1555+
/* XXX this is a pretty random place to make this check */
1556+
if (MyDatabaseId==InvalidOid)
1557+
ereport(ERROR,
1558+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1559+
errmsg("cannot execute SQL commands in WAL sender for physical replication")));
1560+
15651561
/* Tell the caller that this wasn't a WalSender command. */
15661562
return false;
15671563
}
15681564

1565+
/*
1566+
* Looks like a WalSender command, so parse it.
1567+
*/
1568+
parse_rc=replication_yyparse();
1569+
if (parse_rc!=0)
1570+
ereport(ERROR,
1571+
(errcode(ERRCODE_SYNTAX_ERROR),
1572+
errmsg_internal("replication command parser returned %d",
1573+
parse_rc)));
1574+
replication_scanner_finish();
1575+
1576+
cmd_node=replication_parse_result;
1577+
15691578
/*
15701579
* Report query to various monitoring facilities. For this purpose, we
15711580
* 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 intreplication_yylex(void);
121121
externvoidreplication_yyerror(constchar*str)pg_attribute_noreturn();
122122
externvoidreplication_scanner_init(constchar*query_string);
123123
externvoidreplication_scanner_finish(void);
124+
externboolreplication_scanner_is_replication_command(void);
124125

125126
externNode*replication_parse_result;
126127

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp