From: Chao Li <li.evan.chao@gmail.com>
To: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Kirill Reshke <reshkekirill@gmail.com>
Cc: jian he <jian.universality@gmail.com>
Subject: psql: Fix CREATE SCHEMA scanning of nested routine bodies
Date: Mon, 22 Jun 2026 17:34:53 +0800
Message-ID: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com> (raw)
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.plindex 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.lindex 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.hindex 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)
=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: li.evan.chao@gmail.com, tgl@sss.pgh.pa.us, reshkekirill@gmail.com, jian.universality@gmail.com
Subject: Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
In-Reply-To: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com>
References: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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