Bug of psql meta-command \sf & \sv

Started by Jet Zhangover 2 years ago7 messagesbugs
Jump to latest
#1Jet Zhang
jet.cx.zhang@hotmail.com

Hi there,

The psql meta-commands \sf and \sv have a minor bug, for example:

postgres=# CREATE PROCEDURE test () AS $$ BEGIN NULL; END; $$ LANGUAGE plpgsql; -- create a procedure
postgres=# \sf test
CREATE OR REPLACE PROCEDURE public.test()
LANGUAGE plpgsql
AS $procedure$ BEGIN NULL; END; $procedure$

We can use \sf to check the souce of test, but if we use:
postgres=# \sf test;
2023-09-27 16:51:58.632 CST [3460153] ERROR: function "test;" does not exist at character 8
2023-09-27 16:51:58.632 CST [3460153] STATEMENT: SELECT 'test;'::pg_catalog.regproc::pg_catalog.oid
ERROR: function "test;" does not exist

The \sf feedback an error.

And the same error also exists on \sv command:
postgres=# CREATE VIEW v_test AS SELECT * FROM pg_class;
postgres=# \sv v_test
CREATE OR REPLACE VIEW public.v_test AS
SELECT pg_class.oid,
pg_class.relname,
pg_class.relnamespace,
pg_class.reltype,
pg_class.reloftype,
pg_class.relowner,
pg_class.relam,
pg_class.relfilenode,
pg_class.reltablespace,
pg_class.relpages,
pg_class.reltuples,
pg_class.relallvisible,
pg_class.reltoastrelid,
pg_class.relhasindex,
pg_class.relisshared,
pg_class.relpersistence,
pg_class.relkind,
pg_class.relnatts,
pg_class.relchecks,
pg_class.relhasrules,
pg_class.relhastriggers,
pg_class.relhassubclass,
pg_class.relrowsecurity,
pg_class.relforcerowsecurity,
pg_class.relispopulated,
pg_class.relreplident,
pg_class.relispartition,
pg_class.relrewrite,
pg_class.relfrozenxid,
pg_class.relminmxid,
pg_class.relacl,
pg_class.reloptions,
pg_class.relpartbound
FROM pg_class
postgres=# \sv v_test;
2023-09-27 16:56:10.086 CST [3460153] ERROR: relation "v_test;" does not exist at character 8
2023-09-27 16:56:10.086 CST [3460153] STATEMENT: SELECT 'v_test;'::pg_catalog.regclass::pg_catalog.oid
ERROR: relation "v_test;" does not exist

[cid:image001.png@01D80E0F.FAC36F80]

章晨曦 Jet C.X. ZHANG
18657181679
易景科技 http://www.halodbtech.com
浙江省杭州市滨江区长河街道建业路511号华创大厦5层505

Attachments:

D34C3C37F8064725932B6933724325A3[2752723].pngimage/png; name="D34C3C37F8064725932B6933724325A3[2752723].png"Download
#2Daniel Gustafsson
daniel@yesql.se
In reply to: Jet Zhang (#1)
Re: Bug of psql meta-command \sf & \sv

On 27 Sep 2023, at 11:06, Jet Zhang <jet.cx.zhang@hotmail.com> wrote:

Hi there,

The psql meta-commands \sf and \sv have a minor bug, for example:

postgres=# CREATE PROCEDURE test () AS $$ BEGIN NULL; END; $$ LANGUAGE plpgsql; -- create a procedure
postgres=# \sf test
CREATE OR REPLACE PROCEDURE public.test()
LANGUAGE plpgsql
AS $procedure$ BEGIN NULL; END; $procedure$

We can use \sf to check the souce of test, but if we use:
postgres=# \sf test;
2023-09-27 16:51:58.632 CST [3460153] ERROR: function "test;" does not exist at character 8
2023-09-27 16:51:58.632 CST [3460153] STATEMENT: SELECT 'test;'::pg_catalog.regproc::pg_catalog.oid
ERROR: function "test;" does not exist

The \sf feedback an error.

This is not a bug, "test;" is a valid name which is distinct from test.
Semi-colon is not a meta-command terminator.

postgres=# create function "test;"() returns text as $$ begin null; end; $$ language plpgsql;
CREATE FUNCTION
postgres=# \sf test
ERROR: function "test" does not exist
postgres=# \sf test;
CREATE OR REPLACE FUNCTION public."test;"()
RETURNS text
LANGUAGE plpgsql
AS $function$ begin null; end; $function$
postgres=#

--
Daniel Gustafsson

#3Jet Zhang
jet.cx.zhang@hotmail.com
In reply to: Daniel Gustafsson (#2)
RE: Bug of psql meta-command \sf & \sv

I don’t think not a bug. As you said Semi-colon is not a meta-command terminator, but why the other meta-commands
Exp. \dt able to work with Semi-colon?

postgres=# \dt pg_class
List of relations
Schema | Name | Type | Owner
------------+----------+-------+-------
pg_catalog | pg_class | table | jet
(1 row)

postgres=# \dt pg_class;
List of relations
Schema | Name | Type | Owner
------------+----------+-------+-------
pg_catalog | pg_class | table | jet
(1 row)

[cid:image001.png@01D80E0F.FAC36F80]

章晨曦 Jet C.X. ZHANG
18657181679
易景科技 http://www.halodbtech.com
浙江省杭州市滨江区长河街道建业路511号华创大厦5层505

________________________________
From: Daniel Gustafsson <daniel@yesql.se>
Sent: Wednesday, September 27, 2023 5:13:32 PM
To: Jet Zhang <jet.cx.zhang@hotmail.com>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: Bug of psql meta-command \sf & \sv

On 27 Sep 2023, at 11:06, Jet Zhang <jet.cx.zhang@hotmail.com> wrote:

Hi there,

The psql meta-commands \sf and \sv have a minor bug, for example:

postgres=# CREATE PROCEDURE test () AS $$ BEGIN NULL; END; $$ LANGUAGE plpgsql; -- create a procedure
postgres=# \sf test
CREATE OR REPLACE PROCEDURE public.test()
LANGUAGE plpgsql
AS $procedure$ BEGIN NULL; END; $procedure$

We can use \sf to check the souce of test, but if we use:
postgres=# \sf test;
2023-09-27 16:51:58.632 CST [3460153] ERROR: function "test;" does not exist at character 8
2023-09-27 16:51:58.632 CST [3460153] STATEMENT: SELECT 'test;'::pg_catalog.regproc::pg_catalog.oid
ERROR: function "test;" does not exist

The \sf feedback an error.

This is not a bug, "test;" is a valid name which is distinct from test.
Semi-colon is not a meta-command terminator.

postgres=# create function "test;"() returns text as $$ begin null; end; $$ language plpgsql;
CREATE FUNCTION
postgres=# \sf test
ERROR: function "test" does not exist
postgres=# \sf test;
CREATE OR REPLACE FUNCTION public."test;"()
RETURNS text
LANGUAGE plpgsql
AS $function$ begin null; end; $function$
postgres=#

--
Daniel Gustafsson

Attachments:

D34C3C37F8064725932B6933724325A3[2757760].pngimage/png; name="D34C3C37F8064725932B6933724325A3[2757760].png"Download
#4Japin Li
japinli@hotmail.com
In reply to: Jet Zhang (#3)
Re: Bug of psql meta-command \sf & \sv

On Wed, 27 Sep 2023 at 17:25, Jet Zhang <jet.cx.zhang@hotmail.com> wrote:

I don’t think not a bug. As you said Semi-colon is not a meta-command terminator, but why the other meta-commands
Exp. \dt able to work with Semi-colon?

postgres=# \dt pg_class
List of relations
Schema | Name | Type | Owner
------------+----------+-------+-------
pg_catalog | pg_class | table | jet
(1 row)

postgres=# \dt pg_class;
List of relations
Schema | Name | Type | Owner
------------+----------+-------+-------
pg_catalog | pg_class | table | jet
(1 row)

The \sf use the whole line as its input, it can accept more complex input,
for example:

postgres=# \sf test (int)
CREATE OR REPLACE PROCEDURE public.test(IN id integer)
LANGUAGE plpgsql
AS $procedure$ BEGIN NULL; END; $procedure$
postgres=# \sf test (int, text)
CREATE OR REPLACE PROCEDURE public.test(IN id integer, IN info text)
LANGUAGE plpgsql
AS $procedure$ BEGIN NULL; END; $procedure$

Same as \ef.

OTOH, \ev and \sv use same function as \ef and \sf, See exec_command_ef_ev()
and exec_command_sf_sv() in src/bin/psql/common.c file.

static backslashResult
exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
const char *cmd, bool is_func)
{
backslashResult status = PSQL_CMD_SKIP_LINE;

if (active_branch)
{
bool show_linenumbers = (strchr(cmd, '+') != NULL);
PQExpBuffer buf;
char *obj_desc;
Oid obj_oid = InvalidOid;
EditableObjectType eot = is_func ? EditableFunction : EditableView;

buf = createPQExpBuffer();
obj_desc = psql_scan_slash_option(scan_state,
OT_WHOLE_LINE, NULL, true);
^ -- use the whole line

Maybe we can split \sf and \sv, however, I'm not sure it worth.

--
Regrads,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.

#5Jet Zhang
jet.cx.zhang@hotmail.com
In reply to: Japin Li (#4)
RE: Bug of psql meta-command \sf & \sv

Yes, you’re right. Maybe we can spend some effort to strength the function psgl_scan_slash_option.

[cid:image001.png@01D80E0F.FAC36F80]

章晨曦 Jet C.X. ZHANG
18657181679
易景科技 http://www.halodbtech.com
浙江省杭州市滨江区长河街道建业路511号华创大厦5层505

From: Japin Li<mailto:japinli@hotmail.com>
Sent: 2023年9月27日 18:33
To: Jet Zhang<mailto:jet.cx.zhang@hotmail.com>
Cc: Daniel Gustafsson<mailto:daniel@yesql.se>; pgsql-bugs@lists.postgresql.org<mailto:pgsql-bugs@lists.postgresql.org>
Subject: Re: Bug of psql meta-command \sf & \sv

On Wed, 27 Sep 2023 at 17:25, Jet Zhang <jet.cx.zhang@hotmail.com> wrote:

I don’t think not a bug. As you said Semi-colon is not a meta-command terminator, but why the other meta-commands
Exp. \dt able to work with Semi-colon?

postgres=# \dt pg_class
List of relations
Schema | Name | Type | Owner
------------+----------+-------+-------
pg_catalog | pg_class | table | jet
(1 row)

postgres=# \dt pg_class;
List of relations
Schema | Name | Type | Owner
------------+----------+-------+-------
pg_catalog | pg_class | table | jet
(1 row)

The \sf use the whole line as its input, it can accept more complex input,
for example:

postgres=# \sf test (int)
CREATE OR REPLACE PROCEDURE public.test(IN id integer)
LANGUAGE plpgsql
AS $procedure$ BEGIN NULL; END; $procedure$
postgres=# \sf test (int, text)
CREATE OR REPLACE PROCEDURE public.test(IN id integer, IN info text)
LANGUAGE plpgsql
AS $procedure$ BEGIN NULL; END; $procedure$

Same as \ef.

OTOH, \ev and \sv use same function as \ef and \sf, See exec_command_ef_ev()
and exec_command_sf_sv() in src/bin/psql/common.c file.

static backslashResult
exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
const char *cmd, bool is_func)
{
backslashResult status = PSQL_CMD_SKIP_LINE;

if (active_branch)
{
bool show_linenumbers = (strchr(cmd, '+') != NULL);
PQExpBuffer buf;
char *obj_desc;
Oid obj_oid = InvalidOid;
EditableObjectType eot = is_func ? EditableFunction : EditableView;

buf = createPQExpBuffer();
obj_desc = psql_scan_slash_option(scan_state,
OT_WHOLE_LINE, NULL, true);
^ -- use the whole line

Maybe we can split \sf and \sv, however, I'm not sure it worth.

--
Regrads,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.

Attachments:

D34C3C37F8064725932B6933724325A3[2761581].pngimage/png; name="D34C3C37F8064725932B6933724325A3[2761581].png"Download
#6咸🐟
2437705447@qq.com
In reply to: Daniel Gustafsson (#2)
Re: Bug of psql meta-command \sf & \sv

https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
I don't think this meets the documentation's definition of an identifier.&nbsp; Semicolons should not be used this way here.(Excuse me, I need to test the email function.)

咸?
2437705447@qq.com

&nbsp;

------------------&nbsp;Original&nbsp;------------------
From: "Daniel Gustafsson" <daniel@yesql.se&gt;;
Date:&nbsp;Wed, Sep 27, 2023 05:13 PM
To:&nbsp;"Jet Zhang"<jet.cx.zhang@hotmail.com&gt;;
Cc:&nbsp;"pgsql-bugs@lists.postgresql.org"<pgsql-bugs@lists.postgresql.org&gt;;
Subject:&nbsp;Re: Bug of psql meta-command \sf &amp; \sv

&gt; On 27 Sep 2023, at 11:06, Jet Zhang <jet.cx.zhang@hotmail.com&gt; wrote:
&gt;
&gt; Hi there,
&gt;&nbsp;
&gt; The psql meta-commands \sf and \sv have a minor bug, for example:
&gt;&nbsp;
&gt; postgres=# CREATE PROCEDURE test () AS $$ BEGIN NULL; END; $$ LANGUAGE plpgsql;&nbsp;&nbsp; -- create a procedure
&gt; postgres=# \sf test
&gt; CREATE OR REPLACE PROCEDURE public.test()
&gt; LANGUAGE plpgsql
&gt; AS $procedure$ BEGIN NULL; END; $procedure$
&gt;&nbsp;
&gt; We can use \sf to check the souce of test, but if we use:
&gt; postgres=# \sf test;
&gt; 2023-09-27 16:51:58.632 CST [3460153] ERROR:&nbsp; function "test;" does not exist at character 8
&gt; 2023-09-27 16:51:58.632 CST [3460153] STATEMENT:&nbsp; SELECT 'test;'::pg_catalog.regproc::pg_catalog.oid
&gt; ERROR:&nbsp; function "test;" does not exist
&gt;&nbsp;
&gt; The \sf feedback an error.

This is not a bug, "test;" is a valid name which is distinct from test.
Semi-colon is not a meta-command terminator.

postgres=# create function "test;"() returns text as $$ begin null; end; $$ language plpgsql;
CREATE FUNCTION
postgres=# \sf test
ERROR:&nbsp; function "test" does not exist
postgres=# \sf test;
CREATE OR REPLACE FUNCTION public."test;"()
&nbsp;RETURNS text
&nbsp;LANGUAGE plpgsql
AS $function$ begin null; end; $function$
postgres=#

--
Daniel Gustafsson

#7Japin Li
japinli@hotmail.com
In reply to: 咸🐟 (#6)
Re: Bug of psql meta-command \sf & \sv

On Wed, 27 Sep 2023 at 20:35, 咸🐟 <2437705447@qq.com> wrote:

https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
I don't think this meets the documentation's definition of an identifier.&nbsp; Semicolons should not be used this way here.(Excuse me, I need to test the email function.)

In PostgreSQL, quoted identifiers can contain any character, except the
character with code zero. For example:

postgres=# CREATE table "<>!@#$%^&*()" ("*&-=+" int, "?>.,\/" text);
CREATE TABLE
postgres=# \d "<>!@#$%^&*()"
Table "public.<>!@#$%^&*()"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
*&-=+ | integer | | |
?>.,\/ | text | | |

postgres=# INSERT INTO "<>!@#$%^&*()" VALUES (1, 'hello world');
INSERT 0 1
postgres=# SELECT * FROM "<>!@#$%^&*()";
*&-=+ | ?>.,\/
-------+-------------
1 | hello world
(1 row)

--
Regrads,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.