X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 490C3D1B18A for ; Mon, 2 Aug 2004 14:11:06 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 17326-07 for ; Mon, 2 Aug 2004 17:11:02 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id DD15CD1B1DF for ; Mon, 2 Aug 2004 14:10:59 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i72H9gKT012127; Mon, 2 Aug 2004 13:09:43 -0400 (EDT) To: Kaloyan Iliev Iliev Cc: pgsql-sql@postgresql.org Subject: Re: Problems with UNION ALL and ORDER BY In-reply-to: <410E6E42.6030206@faith.digsys.bg> References: <410E6376.1010404@faith.digsys.bg> <10984.1091464511@sss.pgh.pa.us> <410E6E42.6030206@faith.digsys.bg> Comments: In-reply-to Kaloyan Iliev Iliev message dated "Mon, 02 Aug 2004 19:39:30 +0300" Date: Mon, 02 Aug 2004 13:09:42 -0400 Message-ID: <12126.1091466582@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests=SEE_FOR_YOURSELF X-Spam-Level: X-Archive-Number: 200408/12 X-Sequence-Number: 18509 Kaloyan Iliev Iliev writes: > As I say in my previous letter I am using 7.2.3. If you wish I can show > you the query and the result to see for yourself, that there is > something wrong. It just don't order the overall result but the separate > results of the both subqueries. Quite honestly, I don't believe it. In 7.2 I get regression=# explain select * from tenk1 a union all select * from tenk1 b order by unique1; NOTICE: QUERY PLAN: Sort (cost=3128.28..3128.28 rows=20000 width=148) -> Append (cost=0.00..760.00 rows=20000 width=148) -> Subquery Scan *SELECT* 1 (cost=0.00..380.00 rows=10000 width=148) -> Seq Scan on tenk1 a (cost=0.00..380.00 rows=10000 width=148) -> Subquery Scan *SELECT* 2 (cost=0.00..380.00 rows=10000 width=148) -> Seq Scan on tenk1 b (cost=0.00..380.00 rows=10000 width=148) EXPLAIN and as you can see there's only one sort step being applied to the union result. Now if EXPLAIN shows you a different sorting structure for your query, then I'd be interested to see the exact query and the EXPLAIN output. But what I think is that you are misinterpreting the sorting result you get. If you are using a non-C locale you may be seeing some pretty weird sorting rules :-( regards, tom lane