pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: collin.peters@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #12276: Using old name of a renamed or dropped column in a sub-query does not throw error
Date: Thu, 18 Dec 2014 16:53:44 -0500
Message-ID: <5330.1418939624@sss.pgh.pa.us> (raw)
In-Reply-To: <20141218213959.1903.81488@wrigleys.postgresql.org>
References: <20141218213959.1903.81488@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

collin.peters@gmail.com writes:
> Basically if you rename a column, and then use the original name in a
> subquery, that sub-query does not complain that the column no longer exists,
> and seems to complete ok.

This is not a bug, it's just a SQL-standard outer query reference.
You dropped order_id from the "users" table, but there's still a
column by that name in "order_lines", so what you've got in

> SELECT 	* 
> FROM    order_lines
> WHERE   value = 'bar'
> AND     order_id IN (
> 	-- THIS SHOULD FAIL!! THIS COLUMN NAME DOES NOT EXIST ANYMORE
> 	SELECT  order_id
> 	FROM    users
> 	WHERE   user_id = 1
> );

is effectively ... IN (SELECT order_lines.order_id FROM users ...
so the IN condition will succeed as long as there's at least one users
row satisfying user_id = 1.

This is a widely known SQL gotcha, which unfortunately we can't do
anything about without rejecting useful and standard-compliant queries.
The usual advice for protecting yourself against this type of mistake
is to always table-qualify every column reference in a sub-SELECT.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (5+ messages)  latest in thread

Message-ID: <5330.1418939624@sss.pgh.pa.us>
Permalink:  ../5330.1418939624@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/5330.1418939624@sss.pgh.pa.us

 · 

reply

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: tgl@sss.pgh.pa.us, collin.peters@gmail.com
  Subject: Re: BUG #12276: Using old name of a renamed or dropped column in a sub-query does not throw error
  In-Reply-To: <5330.1418939624@sss.pgh.pa.us>
  References: <20141218213959.1903.81488@wrigleys.postgresql.org> <5330.1418939624@sss.pgh.pa.us>

* 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