pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
psql: Fix CREATE SCHEMA scanning of nested routine bodies
6+ messages / 2 participants
[nested] [flat]

Commits from this thread:
  2026-06-23 049b742daa  psql: Tighten heuristics for BEGIN/END within CREATE SCHEMA.
* psql: Fix CREATE SCHEMA scanning of nested routine bodies
@ 2026-06-22 09:34  Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Chao Li @ 2026-06-22 09:34 UTC (permalink / raw)
  To: Postgres hackers <pgsql-hackers@lists.postgresql.org>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kirill Reshke <reshkekirill@gmail.com>; jian he <jian.universality@gmail.com>

Hi,

While testing “[d51697484] Support more object types within CREATE SCHEMA”, I found an issue that seems to be a psql regression.

See this repro:
```
evantest=# CREATE SCHEMA s CREATE VIEW begin AS SELECT 1;
evantest-# end;
WARNING:  there is no transaction in progress
CREATE SCHEMA
COMMIT
```

After the first CREATE SCHEMA line, the prompt changed from =# to -#, meaning psql appeared to be treating the view name “begin" as the start of a BEGIN ... END block. After I typed “end;" on the second line, the server executed the first line properly, then executed the second line as “end;", producing the “no transaction” warning. So, although psql waited for “end;", it still sent two statements to the server. This suggests a psql bug.

I then went back to commit 404db8f9, the immediate parent of d51697484, and ran the same test:
```
evantest=# CREATE SCHEMA s CREATE VIEW begin AS SELECT 1;
CREATE SCHEMA
evantest=#
evantest=# select * from s.begin;
 ?column?
----------
        1
(1 row)
```

It created schema “s" and created a view named “begin" under “s". So this seems to confirm a psql regression introduced by d51697484.

Looking at psqlscan.l, d51697484 made BEGIN/END tracking apply to CREATE SCHEMA, which seems too broad. I think we should only track BEGIN ... END for CREATE SCHEMA after seeing CREATE [OR REPLACE] FUNCTION/PROCEDURE. Following this direction, I tried to make a fix.

