From: Eric Schwarzenbach <subscriber@blackbrook.org>
To: Scott Dunbar <scott@xigole.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #5898: Nested "in" clauses hide bad column names
Date: Tue, 22 Feb 2011 16:28:39 -0500
Message-ID: <4D642A87.5090405@blackbrook.org> (raw)
In-Reply-To: <4D63FB6B.3060804@xigole.com>
References: <201102221726.p1MHQPDh069580@wwwmaster.postgresql.org>
<3314.1298396703@sss.pgh.pa.us>
<4D63FB6B.3060804@xigole.com>
Using table aliases prevents such problems by disambiguating the column
names. For example, if you had table aliases in just one part of this
query as in the below, you would have gotten an error instead of
deleting all those rows:
select respondent_id from respondent where respondent_id in (select
cs.respondent_id from chat_session cs where cs.project_id in (select project_id from
project where company_id = 4));
Cheers,
Eric
On 2/22/2011 1:07 PM, Scott Dunbar wrote:
> Yes, you're correct. I guess this makes sense but it does seem
> strange that I can enter garbage in a query but it still runs. And in
> my case the output from this (the entire table) was then used in a
> delete statement that toasted the entire table. Allowing bogus SQL
> just seems "wrong" but I do understand what's going on.
>
> Thanks for your help.
>
>
> On 02/22/2011 10:45 AM, Tom Lane wrote:
>> "Scott Dunbar"<scott@xigole.com> writes:
>>> I have a nested in clause like:
>>> select respondent_id from respondent where respondent_id in (select
>>> respondent_id from chat_session where project_id in (select project_id from
>>> project where company_id = 4));
>>> However, in this example, there is no column named respondent_id in the
>>> chat_session table.
>> Probably there is one in respondent, though? This behavior is not a bug
>> --- what you have there is an outer reference, and it is working exactly
>> as specified by the SQL standard. Sub-selects would be a whole lot less
>> useful if they couldn't refer to variables of the outer query.
>>
>> regards, tom lane
>
>
> --
> Scott Dunbar
> Xigole Systems, Inc.
> Enterprise software consulting, development, and hosting
> 303·667·6343
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: subscriber@blackbrook.org, scott@xigole.com
Subject: Re: BUG #5898: Nested "in" clauses hide bad column names
In-Reply-To: <4D642A87.5090405@blackbrook.org>
References: <201102221726.p1MHQPDh069580@wwwmaster.postgresql.org> <3314.1298396703@sss.pgh.pa.us> <4D63FB6B.3060804@xigole.com> <4D642A87.5090405@blackbrook.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox