Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WD976-0001Cv-8w for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Feb 2014 08:54:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WD975-0001Db-Hs for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Feb 2014 08:54:43 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WD974-0001DU-B8 for pgsql-bugs@postgresql.org; Tue, 11 Feb 2014 08:54:42 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WD96z-0008J3-DB for pgsql-bugs@postgresql.org; Tue, 11 Feb 2014 08:54:40 +0000 Received: from compute1.internal (compute1.nyi.mail.srv.osa [10.202.2.41]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id 223DB21250 for ; Tue, 11 Feb 2014 03:54:34 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute1.internal (MEProxy); Tue, 11 Feb 2014 03:54:35 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=imap.cc; h= message-id:date:from:mime-version:to:subject:content-type :content-transfer-encoding; s=mesmtp; bh=mcs0bvb98Vvf2y/W0DUooXy C7v0=; b=ZF4aSi/nBU64NOBs1ZjZM6KpGI4GVvOc77UWtS/YG/+clAVOIa9qWNA s1susYq68SISoAqPzZBTR7sKmds+8QoPLc8U8Tg8Zy5oaw+Qt9bQvNdPSf95K9Ij HIf7Dh5Gqg9L4vo4edSi18YbHAlF2XsVldYK4t5Y+KWmqOKYtm2E= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:date:from:mime-version:to :subject:content-type:content-transfer-encoding; s=smtpout; bh=m cs0bvb98Vvf2y/W0DUooXyC7v0=; b=k7PNG/epNC5vzMEzVwREofS5RURzE7Tpw OE7etA9UiLPe46PF+W3R0GgZyCECfZyxgBjpGd1NHKNxWyHy8m3cEToao72R3Ryl OsSppVccQpJd5XDws588v/4Qv9SEujsZoPf4xVV5Yyv5ROL9GnIboWo2E+7mhsqA TeWC3OephE= X-Sasl-enc: nSuJtfPhvi0QlXs90c0nx3+O1ZGPW+YPA09LCBxW3xax 1392108874 Received: from [192.168.1.23] (unknown [195.91.155.97]) by mail.messagingengine.com (Postfix) with ESMTPA id 1D06A6800D8 for ; Tue, 11 Feb 2014 03:54:33 -0500 (EST) Message-ID: <52F9E549.9060900@imap.cc> Date: Tue, 11 Feb 2014 12:54:33 +0400 From: Alexey Bashtanov User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: Nested loop does not preserve order. Why? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -0.8 (/) 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 Hello! Let us have a table T(A, B), an index on T(B, A) and a much smaller table V(B). stat-dev1/ui_dev_16 12:27:07 > create table u as select a, a % 1000 b from generate_series(1, 10000000) a; SELECT 10000000 stat-dev1/ui_dev_16 12:27:28 > create table v as select distinct b from u where b % 100 = 1; SELECT 10 stat-dev1/ui_dev_16 12:27:41 > analyze t; ANALYZE stat-dev1/ui_dev_16 12:27:42 > analyze v; ANALYZE stat-dev1/ui_dev_16 12:27:45 > create index u_i on u(b,a); CREATE INDEX And we are looking for all rows of T that have B present in V and sorted by B, A. The most natural way to obtain them would be to take all records from V ordered by B, and to perform an index-only scan on T for each B. But not for postgresql :( stat-dev1/ui_dev_16 12:28:57 > EXPLAIN ANALYZE select b, a from u where b in (select b from v) order by b, a; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------- Sort (cost=879810.77..892310.77 rows=5000000 width=8) (actual time=4728.831..4756.802 rows=100000 loops=1) Sort Key: u.b, u.a Sort Method: external merge Disk: 1760kB -> Hash Join (cost=1.35..186749.35 rows=5000000 width=8) (actual time=0.121..4568.170 rows=100000 loops=1) Hash Cond: (u.b = v.b) -> Seq Scan on u (cost=0.00..144248.00 rows=10000000 width=8) (actual time=0.027..2294.474 rows=10000000 loops=1) -> Hash (cost=1.23..1.23 rows=10 width=4) (actual time=0.071..0.071 rows=10 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 1kB -> HashAggregate (cost=1.12..1.23 rows=10 width=4) (actual time=0.062..0.063 rows=10 loops=1) -> Seq Scan on v (cost=0.00..1.10 rows=10 width=4) (actual time=0.050..0.052 rows=10 loops=1) Total runtime: 4771.817 ms (11 rows) Replacing IN by EXISTS changes nothing. Putting an ORDER BY clause into IN(...) unsurprisingly does not help. The only way to obtain the desired plan I found is to rewrite the query: stat-dev1/ui_dev_16 12:29:06 > set enable_bitmapscan to off; SET stat-dev1/ui_dev_16 12:29:11 > set enable_seqscan to off; SET stat-dev1/ui_dev_16 12:29:13 > EXPLAIN ANALYZE select b, a from u where b = any((select array_agg(b) from v)::int[]) order by b, a; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------- Index Only Scan using u_i on u (cost=10000000001.58..10000315253.45 rows=99551 width=8) (actual time=0.078..103.239 rows=100000 loops=1) Index Cond: (b = ANY ($0)) Heap Fetches: 100000 InitPlan 1 (returns $0) -> Aggregate (cost=10000000001.13..10000000001.14 rows=1 width=4) (actual time=0.018..0.018 rows=1 loops=1) -> Seq Scan on v (cost=10000000000.00..10000000001.10 rows=10 width=4) (actual time=0.006..0.009 rows=10 loops=1) Total runtime: 119.929 ms (7 rows) The plan I expect for the original query is something like that: Nested Loop Preserving Order (cost=1.85..317895.69 rows=100000 width=8) -> HashAggregate (cost=1.42..1.52 rows=10 width=4) -> Sort (cost=1.27..1.29 rows=10 width=4) Sort Key: v.b -> Seq Scan on v (cost=0.00..1.10 rows=10 width=4) -> Index Only Scan using u_i on u (cost=0.43..31689.42 rows=10000 width=8) Index Cond: (b = v.b) But if we try to hint postgresql we will realize that nested loop does not preserve order (see below). Why? stat-dev1/ui_dev_16 12:42:32 > set enable_bitmapscan to off; SET Time: 0,895 ms stat-dev1/ui_dev_16 12:42:33 > set enable_hashjoin to off; SET Time: 0,762 ms stat-dev1/ui_dev_16 12:42:36 > EXPLAIN select b, a from u where b in (select b from v order by b) order by b, a; QUERY PLAN -------------------------------------------------------------------------------------- Sort (cost=326200.51..326450.51 rows=100000 width=8) Sort Key: u.b, u.a -> Nested Loop (cost=1.85..317895.69 rows=100000 width=8) -> HashAggregate (cost=1.42..1.52 rows=10 width=4) -> Sort (cost=1.27..1.29 rows=10 width=4) Sort Key: v.b -> Seq Scan on v (cost=0.00..1.10 rows=10 width=4) -> Index Only Scan using u_i on u (cost=0.43..31689.42 rows=10000 width=8) Index Cond: (b = v.b) (9 rows) -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs