Re: [HACKERS] SELECT...FOR UPDATE OF class_name

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kristofer Munn <kmunn(at)munn(dot)com>
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: [HACKERS] SELECT...FOR UPDATE OF class_name
Date: 2000-01-16 08:09:26
Message-ID: 19952.948010166@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Kristofer Munn <kmunn(at)munn(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ansley, Michael 2000-01-16 11:52:37 RE: [HACKERS] pg_dump not in very good shape
Previous Message Kristofer Munn 2000-01-16 07:41:02 SELECT...FOR UPDATE OF class_name