From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | Brendan Jurd <direvus(at)gmail(dot)com> |
Cc: | pgsql-hackers(at)postgresql(dot)org |
Subject: | Re: Clobbered parameter names via DECLARE in PL/PgSQL |
Date: | 2012-04-15 07:55:24 |
Message-ID: | CAFj8pRAeswyCVdXs=bh=d_zzaHsjhdd_KpuLhCxW-Trhjz2asQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
2012/4/15 Brendan Jurd <direvus(at)gmail(dot)com>:
> Hello hackers,
>
> It turns out that in a PL/PgSQL function, you can DECLARE a variable
> using the same name as one of the function parameters. This has the
> effect of clobbering the parameter, for example:
>
> CREATE OR REPLACE FUNCTION declare_clobber(foo int)
> RETURNS int LANGUAGE plpgsql AS $$
> DECLARE
> foo text;
> BEGIN
> RETURN foo;
> END;
> $$;
>
> SELECT declare_clobber(1);
> ==> NULL
>
> On the other hand, PL/PgSQL does protect against duplicate definitions
> within DECLARE:
>
> CREATE OR REPLACE FUNCTION declare_clobber(foo int)
> RETURNS int LANGUAGE plpgsql AS $$
> DECLARE
> foo int;
> foo text;
> BEGIN
> RETURN foo;
> END;
> $$;
> ==> ERROR: duplicate declaration at or near "foo"
>
> And it also protects against using a DECLAREd name as a parameter alias:
>
> CREATE OR REPLACE FUNCTION declare_clobber(foo int)
> RETURNS int LANGUAGE plpgsql AS $$
> DECLARE
> bar int;
> bar ALIAS FOR $1;
> BEGIN
> RETURN bar;
> END;
> $$;
> ==> ERROR: duplicate declaration at or near "bar"
>
> I would suggest that if the user DECLAREs a variable with the same
> name as a parameter, it is very evidently a programming error, and we
> should raise the same "duplicate declaration" error. I haven't yet
> looked at how difficult this would be to fix, but if there are no
> objections I would like to attempt a patch.
>
I disagree - variables and parameters are in different namespace so
you can exactly identify variable and parameter. More - it is
compatibility break.
If plpgsql_check_function exists, then this check can be implemented
as warning.
Regards
Pavel
> Cheers,
> BJ
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
From | Date | Subject | |
---|---|---|---|
Next Message | Brendan Jurd | 2012-04-15 08:24:27 | Re: Clobbered parameter names via DECLARE in PL/PgSQL |
Previous Message | Brendan Jurd | 2012-04-15 07:49:30 | Clobbered parameter names via DECLARE in PL/PgSQL |