BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

Started by PG Bug reporting formover 2 years ago8 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 18195
Logged by: Pavel Kulakov
Email address: paul.kulakov@systematica.ru
PostgreSQL version: 15.5
Operating system: Debian GNU/Linux 11
Description:

1. The following code is successfully executed although it has incorrect
syntax: there must be comma (,) between _n and _s in 'into' section.
The output is "_n = 1, _s = <NULL>"

2. Documentation
(https://www.postgresql.org/docs/current/plpgsql-statements.html) in 43.5.3.
says "the command's result columns must exactly match the structure of the
target as to number and data types, or else a run-time error occurs". But we
see that number of columns can differ from number of variables.
I think either the documentation or the source code should be fixed.

do $sql$
declare _n int; _s text;
begin
select 1, 'string1', 'string2'
into _n _s;

raise notice '_n = %, _s = %', _n, _s;
end;
$sql$;

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: PG Bug reporting form (#1)
Re: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

PG Bug reporting form <noreply@postgresql.org> writes:

1. The following code is successfully executed although it has incorrect
syntax: there must be comma (,) between _n and _s in 'into' section.

select 1, 'string1', 'string2'
into _n _s;

I believe this is being read the same as

select 1, 'string1', 'string2' _s into _n;

That is, the lack of a comma causes the INTO sub-clause to end,
and then _s is taken as an AS-less column label. As the manual
explains, for backwards-compatibility reasons we allow INTO to be
embedded anywhere in the command, even though that leads to
surprising-looking cases like this one.

As for the question of why you don't get an error for the wrong
number of INTO targets, again that's backwards compatibility.
There's a "strict_multi_assignment" check you can turn on to make
it complain about that [1]https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQL-EXTRA-CHECKS.

regards, tom lane

[1]: https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQL-EXTRA-CHECKS

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#2)
Re: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

Hi

út 14. 11. 2023 v 16:31 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

PG Bug reporting form <noreply@postgresql.org> writes:

1. The following code is successfully executed although it has incorrect
syntax: there must be comma (,) between _n and _s in 'into' section.

select 1, 'string1', 'string2'
into _n _s;

I believe this is being read the same as

select 1, 'string1', 'string2' _s into _n;

That is, the lack of a comma causes the INTO sub-clause to end,
and then _s is taken as an AS-less column label. As the manual
explains, for backwards-compatibility reasons we allow INTO to be
embedded anywhere in the command, even though that leads to
surprising-looking cases like this one.

As for the question of why you don't get an error for the wrong
number of INTO targets, again that's backwards compatibility.
There's a "strict_multi_assignment" check you can turn on to make
it complain about that [1].

regards, tom lane

[1]
https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQL-EXTRA-CHECKS

The extension plpgsql_check https://github.com/okbob/plpgsql_check can
raise warnings for these cases too

Regards

Pavel

#4Pavel Kulakov
paul.kulakov@systematica.ru
In reply to: Tom Lane (#2)
RE: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

Hi Tom,

Thanks for your answer.
I set plpgsql.extra_errors = 'strict_multi_assignment'

Now, how to explain the following working?

do $sql$
declare
_n int; _s text;
begin
select 1 into _n _s;
raise notice '_n = %, _s = %', _n, _s;
end;
$sql$;

Regards,
Pavel

-----Original Message-----
From: Tom Lane <tgl@sss.pgh.pa.us>
Sent: Tuesday, November 14, 2023 6:31 PM
To: paul.kulakov@systematica.ru
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO
statement

PG Bug reporting form <noreply@postgresql.org> writes:

1. The following code is successfully executed although it has
incorrect
syntax: there must be comma (,) between _n and _s in 'into' section.

select 1, 'string1', 'string2'
into _n _s;

I believe this is being read the same as

select 1, 'string1', 'string2' _s into _n;

That is, the lack of a comma causes the INTO sub-clause to end, and then _s
is taken as an AS-less column label. As the manual explains, for
backwards-compatibility reasons we allow INTO to be embedded anywhere in the
command, even though that leads to surprising-looking cases like this one.

As for the question of why you don't get an error for the wrong number of
INTO targets, again that's backwards compatibility.
There's a "strict_multi_assignment" check you can turn on to make it
complain about that [1]https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQ L-EXTRA-CHECKS.

regards, tom lane

[1]: https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQ L-EXTRA-CHECKS
https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQ
L-EXTRA-CHECKS

#5Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Kulakov (#4)
Re: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

Hi

st 15. 11. 2023 v 8:23 odesílatel Pavel Kulakov <paul.kulakov@systematica.ru>
napsal:

Hi Tom,

Thanks for your answer.
I set plpgsql.extra_errors = 'strict_multi_assignment'

Now, how to explain the following working?

do $sql$
declare
_n int; _s text;
begin
select 1 into _n _s;
raise notice '_n = %, _s = %', _n, _s;
end;
$sql$;

looks so parser is not sensitive to garbage after variable name

(2023-11-15 08:27:11) postgres=# do $sql$
declare
_n int; _s text;
begin
select 1 into _n somegarbage;
raise notice '_n = %, _s = %', _n, _s;
end;
$sql$;
NOTICE: _n = 1, _s = <NULL>
DO

Show quoted text

Regards,
Pavel

-----Original Message-----
From: Tom Lane <tgl@sss.pgh.pa.us>
Sent: Tuesday, November 14, 2023 6:31 PM
To: paul.kulakov@systematica.ru
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO
statement

PG Bug reporting form <noreply@postgresql.org> writes:

1. The following code is successfully executed although it has
incorrect
syntax: there must be comma (,) between _n and _s in 'into' section.

select 1, 'string1', 'string2'
into _n _s;

I believe this is being read the same as

select 1, 'string1', 'string2' _s into _n;

That is, the lack of a comma causes the INTO sub-clause to end, and then _s
is taken as an AS-less column label. As the manual explains, for
backwards-compatibility reasons we allow INTO to be embedded anywhere in
the
command, even though that leads to surprising-looking cases like this one.

As for the question of why you don't get an error for the wrong number of
INTO targets, again that's backwards compatibility.
There's a "strict_multi_assignment" check you can turn on to make it
complain about that [1].

regards, tom lane

[1]

https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQ
L-EXTRA-CHECKS
<https://www.postgresql.org/docs/current/plpgsql-development-tips.html#PLPGSQL-EXTRA-CHECKS&gt;

#6Pavel Kulakov
paul.kulakov@systematica.ru
In reply to: PG Bug reporting form (#1)
RE: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

Hi Pavel,

Now I see what Tom said: this 'garbage' becomes an 'AS'-alias of the column.
Invalid code
select 1 into _n somegarbage;
is treated as
select 1 somegarbage into _n;
which is valid.

Regards,
Pavel

#7Pavel Kulakov
paul.kulakov@systematica.ru
In reply to: PG Bug reporting form (#1)
RE: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

This also permits the following syntax in PL/pgSQL:

do $sql$
declare _s text;
begin
_s = '1' somegarbage;
end;
$sql$;

I understand why it works. But it looks really strange.

#8Pavel Stehule
pavel.stehule@gmail.com
In reply to: Pavel Kulakov (#7)
Re: BUG #18195: PL/pgSQL: invalid syntax allowed in SELECT INTO statement

st 15. 11. 2023 v 8:47 odesílatel Pavel Kulakov <paul.kulakov@systematica.ru>
napsal:

This also permits the following syntax in PL/pgSQL:

do $sql$
declare _s text;
begin
_s = '1' somegarbage;
end;
$sql$;

I understand why it works. But it looks really strange.

yes, it is strange, but PL/pgSQL is a translator from PL/pgSQL to SQL, and
'1 xxx' is the correct expression in SQL. Probably today, where PL/pgSQL
assign statements are supported in SQL parser the better design can be
implemented. But probably it should still be hard for SELECT INTO.