Обсуждение: Bug of psql meta-command \sf & \sv

Поиск
Список
Период
Сортировка

Bug of psql meta-command \sf & \sv

От
Jet Zhang
Дата:

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

 

 

 

章晨曦  Jet C.X. ZHANG

18657181679

易景科技 http://www.halodbtech.com

浙江省杭州市滨江区长河街道建业路511号华创大厦5505

 

 

Вложения

Re: Bug of psql meta-command \sf & \sv

От
Daniel Gustafsson
Дата:
> 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




RE: Bug of psql meta-command \sf & \sv

От
Jet Zhang
Дата:

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)

 

 

 

章晨曦  Jet C.X. ZHANG

18657181679

易景科技 http://www.halodbtech.com

浙江省杭州市滨江区长河街道建业路511号华创大厦5505

 

 


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

Вложения

Re: Bug of psql meta-command \sf & \sv

От
Japin Li
Дата:
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.



RE: Bug of psql meta-command \sf & \sv

От
Jet Zhang
Дата:

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

 

 

 

章晨曦  Jet C.X. ZHANG

18657181679

易景科技 http://www.halodbtech.com

浙江省杭州市滨江区长河街道建业路511号华创大厦5505

 

 

From: Japin Li
Sent: 2023
927 18:33
To: Jet Zhang
Cc: Daniel Gustafsson; 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.

 

Вложения

Re: Bug of psql meta-command \sf & \sv

От
"咸�9�5"
Дата:
I don't think this meets the documentation's definition of an identifier.  Semicolons should not be used this way here.(Excuse me, I need to test the email function.)

 


------------------ Original ------------------
From: "Daniel Gustafsson" <daniel@yesql.se>;
Date: Wed, Sep 27, 2023 05:13 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




Re: Bug of psql meta-command \sf & \sv

От
Japin Li
Дата:
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.  Semicolons should not be used this
wayhere.(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.