Re: deleting function - Mailing list pgsql-general

From Sam Mason
Subject Re: deleting function
Date
Msg-id 20090426103952.GN12225@frubble.xen.chris-lamb.co.uk
Whole thread Raw
In response to Re: deleting function  (pavunkumar <pavun.bks@gmail.com>)
List pgsql-general
On Fri, Apr 24, 2009 at 08:19:37PM -0700, pavunkumar wrote:
> On Apr 24, 8:55 pm, t...@sss.pgh.pa.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/

pgsql-general by date:

Previous
From: Scott Marlowe
Date:
Subject: Re: PostgreSQL vacuumdb question
Next
From: chung@engr.orst.edu
Date:
Subject: Open source and diagramming survey