Skip to content

Commit 999f172

Browse files
committed
De-reserve keywords EXECUTE and STRICT in PL/pgSQL.
On close inspection, there does not seem to be a strong reason why these should be fully-reserved keywords. I guess they just escaped consideration in previous attempts to minimize PL/pgSQL's list of reserved words. Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com> Discussion: https://postgr.es/m/2185258.1745617445@sss.pgh.pa.us
1 parent bd09f02 commit 999f172

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

src/pl/plpgsql/src/expected/plpgsql_misc.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,17 @@ do $$ declare x public.foo%rowtype; begin end $$;
6565
ERROR: relation "public.foo" does not exist
6666
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
6767
do $$ declare x public.misc_table%rowtype; begin end $$;
68+
-- Test handling of an unreserved keyword as a variable name
69+
-- and record field name.
70+
do $$
71+
declare
72+
execute int;
73+
r record;
74+
begin
75+
execute := 10;
76+
raise notice 'execute = %', execute;
77+
select 1 as strict into r;
78+
raise notice 'r.strict = %', r.strict;
79+
end $$;
80+
NOTICE: execute = 10
81+
NOTICE: r.strict = 1

src/pl/plpgsql/src/pl_gram.y

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,8 @@ for_control : for_variable K_IN
13681368
int tok = yylex(&yylval, &yylloc, yyscanner);
13691369
int tokloc = yylloc;
13701370

1371-
if (tok == K_EXECUTE)
1371+
if (tok_is_keyword(tok, &yylval,
1372+
K_EXECUTE, "execute"))
13721373
{
13731374
/* EXECUTE means it's a dynamic FOR loop */
13741375
PLpgSQL_stmt_dynfors *new;
@@ -2135,7 +2136,8 @@ stmt_open : K_OPEN cursor_variable
21352136
yyerror(&yylloc, NULL, yyscanner, "syntax error, expected \"FOR\"");
21362137

21372138
tok = yylex(&yylval, &yylloc, yyscanner);
2138-
if (tok == K_EXECUTE)
2139+
if (tok_is_keyword(tok, &yylval,
2140+
K_EXECUTE, "execute"))
21392141
{
21402142
int endtoken;
21412143

@@ -2536,6 +2538,7 @@ unreserved_keyword :
25362538
| K_ERRCODE
25372539
| K_ERROR
25382540
| K_EXCEPTION
2541+
| K_EXECUTE
25392542
| K_EXIT
25402543
| K_FETCH
25412544
| K_FIRST
@@ -2581,6 +2584,7 @@ unreserved_keyword :
25812584
| K_SLICE
25822585
| K_SQLSTATE
25832586
| K_STACKED
2587+
| K_STRICT
25842588
| K_TABLE
25852589
| K_TABLE_NAME
25862590
| K_TYPE
@@ -3514,7 +3518,8 @@ make_return_query_stmt(int location, YYSTYPE *yylvalp, YYLTYPE *yyllocp, yyscan_
35143518
new->stmtid = ++plpgsql_curr_compile->nstatements;
35153519

35163520
/* check for RETURN QUERY EXECUTE */
3517-
if ((tok = yylex(yylvalp, yyllocp, yyscanner)) != K_EXECUTE)
3521+
tok = yylex(yylvalp, yyllocp, yyscanner);
3522+
if (!tok_is_keyword(tok, yylvalp, K_EXECUTE, "execute"))
35183523
{
35193524
/* ordinary static query */
35203525
plpgsql_push_back_token(tok, yylvalp, yyllocp, yyscanner);
@@ -3597,7 +3602,7 @@ read_into_target(PLpgSQL_variable **target, bool *strict, YYSTYPE *yylvalp, YYLT
35973602
*strict = false;
35983603

35993604
tok = yylex(yylvalp, yyllocp, yyscanner);
3600-
if (strict && tok == K_STRICT)
3605+
if (strict && tok_is_keyword(tok, yylvalp, K_STRICT, "strict"))
36013606
{
36023607
*strict = true;
36033608
tok = yylex(yylvalp, yyllocp, yyscanner);

src/pl/plpgsql/src/pl_reserved_kwlist.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ PG_KEYWORD("case", K_CASE)
3333
PG_KEYWORD("declare", K_DECLARE)
3434
PG_KEYWORD("else", K_ELSE)
3535
PG_KEYWORD("end", K_END)
36-
PG_KEYWORD("execute", K_EXECUTE)
3736
PG_KEYWORD("for", K_FOR)
3837
PG_KEYWORD("foreach", K_FOREACH)
3938
PG_KEYWORD("from", K_FROM)
@@ -44,7 +43,6 @@ PG_KEYWORD("loop", K_LOOP)
4443
PG_KEYWORD("not", K_NOT)
4544
PG_KEYWORD("null", K_NULL)
4645
PG_KEYWORD("or", K_OR)
47-
PG_KEYWORD("strict", K_STRICT)
4846
PG_KEYWORD("then", K_THEN)
4947
PG_KEYWORD("to", K_TO)
5048
PG_KEYWORD("using", K_USING)

src/pl/plpgsql/src/pl_scanner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ IdentifierLookup plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
5353
* We try to avoid reserving more keywords than we have to; but there's
5454
* little point in not reserving a word if it's reserved in the core grammar.
5555
* Currently, the following words are reserved here but not in the core:
56-
* BEGIN BY DECLARE EXECUTE FOREACH IF LOOP STRICT WHILE
56+
* BEGIN BY DECLARE FOREACH IF LOOP WHILE
5757
*/
5858

5959
/* ScanKeywordList lookup data for PL/pgSQL keywords */

src/pl/plpgsql/src/pl_unreserved_kwlist.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ PG_KEYWORD("elsif", K_ELSIF)
5858
PG_KEYWORD("errcode", K_ERRCODE)
5959
PG_KEYWORD("error", K_ERROR)
6060
PG_KEYWORD("exception", K_EXCEPTION)
61+
PG_KEYWORD("execute", K_EXECUTE)
6162
PG_KEYWORD("exit", K_EXIT)
6263
PG_KEYWORD("fetch", K_FETCH)
6364
PG_KEYWORD("first", K_FIRST)
@@ -103,6 +104,7 @@ PG_KEYWORD("scroll", K_SCROLL)
103104
PG_KEYWORD("slice", K_SLICE)
104105
PG_KEYWORD("sqlstate", K_SQLSTATE)
105106
PG_KEYWORD("stacked", K_STACKED)
107+
PG_KEYWORD("strict", K_STRICT)
106108
PG_KEYWORD("table", K_TABLE)
107109
PG_KEYWORD("table_name", K_TABLE_NAME)
108110
PG_KEYWORD("type", K_TYPE)

src/pl/plpgsql/src/sql/plpgsql_misc.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,16 @@ do $$ declare x foo.bar%rowtype; begin end $$;
3737
do $$ declare x foo.bar.baz%rowtype; begin end $$;
3838
do $$ declare x public.foo%rowtype; begin end $$;
3939
do $$ declare x public.misc_table%rowtype; begin end $$;
40+
41+
-- Test handling of an unreserved keyword as a variable name
42+
-- and record field name.
43+
do $$
44+
declare
45+
execute int;
46+
r record;
47+
begin
48+
execute := 10;
49+
raise notice 'execute = %', execute;
50+
select 1 as strict into r;
51+
raise notice 'r.strict = %', r.strict;
52+
end $$;

0 commit comments

Comments
 (0)