Re: [HACKERS] SELECT...FOR UPDATE OF class_name - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [HACKERS] SELECT...FOR UPDATE OF class_name
Date
Msg-id 19952.948010166@sss.pgh.pa.us
Whole thread Raw
In response to SELECT...FOR UPDATE OF class_name  (Kristofer Munn <kmunn@munn.com>)
Responses Re: [HACKERS] SELECT...FOR UPDATE OF class_name
List pgsql-hackers
Kristofer Munn <kmunn@munn.com> writes:
> select 1 from tbl2 t2, tbl1 t1 where t1.id1 = t2.id1 and 
>     t2.id1 = 7 for update of tbl2;

> ERROR:  FOR UPDATE: relation tbl2 not found in FROM clause

I believe the error message is correct; you should have written

select 1 from tbl2 t2, tbl1 t1 where t1.id1 = t2.id1 and t2.id1 = 7 for update of t2;

A lot of people do not realize that writing an alias for a table
in FROM means that as far as all the rest of that query is concerned,
that alias *is* the name of the table.  The original table name is
completely masked by the alias.  This must be so, because one of the
main reasons for the alias facility is to resolve ambiguity when you
are doing self-joins.  Consider
select * from person p1, person p2 where p1.spouse = p2.id;

If you wrote instead
select * from person p1, person p2 where p1.spouse = person.id;

which instance of the person table is being referenced?  SQL resolves
this by treating it as an error: there is no table named person
available from that FROM clause.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Kristofer Munn
Date:
Subject: SELECT...FOR UPDATE OF class_name
Next
From: "Ansley, Michael"
Date:
Subject: RE: [HACKERS] pg_dump not in very good shape