Re: Clobbered parameter names via DECLARE in PL/PgSQL - Mailing list pgsql-hackers

From Brendan Jurd
Subject Re: Clobbered parameter names via DECLARE in PL/PgSQL
Date
Msg-id CADxJZo2J22+g1PXTxYnqj+k=nbd__84xsub84wP2=s9z5HMB4w@mail.gmail.com
Whole thread Raw
In response to Re: Clobbered parameter names via DECLARE in PL/PgSQL  (Pavel Stehule <pavel.stehule@gmail.com>)
Responses Re: Clobbered parameter names via DECLARE in PL/PgSQL
List pgsql-hackers
On 15 April 2012 17:55, Pavel Stehule <pavel.stehule@gmail.com> wrote:
> 2012/4/15 Brendan Jurd <direvus@gmail.com>:
>> 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:
>>
...
>>
>> 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.
>

They may technically be in different namespaces, but the fact that the
declared variable quietly goes ahead and masks the parameter locally,
seems like a recipe for unexpected consequences.  It certainly was in
my case, and I doubt I'm the first or the last to make that mistake.

Under these conditions, you now have "foo" which refers to the
variable, and "declare_clobber.foo" which refers to the parameter.
Not exactly a model of clarity, and it's also quite easy to miss the
part of the PL/PgSQL docs mentioning this notation.

Perhaps it's a failure of imagination on my part, but I can't think of
a legitimate reason for a programmer to deliberately use the same name
to refer to a declared variable and a function parameter.  What would
be the benefit?

Cheers,
BJ


pgsql-hackers by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: Clobbered parameter names via DECLARE in PL/PgSQL
Next
From: Pavel Stehule
Date:
Subject: Re: Clobbered parameter names via DECLARE in PL/PgSQL