I added some test cases, but I’m not sure whether 001_basic.pl is the right place for them. Should these tests be added to a new file, say 040_psqlscan.pl? I saw src/test/regress/sql/create_schema.sql, but I believe that is for server-side testing, so these client-side tests should not go there. Suggestions are appreciated.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0001-psql-Fix-CREATE-SCHEMA-scanning-with-object-names.patch (application/octet-stream, 8.6K) download From e9d7b787cd3ce0579114558e27fcf5b6d4f3a22a Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" <lic@highgo.com> Date: Mon, 22 Jun 2026 15:33:04 +0800 Subject: [PATCH v1] psql: Fix CREATE SCHEMA scanning with object names like "begin" Since d51697484, psql's scanner treats CREATE SCHEMA as a command that may contain SQL-standard routine bodies, so that semicolons inside BEGIN ATOMIC ... END blocks do not terminate the command too early. However, the heuristic enabled BEGIN/END tracking for every CREATE SCHEMA statement. As a result, an object name such as "begin" could make interactive psql think the statement was still incomplete, for example: CREATE SCHEMA s CREATE VIEW begin AS SELECT 1; Fix this by enabling BEGIN/END tracking in CREATE SCHEMA only after recognizing a nested CREATE [OR REPLACE] FUNCTION/PROCEDURE command at top-level parenthesis depth. Add interactive psql tests for the false-positive cases and for nested SQL routine bodies inside CREATE SCHEMA. Author: Chao Li <lic@highgo.com> --- src/bin/psql/t/001_basic.pl | 52 ++++++++++++++++++ src/fe_utils/psqlscan.l | 83 +++++++++++++++++++++++++---- src/include/fe_utils/psqlscan_int.h | 7 +++ 3 files changed, 132 insertions(+), 10 deletions(-) diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl index bbd330216ae..d300ef10921 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -72,6 +72,58 @@ max_wal_senders = 4 }); $node->start; +{ + my $h = $node->interactive_psql('postgres'); + $h->set_query_timer_restart(); + + my $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA IF EXISTS psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema CREATE VIEW begin AS SELECT 1;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with object named begin ends at semicolon'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE VIEW v AS SELECT 1 AS function, 2 AS begin;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with column alias function and begin ends at semicolon'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE PROCEDURE p()\n" + . " BEGIN ATOMIC\n" + . " SELECT 1;\n" + . " END;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with SQL procedure body waits for END'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE OR REPLACE FUNCTION f() RETURNS int\n" + . " BEGIN ATOMIC\n" + . " SELECT 1;\n" + . " END;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with OR REPLACE SQL function body waits for END'); + + $h->quit or die "psql returned $?"; +} + psql_like($node, '\copyright', qr/Copyright/, '\copyright'); psql_like($node, '\help', qr/ALTER/, '\help without arguments'); psql_like($node, '\help SELECT', qr/SELECT/, '\help with argument'); diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l index d29dda4d8e1..410eb657703 100644 --- a/src/fe_utils/psqlscan.l +++ b/src/fe_utils/psqlscan.l @@ -61,6 +61,22 @@ typedef int YYSTYPE; #define ECHO psqlscan_emit(cur_state, yytext, yyleng) +/* + * States for detecting nested CREATE [OR REPLACE] FUNCTION/PROCEDURE + * in CREATE SCHEMA. + */ +#define CS_BODY_NONE 0 +#define CS_BODY_CREATE 1 +#define CS_BODY_CREATE_OR 2 +#define CS_BODY_CREATE_OR_REPLACE 3 + +static void +psqlscan_reset_create_schema_body(PsqlScanState state) +{ + state->create_schema_state = CS_BODY_NONE; + state->create_schema_routine_body = false; +} + %} %option reentrant @@ -682,6 +698,7 @@ other . /* Terminate lexing temporarily */ cur_state->start_state = YY_START; cur_state->identifier_count = 0; + psqlscan_reset_create_schema_body(cur_state); return LEXRES_SEMI; } } @@ -695,7 +712,10 @@ other . /* Force a semi-colon or colon into the query buffer */ psqlscan_emit(cur_state, yytext + 1, 1); if (yytext[1] == ';') + { cur_state->identifier_count = 0; + psqlscan_reset_create_schema_body(cur_state); + } } "\\" { @@ -921,6 +941,9 @@ other . {identifier} { + bool is_create_schema; + bool track_begin_depth; + /* * We need to track if we are inside a BEGIN .. END block * in a function definition, so that semicolons contained @@ -928,11 +951,9 @@ other . * writing a full parser here, the following heuristic * should work. First, we track whether the beginning of * the statement matches CREATE [OR REPLACE] - * {FUNCTION|PROCEDURE|SCHEMA}. (Allowing this in - * CREATE SCHEMA, without tracking whether we're within a - * CREATE FUNCTION/PROCEDURE subcommand, is a bit shaky - * but should be okay with the present set of valid - * subcommands.) + * {FUNCTION|PROCEDURE}. For CREATE SCHEMA, track the + * BEGIN .. END block only after recognizing an embedded + * CREATE [OR REPLACE] FUNCTION/PROCEDURE subcommand. */ if (cur_state->identifier_count == 0) @@ -951,11 +972,49 @@ other . cur_state->identifier_count++; - if (cur_state->identifiers[0] == 'c' && - (cur_state->identifiers[1] == 'f' || cur_state->identifiers[1] == 'p' || - (cur_state->identifiers[1] == 'o' && cur_state->identifiers[2] == 'r' && - (cur_state->identifiers[3] == 'f' || cur_state->identifiers[3] == 'p')) || - cur_state->identifiers[1] == 's') && + is_create_schema = + cur_state->identifiers[0] == 'c' && + cur_state->identifiers[1] == 's'; + + /* + * In CREATE SCHEMA, recognize nested SQL routines only at + * top-level paren depth. This avoids treating + * identifiers appearing in expressions as schema elements. + */ + if (is_create_schema && + cur_state->paren_depth == 0 && + cur_state->begin_depth == 0 && + !cur_state->create_schema_routine_body) + { + if (pg_strcasecmp(yytext, "create") == 0) + cur_state->create_schema_state = CS_BODY_CREATE; + else if (cur_state->create_schema_state == CS_BODY_CREATE && + pg_strcasecmp(yytext, "or") == 0) + cur_state->create_schema_state = CS_BODY_CREATE_OR; + else if (cur_state->create_schema_state == CS_BODY_CREATE_OR && + pg_strcasecmp(yytext, "replace") == 0) + cur_state->create_schema_state = CS_BODY_CREATE_OR_REPLACE; + else if ((cur_state->create_schema_state == CS_BODY_CREATE || + cur_state->create_schema_state == CS_BODY_CREATE_OR_REPLACE) && + (pg_strcasecmp(yytext, "function") == 0 || + pg_strcasecmp(yytext, "procedure") == 0)) + cur_state->create_schema_routine_body = true; + else + cur_state->create_schema_state = CS_BODY_NONE; + } + + track_begin_depth = + cur_state->identifiers[0] == 'c' && + (cur_state->identifiers[1] == 'f' || + cur_state->identifiers[1] == 'p' || + (cur_state->identifiers[1] == 'o' && + cur_state->identifiers[2] == 'r' && + (cur_state->identifiers[3] == 'f' || + cur_state->identifiers[3] == 'p')) || + (is_create_schema && + cur_state->create_schema_routine_body)); + + if (track_begin_depth && cur_state->paren_depth == 0) { if (pg_strcasecmp(yytext, "begin") == 0) @@ -973,6 +1032,9 @@ other . { if (cur_state->begin_depth > 0) cur_state->begin_depth--; + if (is_create_schema && + cur_state->begin_depth == 0) + psqlscan_reset_create_schema_body(cur_state); } } @@ -1294,6 +1356,7 @@ psql_scan_reset(PsqlScanState state) state->dolqstart = NULL; state->identifier_count = 0; state->begin_depth = 0; + psqlscan_reset_create_schema_body(state); } /* diff --git a/src/include/fe_utils/psqlscan_int.h b/src/include/fe_utils/psqlscan_int.h index 488f416f0e5..4c2b4db07c4 100644 --- a/src/include/fe_utils/psqlscan_int.h +++ b/src/include/fe_utils/psqlscan_int.h @@ -121,6 +121,13 @@ typedef struct PsqlScanStateData char identifiers[4]; /* records the first few identifiers */ int begin_depth; /* depth of begin/end pairs */ + /* + * State to track CREATE SCHEMA's nested CREATE [OR REPLACE] + * FUNCTION/PROCEDURE commands. + */ + int create_schema_state; + bool create_schema_routine_body; /* saw nested function/procedure */ + /* * Callback functions provided by the program making use of the lexer, * plus a void* callback passthrough argument. -- 2.50.1 (Apple Git-155) =
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
@ 2026-06-22 16:04  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2026-06-22 16:04 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>; jian he <jian.universality@gmail.com>

