Re: BUG #1956: Plpgsql top-level DECLARE does not share scope

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: "Karl O(dot) Pinc" <kop(at)meme(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #1956: Plpgsql top-level DECLARE does not share scope
Date: 2005-10-13 03:32:20
Message-ID: 200510130332.j9D3WKd10020@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs


Interesting. If I define this in C:

int x(int y)
{
char *y;
}

I get a warning:

x.c: In function `x':
x.c:3: warning: declaration of `y' shadows a parameter

but no error. We tend to follow the C conventions, so perhaps we should
throw a warning, but I can't think of any cases where we throw a warning
in plpgsql because we compile it once on first call.

I am thinking this falls in the "don't do that" category.

---------------------------------------------------------------------------

Karl O. Pinc wrote:
>
> The following bug has been logged online:
>
> Bug reference: 1956
> Logged by: Karl O. Pinc
> Email address: kop(at)meme(dot)com
> PostgreSQL version: 8.0.3
> Operating system: Linux
> Description: Plpgsql top-level DECLARE does not share scope with
> CREATE FUNCTION
> Details:
>
> Depending on how you want to look at it, this is not really a bug. It does
> produce odd results though.
>
> The ALIASes for function parameters that get created as part of the plpgsql
> CREATE FUNCTION do not share scope (namespace?) with the top-level DECLARE
> of the function. As a result, you inadvertently make your function
> arguments 'disappear' by "re-declaring" them in the top-level DECLARE. I
> haven't put any deep thought into this, but offhand this 'feature' seems to
> have no utility but does have the ability to cause problems. Hence, this
> report.
>
> It would be nice if somebody who has deep thoughts on this would think
> them.
>
> I would expect the below to produce an error when creating foo(int). The
> error would complain about trying to declare the same thing with two
> different types. Instead we see the result below.
>
> create function foo (arg int)
> returns int
> language plpgsql
> as $$
> declare
> arg text;
> begin
> return bar(arg);
> end;
> $$;
>
> create function bar (arg int)
> returns int
> language plpgsql
> as $$
> begin
> return arg + 1;
> end;
> $$;
>
> babase=# select foo(1);
> ERROR: function bar(text) does not exist
> HINT: No function matches the given name and argument types. You may need
> to add explicit type casts.
> CONTEXT: SQL statement "SELECT bar( $1 )"
> PL/pgSQL function "foo" line 4 at return
>
> Regards,
> Karl O. Pinc
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Jan Wieck 2005-10-13 03:49:05 Re: BUG #1953: trigger action on delete
Previous Message Tom Lane 2005-10-13 03:20:20 Re: BUG #1953: trigger action on delete