Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id D0CFA11CFAA8 for ; Sat, 17 Dec 2011 22:58:31 -0400 (AST) Received: from mail-iy0-f174.google.com ([209.85.210.174]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Rc6xK-0008VO-Fw for pgsql-sql@postgresql.org; Sun, 18 Dec 2011 02:58:31 +0000 Received: by iaeh11 with SMTP id h11so6566188iae.19 for ; Sat, 17 Dec 2011 18:58:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=JgnORuh/UM2MhUZ086+rEOR7wcXINDZSG7OuKIC4n7Q=; b=ogDO31GkfkV9DZ0wkYByQg5dpjoy7uyRQOyeEcg4lAbCrYvnBT+OLsOl0no+HqFcGO x3p2bQ2Cz9QMFr+GatHLweWmoA57clb1mM79mXsiwjItw/S6n+k5yzlG6j8RE5UqYlpv b1Odu68QoCn+Y8ghJ6rw/6+perWtVuB5w+N2U= Received: by 10.50.190.202 with SMTP id gs10mr19160799igc.63.1324177097830; Sat, 17 Dec 2011 18:58:17 -0800 (PST) Received: from [10.9.73.101] (173-164-88-9-Oregon.hfc.comcastbusiness.net. [173.164.88.9]) by mx.google.com with ESMTPS id cv10sm6760510igc.0.2011.12.17.18.58.16 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 17 Dec 2011 18:58:17 -0800 (PST) Subject: Re: Column "..." does not exist (view + union) Mime-Version: 1.0 (Apple Message framework v1251.1) Content-Type: text/plain; charset=us-ascii From: Brent Dombrowski In-Reply-To: <4EED15EA.6000507@gmail.com> Date: Sat, 17 Dec 2011 18:58:18 -0800 Cc: pgsql-sql@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <7E407B4A-8F66-45CD-804D-E97A56C808DA@gmail.com> References: <4EEC0533.9050007@gmail.com> <20111217090246.GA12209@tux> <4EECA699.2020700@gmail.com> <4EED15EA.6000507@gmail.com> To: Stefan Weiss X-Mailer: Apple Mail (2.1251.1) X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201112/76 X-Sequence-Number: 36137 On Dec 17, 2011, at 2:21 PM, Stefan Weiss wrote: > I know, but the problem only occurs when I want to sort by a column > which hasn't been selected, and thus cannot be referred to by its = index. > For normal (non-union) queries, this is possible: >=20 > SELECT relname > FROM pg_class > WHERE relhasindex > ORDER BY relpages; >=20 > In this trivial case, PostgreSQL knows where to look for "relpages". > Not so in a union: >=20 > SELECT relname > FROM pg_class > WHERE relhasindex > UNION > SELECT relname > FROM pg_class > WHERE relhasoids > ORDER BY relpages; >=20 > (ERROR: column "relpages" does not exist) >=20 > I understand the error now (I think), and I know how to avoid it. The real problem here is the order of operations. This is what Postgres = did: SELECT * FROM ( SELECT relname FROM pg_class WHERE relhasindex UNION SELECT relname FROM pg_class WHERE relhasoids ) as foo ORDER BY relpages; It applied the union before the order by. After the union, the relpages = column was projected away. Thus relpages does not exist for the order = by. The postgres manual states that this is what will happen. The UNION clause has this general form: select_statement UNION [ ALL ] select_statement select_statement is any SELECT statement without an ORDER BY, LIMIT, FOR = UPDATE, or FOR SHARE clause. (ORDER BY and LIMIT can be attached to a = subexpression if it is enclosed in parentheses. Without parentheses, = these clauses will be taken to apply to the result of the UNION, not to = its right-hand input expression.)=