Chao Li <li.evan.chao@gmail.com> writes:
> See this repro:
> ```
> evantest=# CREATE SCHEMA s CREATE VIEW begin AS SELECT 1;
> evantest-# end;
> WARNING:  there is no transaction in progress
> CREATE SCHEMA
> COMMIT
> ```

Meh.  Yeah, you can fool psql by using non-reserved keywords as
identifiers.  I'm not super excited about adding complexity that
resolves specific edge cases of that kind, because there will always
be more of them.  As an example, this fools both HEAD and the back
branches:

regression=# create function begin() returns int
regression-# begin atomic return 2+2; end;
regression-# 

Is it really likely that anyone will use "begin" as an object name?

Having said that, I agree that what psqlscan.l is doing now is quite
hokey and maybe we should make it smarter.  I don't like the details
of your patch too much though.  It's too obviously bolted on: the
added parsing logic looks nothing like what was there before.
Also, I think it can still be fooled by

      create schema s
        create function f() ... as $$ function body $$
	create view begin as ...

because you don't reset create_schema_routine_body unless you
see "end".

What I'd think about doing, if you want to pursue this, is to
have a buffer similar to cur_state->identifiers[] but it tracks
identifiers starting from the most recent CREATE sub-clause
start within CREATE SCHEMA (ie, a non-nested CREATE keyword),
and then applies basically the same logic as in the original code
to decide if we're within a CREATE FUNCTION sub-clause.

