On 04/15/2012 03:49 AM, Brendan Jurd wrote:
> 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:
>
...
>
> 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.
This doesn't need fixing, IMNSHO. The name isn't "clobbered" and the
partameter is accessible.
The docs state:
Any statement in the statement section of a block can be a subblock. Subblocks can be used for logical grouping or
tolocalize variables to a small group of statements. Variables declared in a subblock mask any similarly-named
variablesof outer blocks for the duration of the subblock; but you can access the outer variables anyway if you
qualifytheir names with their block's label. ... There is actually a hidden "outer block" surrounding the body of
any PL/pgSQL function. This block provides the declarations of the function's parameters (if any), as well as some
specialvariables such as FOUND (see Section 39.5.5). The outer block is labeled with the function's name, meaning
thatparameters and special variables can be qualified with the function's name.
Note that you can label the outermost block of the function body, as the
example in the docs shows.
cheers
andrew