Re: deleting function

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: deleting function
Date: 2009-04-26 10:39:52
Message-ID: 20090426103952.GN12225@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Apr 24, 2009 at 08:19:37PM -0700, pavunkumar wrote:
> On Apr 24, 8:55 pm, t(dot)(dot)(dot)(at)sss(dot)pgh(dot)pa(dot)us (Tom Lane) wrote:
> > Don't use the same name for a parameter as you use for a table column
> > referenced in the function. The above is equivalent to
> > "delete... where $1=$1", ie, delete everything.
>
> Whatever your saying right , But why the function not saying error....
> ? that is my doubt... this is what I want to clarify....!

This is the same as most imperative programming languages; for example
in C:

{
int id = 1;
{
int id = 5;
printf ("id = %i\n", id);
}
printf ("id = %i\n", id);
}

Will print out 5 followed 1. In C it was considered useful to allow
inner blocks of code to have variables of the same name as outer blocks
and the actual name resolution would proceed from the inner-most block
outwards until a matching definition is found. This convention was
invented before C and is used in most languages I'm aware of.

PL/pgSQL follows a similar convention; unqualified names are looked
up first against the function's parameters and then defaults back to
looking for column names. It could be argued that it would be nice if
you got an error in the case of ambiguity like above, but PG doesn't
currently do that.

If you'd written your delete command as:

DELETE FROM testing WHERE testing.id = id;

or better:

DELETE FROM testing t WHERE t.id = id;

then it would do what you're expecting.

Hope that helps

--
Sam http://samason.me.uk/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message chung 2009-04-26 23:08:05 Open source and diagramming survey
Previous Message Scott Marlowe 2009-04-26 03:42:02 Re: PostgreSQL vacuumdb question