Also, you could avoid cluttering other productions by resetting
all the new state when identifier_count is zero, in the same place
where we reset cur_state->identifiers[] today.

It'd likely be appropriate to pull all this logic out of the
{identifier} production and put it in a subroutine, just because
it's getting unduly complicated.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
@ 2026-06-22 23:08  Chao Li <li.evan.chao@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Chao Li @ 2026-06-22 23:08 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>; jian he <jian.universality@gmail.com>



> On Jun 23, 2026, at 00:04, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 
> Chao Li <li.evan.chao@gmail.com> writes:
>> See this repro:
>> ```
>> evantest=# CREATE SCHEMA s CREATE VIEW begin AS SELECT 1;
>> evantest-# end;
>> WARNING:  there is no transaction in progress
>> CREATE SCHEMA
>> COMMIT
>> ```
> 
> Meh.  Yeah, you can fool psql by using non-reserved keywords as
> identifiers.  I'm not super excited about adding complexity that
> resolves specific edge cases of that kind, because there will always
> be more of them.  As an example, this fools both HEAD and the back
> branches:
> 
> regression=# create function begin() returns int
> regression-# begin atomic return 2+2; end;
> regression-# 
> 
> Is it really likely that anyone will use "begin" as an object name?
> 
> Having said that, I agree that what psqlscan.l is doing now is quite
> hokey and maybe we should make it smarter.  I don't like the details
> of your patch too much though.  It's too obviously bolted on: the
> added parsing logic looks nothing like what was there before.
> Also, I think it can still be fooled by
> 
>      create schema s
>        create function f() ... as $$ function body $$
> create view begin as ...
> 
> because you don't reset create_schema_routine_body unless you
> see "end".
> 
> What I'd think about doing, if you want to pursue this, is to
> have a buffer similar to cur_state->identifiers[] but it tracks
> identifiers starting from the most recent CREATE sub-clause
> start within CREATE SCHEMA (ie, a non-nested CREATE keyword),
> and then applies basically the same logic as in the original code
> to decide if we're within a CREATE FUNCTION sub-clause.
> 
> Also, you could avoid cluttering other productions by resetting
> all the new state when identifier_count is zero, in the same place
> where we reset cur_state->identifiers[] today.
> 
> It'd likely be appropriate to pull all this logic out of the
> {identifier} production and put it in a subroutine, just because
> it's getting unduly complicated.
> 
> regards, tom lane

Thanks for the detailed explanation. I will try to rework the patch along the direction you suggested.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/









^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
@ 2026-06-23 08:13  Chao Li <li.evan.chao@gmail.com>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Chao Li @ 2026-06-23 08:13 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>; jian he <jian.universality@gmail.com>



