Thread: plpgsql variable named as SQL keyword

plpgsql variable named as SQL keyword

From
Pavel Stehule
Date:
Hi

one user of plpgsql_check reported interesting error message

create or replace function omega.foo(a int)
returns int as $$
declare offset integer := 0;
begin 
  return offset + 1;
end;
$$ language plpgsql;

postgres=# select omega.foo(10);
ERROR:  query "SELECT offset + 1" returned 0 columns
CONTEXT:  PL/pgSQL function omega.foo(integer) line 4 at RETURN

Maybe we should to disallow variables named as sql reserved keyword.

Regards

Pavel

Re: plpgsql variable named as SQL keyword

From
Tom Lane
Date:
Pavel Stehule <pavel.stehule@gmail.com> writes:
> Maybe we should to disallow variables named as sql reserved keyword.

That would just break existing code.  There are lots of other
examples where you can get away with such things.

We've expended quite a lot of sweat to avoid reserving more names than
we had to in plpgsql.  I'm disinclined to throw that away just because
somebody found an error message confusing.  It's not like reserving
"offset" would cause this case to work.

            regards, tom lane


Re: plpgsql variable named as SQL keyword

From
Pavel Stehule
Date:


čt 28. 2. 2019 v 19:20 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:
Pavel Stehule <pavel.stehule@gmail.com> writes:
> Maybe we should to disallow variables named as sql reserved keyword.

That would just break existing code.  There are lots of other
examples where you can get away with such things.

We've expended quite a lot of sweat to avoid reserving more names than
we had to in plpgsql.  I'm disinclined to throw that away just because
somebody found an error message confusing.  It's not like reserving
"offset" would cause this case to work.

partially I solved it with new warning in plpgsql_check


postgres=# select * from plpgsql_check_function('omega.foo(int, int, int)');
+-------------------------------------------------------------------------------+
|                            plpgsql_check_function                             |
+-------------------------------------------------------------------------------+
| warning:00000:3:statement block:name of variable "offset" is reserved keyword |
| Detail: The reserved keyword was used as variable name.                       |
| error:42601:4:RETURN:query "SELECT offset + 1" returned 0 columns             |
+-------------------------------------------------------------------------------+
(3 rows)


I understand so it has not simple solution (or had not solution). I reported it +/- for record.

Thank you for reply

Pavel


                        regards, tom lane