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

Re: Clobbered parameter names via DECLARE in PL/PgSQL

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
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 10:52:25
Message-ID: 4F8AA869.60805@dunslane.net (view raw or flat)
Thread:
Lists: pgsql-hackers

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 to localize variables
    to a small group of statements. Variables declared in a subblock
    mask any similarly-named variables of outer blocks for the duration
    of the subblock; but you can access the outer variables anyway if
    you qualify their 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 special variables
    such as FOUND (see Section 39.5.5). The outer block is labeled with
    the function's name, meaning that parameters 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


In response to

pgsql-hackers by date

Next:From: Simon RiggsDate: 2012-04-15 12:16:31
Subject: Re: Last gasp
Previous:From: Pavel StehuleDate: 2012-04-15 10:47:12
Subject: documentation bug - missing info about unpackaged control files for extension

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