> On Jun 23, 2026, at 07:08, Chao Li <li.evan.chao@gmail.com> wrote:
> 
> 
> 
>> On Jun 23, 2026, at 00:04, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> 
>> Chao Li <li.evan.chao@gmail.com> writes:
>>> See this repro:
>>> ```
>>> evantest=# CREATE SCHEMA s CREATE VIEW begin AS SELECT 1;
>>> evantest-# end;
>>> WARNING:  there is no transaction in progress
>>> CREATE SCHEMA
>>> COMMIT
>>> ```
>> 
>> Meh.  Yeah, you can fool psql by using non-reserved keywords as
>> identifiers.  I'm not super excited about adding complexity that
>> resolves specific edge cases of that kind, because there will always
>> be more of them.  As an example, this fools both HEAD and the back
>> branches:
>> 
>> regression=# create function begin() returns int
>> regression-# begin atomic return 2+2; end;
>> regression-# 
>> 
>> Is it really likely that anyone will use "begin" as an object name?
>> 
>> Having said that, I agree that what psqlscan.l is doing now is quite
>> hokey and maybe we should make it smarter.  I don't like the details
>> of your patch too much though.  It's too obviously bolted on: the
>> added parsing logic looks nothing like what was there before.
>> Also, I think it can still be fooled by
>> 
>>     create schema s
>>       create function f() ... as $$ function body $$
>> create view begin as ...
>> 
>> because you don't reset create_schema_routine_body unless you
>> see "end".
>> 
>> What I'd think about doing, if you want to pursue this, is to
>> have a buffer similar to cur_state->identifiers[] but it tracks
>> identifiers starting from the most recent CREATE sub-clause
>> start within CREATE SCHEMA (ie, a non-nested CREATE keyword),
>> and then applies basically the same logic as in the original code
>> to decide if we're within a CREATE FUNCTION sub-clause.
>> 
>> Also, you could avoid cluttering other productions by resetting
>> all the new state when identifier_count is zero, in the same place
>> where we reset cur_state->identifiers[] today.
>> 
>> It'd likely be appropriate to pull all this logic out of the
>> {identifier} production and put it in a subroutine, just because
>> it's getting unduly complicated.
>> 
>> regards, tom lane
> 
> Thanks for the detailed explanation. I will try to rework the patch along the direction you suggested.
> 

Following your suggestion, I moved the "{identifier}" logic into a new helper psqlscan_track_identifier(), and added “create_schema_identifiers" to PsqlScanStateData to track identifiers from the current top-level CREATE element within CREATE SCHEMA in the same way as the top level “identifiers". I also added a few more test cases.

