plpgsql variable named as SQL keyword

Started by Pavel Stehuleabout 7 years ago3 messageshackers
Jump to latest
#1Pavel Stehule
pavel.stehule@gmail.com

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

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Pavel Stehule (#1)
Re: plpgsql variable named as SQL keyword

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

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#2)
Re: plpgsql variable named as SQL keyword

č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

https://github.com/okbob/plpgsql_check/commit/5b9ef57d570c1d11fb92b9cff76655a03767f662

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

Show quoted text

regards, tom lane