Skip site navigation (1) Skip section navigation (2)

Re: Clobbered parameter names via DECLARE in PL/PgSQL

From: Brendan Jurd <direvus(at)gmail(dot)com>
To: Pavel Stehule <pavel(dot)stehule(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 08:24:27
Message-ID: CADxJZo2J22+g1PXTxYnqj+k=nbd__84xsub84wP2=s9z5HMB4w@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-hackers
On 15 April 2012 17:55, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> 2012/4/15 Brendan Jurd <direvus(at)gmail(dot)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

In response to

Responses

pgsql-hackers by date

Next:From: Pavel StehuleDate: 2012-04-15 08:54:10
Subject: Re: Clobbered parameter names via DECLARE in PL/PgSQL
Previous:From: Pavel StehuleDate: 2012-04-15 07:55:24
Subject: Re: Clobbered parameter names via DECLARE in PL/PgSQL

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group