Please see the attached v2 for details.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v2-0001-psql-Fix-CREATE-SCHEMA-scanning-with-object-names.patch (application/octet-stream, 11.3K) download From 8a4fc0a11b63cf10b5b5f0784bf3cbff5c4aa97d Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" <lic@highgo.com> Date: Mon, 22 Jun 2026 15:33:04 +0800 Subject: [PATCH v2] psql: Fix CREATE SCHEMA scanning with object names like "begin" Since d51697484, psql's scanner treats CREATE SCHEMA as a command that may contain SQL-standard routine bodies, so that semicolons inside BEGIN ATOMIC ... END blocks do not terminate the command too early. However, the heuristic enabled BEGIN/END tracking for every CREATE SCHEMA statement. As a result, an object name such as "begin" could make interactive psql think the statement was still incomplete, for example: CREATE SCHEMA s CREATE VIEW begin AS SELECT 1; Fix this by tracking identifiers from each top-level CREATE schema element separately while scanning CREATE SCHEMA, and applying the existing CREATE [OR REPLACE] FUNCTION/PROCEDURE heuristic to the current schema element rather than to the whole CREATE SCHEMA statement. This intentionally leaves the existing top-level CREATE FUNCTION/PROCEDURE heuristic unchanged. Add interactive psql tests for false-positive cases and for nested SQL routine bodies inside CREATE SCHEMA. Author: Chao Li <lic@highgo.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com --- src/bin/psql/t/001_basic.pl | 76 ++++++++++++ src/fe_utils/psqlscan.l | 173 +++++++++++++++++++--------- src/include/fe_utils/psqlscan_int.h | 5 + 3 files changed, 199 insertions(+), 55 deletions(-) diff --git a/src/bin/psql/t/001_basic.pl b/src/bin/psql/t/001_basic.pl index bbd330216ae..c7dd50bafd6 100644 --- a/src/bin/psql/t/001_basic.pl +++ b/src/bin/psql/t/001_basic.pl @@ -72,6 +72,82 @@ max_wal_senders = 4 }); $node->start; +{ + my $h = $node->interactive_psql('postgres'); + $h->set_query_timer_restart(); + + my $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA IF EXISTS psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema CREATE VIEW begin AS SELECT 1;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with object named begin ends at semicolon'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE VIEW v AS SELECT 1 AS function, 2 AS begin;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with column alias function and begin ends at semicolon'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE FUNCTION f() RETURNS int LANGUAGE sql AS \$\$ SELECT 1; \$\$\n" + . " CREATE VIEW begin AS SELECT 1;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with SQL function and later object named begin ends at semicolon'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE PROCEDURE p()\n" + . " BEGIN ATOMIC\n" + . " SELECT 1;\n" + . " END;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with SQL procedure body waits for END'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE FUNCTION f_case() RETURNS int\n" + . " BEGIN ATOMIC\n" + . " RETURN CASE WHEN true THEN 1 ELSE 0 END;\n" + . " END;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with SQL function body containing CASE waits for routine END'); + + $out = $h->query_until( + qr/^CREATE SCHEMA\r?$/m, + "DROP SCHEMA psql_scan_schema CASCADE;\n" + . "CREATE SCHEMA psql_scan_schema\n" + . " CREATE OR REPLACE FUNCTION f() RETURNS int\n" + . " BEGIN ATOMIC\n" + . " SELECT 1;\n" + . " END;\n"); + like( + $out, + qr/^CREATE SCHEMA\r?$/m, + 'CREATE SCHEMA with OR REPLACE SQL function body waits for END'); + + $h->quit or die "psql returned $?"; +} + psql_like($node, '\copyright', qr/Copyright/, '\copyright'); psql_like($node, '\help', qr/ALTER/, '\help without arguments'); psql_like($node, '\help SELECT', qr/SELECT/, '\help with argument'); diff --git a/src/fe_utils/psqlscan.l b/src/fe_utils/psqlscan.l index d29dda4d8e1..722924688c6 100644 --- a/src/fe_utils/psqlscan.l +++ b/src/fe_utils/psqlscan.l @@ -61,6 +61,122 @@ typedef int YYSTYPE; #define ECHO psqlscan_emit(cur_state, yytext, yyleng) +static void +psqlscan_reset_create_schema_identifiers(PsqlScanState state) +{ + memset(state->create_schema_identifiers, 0, + sizeof(state->create_schema_identifiers)); + state->create_schema_identifier_count = 0; +} + +static void +psqlscan_record_create_keyword(char *identifiers, int *identifier_count, + int identifiers_size, const char *identifier) +{ + if (*identifier_count < identifiers_size) + { + /* + * "schema" is needed for recognizing top-level CREATE SCHEMA. It is + * harmless in the CREATE SCHEMA subcommand buffer. + */ + if (pg_strcasecmp(identifier, "create") == 0 || + pg_strcasecmp(identifier, "function") == 0 || + pg_strcasecmp(identifier, "procedure") == 0 || + pg_strcasecmp(identifier, "or") == 0 || + pg_strcasecmp(identifier, "replace") == 0 || + pg_strcasecmp(identifier, "schema") == 0) + identifiers[*identifier_count] = + pg_tolower((unsigned char) identifier[0]); + } + + (*identifier_count)++; +} + +static bool +psqlscan_is_create_routine(const char *identifiers) +{ + return identifiers[0] == 'c' && + (identifiers[1] == 'f' || identifiers[1] == 'p' || + (identifiers[1] == 'o' && identifiers[2] == 'r' && + (identifiers[3] == 'f' || identifiers[3] == 'p'))); +} + +/* + * Track whether we are inside a BEGIN .. END block in a function definition, + * so that semicolons contained therein don't terminate the whole statement. + * Short of writing a full parser here, the following heuristic should work. + * We track whether the beginning of the statement matches CREATE [OR REPLACE] + * {FUNCTION|PROCEDURE}. For CREATE SCHEMA, track BEGIN .. END blocks only + * after recognizing an embedded CREATE [OR REPLACE] FUNCTION/PROCEDURE + * subcommand. + */ +static void +psqlscan_track_identifier(PsqlScanState state, const char *identifier) +{ + bool is_create_schema; + bool track_begin_depth; + + if (state->identifier_count == 0) + { + memset(state->identifiers, 0, sizeof(state->identifiers)); + psqlscan_reset_create_schema_identifiers(state); + } + + psqlscan_record_create_keyword(state->identifiers, + &state->identifier_count, + sizeof(state->identifiers), + identifier); + + is_create_schema = + state->identifiers[0] == 'c' && + state->identifiers[1] == 's'; + + /* + * In CREATE SCHEMA, track identifiers from each top-level CREATE schema + * element separately, so that BEGIN/END tracking is enabled only for nested + * CREATE [OR REPLACE] FUNCTION/PROCEDURE elements. This avoids treating + * identifiers appearing in expressions as schema elements. + */ + if (is_create_schema && + state->paren_depth == 0 && + state->begin_depth == 0) + { + if (pg_strcasecmp(identifier, "create") == 0) + psqlscan_reset_create_schema_identifiers(state); + + psqlscan_record_create_keyword(state->create_schema_identifiers, + &state->create_schema_identifier_count, + sizeof(state->create_schema_identifiers), + identifier); + } + + track_begin_depth = + psqlscan_is_create_routine(state->identifiers) || + (is_create_schema && + psqlscan_is_create_routine(state->create_schema_identifiers)); + + if (track_begin_depth && + state->paren_depth == 0) + { + if (pg_strcasecmp(identifier, "begin") == 0) + state->begin_depth++; + else if (pg_strcasecmp(identifier, "case") == 0) + { + /* + * CASE also ends with END. We only need to track this if we are + * already inside a BEGIN. + */ + if (state->begin_depth >= 1) + state->begin_depth++; + } + else if (pg_strcasecmp(identifier, "end") == 0) + { + if (state->begin_depth > 0) + state->begin_depth--; + } + } +} + %} %option reentrant @@ -921,61 +1037,7 @@ other . {identifier} { - /* - * We need to track if we are inside a BEGIN .. END block - * in a function definition, so that semicolons contained - * therein don't terminate the whole statement. Short of - * writing a full parser here, the following heuristic - * should work. First, we track whether the beginning of - * the statement matches CREATE [OR REPLACE] - * {FUNCTION|PROCEDURE|SCHEMA}. (Allowing this in - * CREATE SCHEMA, without tracking whether we're within a - * CREATE FUNCTION/PROCEDURE subcommand, is a bit shaky - * but should be okay with the present set of valid - * subcommands.) - */ - - if (cur_state->identifier_count == 0) - memset(cur_state->identifiers, 0, sizeof(cur_state->identifiers)); - - if (cur_state->identifier_count < sizeof(cur_state->identifiers)) - { - if (pg_strcasecmp(yytext, "create") == 0 || - pg_strcasecmp(yytext, "function") == 0 || - pg_strcasecmp(yytext, "procedure") == 0 || - pg_strcasecmp(yytext, "or") == 0 || - pg_strcasecmp(yytext, "replace") == 0 || - pg_strcasecmp(yytext, "schema") == 0) - cur_state->identifiers[cur_state->identifier_count] = pg_tolower((unsigned char) yytext[0]); - } - - cur_state->identifier_count++; - - if (cur_state->identifiers[0] == 'c' && - (cur_state->identifiers[1] == 'f' || cur_state->identifiers[1] == 'p' || - (cur_state->identifiers[1] == 'o' && cur_state->identifiers[2] == 'r' && - (cur_state->identifiers[3] == 'f' || cur_state->identifiers[3] == 'p')) || - cur_state->identifiers[1] == 's') && - cur_state->paren_depth == 0) - { - if (pg_strcasecmp(yytext, "begin") == 0) - cur_state->begin_depth++; - else if (pg_strcasecmp(yytext, "case") == 0) - { - /* - * CASE also ends with END. We only need to track - * this if we are already inside a BEGIN. - */ - if (cur_state->begin_depth >= 1) - cur_state->begin_depth++; - } - else if (pg_strcasecmp(yytext, "end") == 0) - { - if (cur_state->begin_depth > 0) - cur_state->begin_depth--; - } - } - + psqlscan_track_identifier(cur_state, yytext); ECHO; } @@ -1294,6 +1356,7 @@ psql_scan_reset(PsqlScanState state) state->dolqstart = NULL; state->identifier_count = 0; state->begin_depth = 0; + psqlscan_reset_create_schema_identifiers(state); } /* diff --git a/src/include/fe_utils/psqlscan_int.h b/src/include/fe_utils/psqlscan_int.h index 488f416f0e5..0d8b9034028 100644 --- a/src/include/fe_utils/psqlscan_int.h +++ b/src/include/fe_utils/psqlscan_int.h @@ -121,6 +121,11 @@ typedef struct PsqlScanStateData char identifiers[4]; /* records the first few identifiers */ int begin_depth; /* depth of begin/end pairs */ + int create_schema_identifier_count; /* first few identifiers of a + * CREATE SCHEMA element */ + char create_schema_identifiers[4]; /* count for + * create_schema_identifiers */ + /* * Callback functions provided by the program making use of the lexer, * plus a void* callback passthrough argument. -- 2.50.1 (Apple Git-155) =
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
@ 2026-06-23 18:16  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2026-06-23 18:16 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>; jian he <jian.universality@gmail.com>

