Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id EA2A32E2F14 for ; Sat, 22 Dec 2007 18:22:15 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 02910-04-7 for ; Sat, 22 Dec 2007 18:21:58 -0400 (AST) Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.182]) by postgresql.org (Postfix) with ESMTP id 7EE982E3933 for ; Sat, 22 Dec 2007 18:12:50 -0400 (AST) Received: by py-out-1112.google.com with SMTP id u77so2240906pyb.15 for ; Sat, 22 Dec 2007 14:12:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:mime-version:content-transfer-encoding:message-id:content-type:to:from:subject:date:x-mailer; bh=8AtqoRMrCHY2hhg6qn2yDGco0AssEJ1ADK71ClnCmug=; b=qE5PPCqR60JPv4Y68jn9oETunx4K3gRpMjK+2rDtrnHiWW/lECAvi0Na2BfMwqTEZAqqRu/1V+dbBG0HoRjC6vpRSjiihOB2APIwCzc+b7ECDDy0NwVFbiB1RP6QGkIWtOO7MA2Wz00tlZw8ST2JGleymTNKJRjYCMTNkBN3gg4= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:content-transfer-encoding:message-id:content-type:to:from:subject:date:x-mailer; b=lTuBgak9Lsi7r2HkDv4i/oC8BJY73cQv5k3xgt8xVpv3MxvApFaqKodQhQMzpyIvygWtAQnMWE2mjLCoZaz9ZWZwHSlDJkGK+EqQXoUemSAmswAXhyPJJ89T3gwN8bq+BoIr8RhsovYkZmerO90xx7cbZYIxIh0pSqUN09Mm9V8= Received: by 10.64.210.3 with SMTP id i3mr4191933qbg.22.1198361568252; Sat, 22 Dec 2007 14:12:48 -0800 (PST) Received: from ?192.168.213.33? ( [99.231.154.182]) by mx.google.com with ESMTPS id e16sm1551708qbe.10.2007.12.22.14.12.46 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 22 Dec 2007 14:12:47 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v752.2) Content-Transfer-Encoding: 7bit Message-Id: <1B7BB76A-D750-4808-8104-2EC50DDB2FBA@gmail.com> Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed To: pgsql-sql@postgresql.org From: Rick Innis Subject: Misnamed field in subquery does not cause error when field name exists in parent query Date: Sat, 22 Dec 2007 17:12:45 -0500 X-Mailer: Apple Mail (2.752.2) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/116 X-Sequence-Number: 29808 Wondering if this is actually a bug, before I file a bug report on it. Consider this sequence: =# create table public.test1 (id int); CREATE TABLE =# create table public.test2 (it int); CREATE TABLE =# select * from test1 where id in (select id from test2); id ---- (0 rows) Note that the select from test2, which references a non-existent field, doesn't cause an error when that statement is used as a subselect AND the non-existent field does exist in a table referenced in the outer select. This is particularly catastrophic when you replace 'select *' by 'delete', as illustrated below. =# insert into test1 values(1); INSERT 0 1 =# insert into test1 values(2); INSERT 0 1 =# insert into test1 values(3); INSERT 0 1 =# delete from test1 where id in (select id from test2); DELETE 0 =# select * From test1; id ---- 1 2 3 (3 rows) =# insert into test2 values(11); INSERT 0 1 =# insert into test2 values(12); INSERT 0 1 =# insert into test2 values(13); INSERT 0 1 =# delete from test1 where id in (select id from test2); DELETE 3 =# select * From test1; id ---- (0 rows) This has bitten me twice, recently, with consequent (though recoverable) data loss. It may be that SQL is doing exactly as it should, since 'id' is in scope within the subselect, but if that's the case it's a nasty gotcha. Obviously the subselect, when used as a select, does generate an error. =# select id from test2; ERROR: column "id" does not exist LINE 1: select id from test2; ^ Referencing a field that doesn't exist in either also causes an error. =# select * from test1 where id in (select if from test2); ERROR: column "if" does not exist LINE 1: select * from test1 where id in (select if from test2); ^ Insight from wiser SQL heads than mine would be appreciated. --Rick.