Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y1j15-00009J-UP for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Dec 2014 21:53:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y1j15-0002no-Dw for pgsql-bugs@arkaria.postgresql.org; Thu, 18 Dec 2014 21:53:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1j14-0002m2-A7 for pgsql-bugs@postgresql.org; Thu, 18 Dec 2014 21:53:50 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1j11-0003Eu-7P for pgsql-bugs@postgresql.org; Thu, 18 Dec 2014 21:53:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id sBILri3d005331; Thu, 18 Dec 2014 16:53:44 -0500 From: Tom Lane 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 In-reply-to: <20141218213959.1903.81488@wrigleys.postgresql.org> References: <20141218213959.1903.81488@wrigleys.postgresql.org> Comments: In-reply-to collin.peters@gmail.com message dated "Thu, 18 Dec 2014 21:39:59 +0000" Date: Thu, 18 Dec 2014 16:53:44 -0500 Message-ID: <5330.1418939624@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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