Chao Li <li.evan.chao@gmail.com> writes:
> Following your suggestion, I moved the "{identifier}" logic into a new helper psqlscan_track_identifier(), and added “create_schema_identifiers" to PsqlScanStateData to track identifiers from the current top-level CREATE element within CREATE SCHEMA in the same way as the top level “identifiers". I also added a few more test cases.

> Please see the attached v2 for details.

Pushed with some mostly-cosmetic changes.  Notably, I renamed the
existing "identifiers" field to help distinguish it from the new one.

I didn't use your test cases because it seemed unduly expensive to do
it that way.  We can perfectly well exercise this as part of plain-SQL
regression testing, so I just added a booby-trap case in
create_schema.sql.  Without this fix, psql marches on and tries to run
the various \d commands after the CREATE SCHEMA before it's sent the
CREATE SCHEMA command.  So even though no error is reported, the
output is quite different.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
@ 2026-06-24 00:49  Chao Li <li.evan.chao@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 6+ messages in thread

From: Chao Li @ 2026-06-24 00:49 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>; Kirill Reshke <reshkekirill@gmail.com>; jian he <jian.universality@gmail.com>



> On Jun 24, 2026, at 02:16, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 
> Chao Li <li.evan.chao@gmail.com> writes:
>> Following your suggestion, I moved the "{identifier}" logic into a new helper psqlscan_track_identifier(), and added “create_schema_identifiers" to PsqlScanStateData to track identifiers from the current top-level CREATE element within CREATE SCHEMA in the same way as the top level “identifiers". I also added a few more test cases.
> 
>> Please see the attached v2 for details.
> 
> Pushed with some mostly-cosmetic changes.

Thanks for pushing.

> Notably, I renamed the
> existing "identifiers" field to help distinguish it from the new one.
> 

The renaming makes sense.

> I didn't use your test cases because it seemed unduly expensive to do
> it that way.  We can perfectly well exercise this as part of plain-SQL
> regression testing, so I just added a booby-trap case in
> create_schema.sql Without this fix, psql marches on and tries to run
> the various \d commands after the CREATE SCHEMA before it's sent the
> CREATE SCHEMA command.  So even though no error is reported, the
> output is quite different.
> 

I think I over worried about the test. Thank for simplifying the tests.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/









^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-06-24 00:49 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-22 09:34 psql: Fix CREATE SCHEMA scanning of nested routine bodies Chao Li <li.evan.chao@gmail.com>
2026-06-22 16:04 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-06-22 23:08   ` Chao Li <li.evan.chao@gmail.com>
2026-06-23 08:13     ` Chao Li <li.evan.chao@gmail.com>
2026-06-23 18:16       ` Tom Lane <tgl@sss.pgh.pa.us>
2026-06-24 00:49         ` Chao Li <li.evan.chao@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox