Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPi1t-000129-0D for pgsql-bugs@arkaria.postgresql.org; Tue, 18 Mar 2014 00:37:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WPi1s-0001y2-CE for pgsql-bugs@arkaria.postgresql.org; Tue, 18 Mar 2014 00:37:16 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPi1p-0001xt-TG for pgsql-bugs@postgresql.org; Tue, 18 Mar 2014 00:37:14 +0000 Received: from mail-ee0-x22e.google.com ([2a00:1450:4013:c00::22e]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WPi1f-0004YF-MU for pgsql-bugs@postgresql.org; Tue, 18 Mar 2014 00:37:13 +0000 Received: by mail-ee0-f46.google.com with SMTP id t10so4743724eei.33 for ; Mon, 17 Mar 2014 17:37:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:reply-to:user-agent:mime-version:to :subject:content-type:content-transfer-encoding; bh=EiWO7Kem1iSCJiA1odh0MEIbuodVjuU3Z3I3kgWtjN8=; b=BzeAkfxbQrU6UL/5K0gZa6TxEPwY0ow9PwVaxK6fe+AL0tbVeYuXOfNBxqJzde9Sip +n1XHjb0zNh6GaNO8LNVLJdLaLXJMBTPKmzoBpfcN/GUNAHA0uSTJZS2lZtFFoJPTt0K bSPgpRhgZrow2cAyF935vZo+i5ZHFyKGS4berqCB+qPEAtVwvhWh+oWrtA1Odsi+fdQf un7622RK920xq9bJXPlCuRbKy92p5h30yawjAVu+hOZ+fVaFPhIC/QITGINe5AVrgZh8 Krv6pGVStZ5GR2vr8x6isBlGHbsm47v4EmOadth0+mUmeDMB89gCSMTxGlIT0xBObPJb MtlQ== X-Received: by 10.14.206.137 with SMTP id l9mr26861816eeo.40.1395103021659; Mon, 17 Mar 2014 17:37:01 -0700 (PDT) Received: from [192.168.1.108] (BC242194.catv.pool.telekom.hu. [188.36.33.148]) by mx.google.com with ESMTPSA id j41sm43158253eeg.10.2014.03.17.17.37.00 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 17 Mar 2014 17:37:00 -0700 (PDT) Message-ID: <53279529.2070902@freemail.hu> Date: Tue, 18 Mar 2014 01:36:57 +0100 From: =?ISO-8859-2?Q?K=F3sz=F3_J=F3zsef?= Reply-To: k.joe@freemail.hu User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 MIME-Version: 1.0 To: pgsql-bugs@postgresql.org Subject: pg 9.3 exists subselect with limit brakes query plan Content-Type: text/plain; charset=ISO-8859-2; format=flowed Content-Transfer-Encoding: 8bit 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 Hello, We plan to upgrade from PostgreSQL 8.3 to 9.3 and experience some planner-related problems in our applications with subselects using EXISTS and LIMIT keywords. We have two similar environment with these postgres versions, in 8.3 does not matter, if the subselect using LIMIT or not, but in 9.3 the LIMIT keyword causes odd behavior, enforces seq scans. In 8.3 always get similar plans to both case, but in 9.3 the LIMIT keyword slows down the query. Additionally without index to subselect id field the plan and running time worse with one magnitude in 9.3 than 8.3. The two test case: SELECT ... WHERE EXISTS (SELECT ...); SELECT ... WHERE EXISTS (SELECT ... LIMIT 1); We try to make some application-independent, reproducible tests to show the difference between versions: ===== Initializations ===== # createdb limit1test -U postgres # pgbench -i -s 10 limit1test -U postgres limit1test=# select count(*) from pgbench_branches; count ------- 10 limit1test=# select count(*) from pgbench_accounts; count --------- 1000000 limit1test=# select B.bid, count(*) from pgbench_branches B join pgbench_accounts using (bid) group by 1 order by 1; bid | count -----+-------- 1 | 100000 2 | 100000 3 | 100000 4 | 100000 5 | 100000 6 | 100000 7 | 100000 8 | 100000 9 | 100000 10 | 100000 ===== PostgreSQL 9.3 ===== # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid); --------------------------------------------------------------------- Nested Loop Semi Join (cost=0.00..26402.35 rows=10 width=4) (actual time=0.012..4972.868 rows=10 loops=1) Join Filter: (b.bid = a.bid) Rows Removed by Join Filter: 4500000 -> Seq Scan on pgbench_branches b (cost=0.00..1.10 rows=10 width=4) (actual time=0.003..0.013 rows=10 loops=1) -> Seq Scan on pgbench_accounts a (cost=0.00..26394.00 rows=1000000 width=4) (actual time=0.002..242.321 rows=450001 loops=10) Total runtime: 4972.904 ms limit1test=# explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid limit 1); --------------------------------------------------------------------- Seq Scan on pgbench_branches b (cost=0.00..3.99 rows=5 width=4) (actual time=0.018..382.774 rows=10 loops=1) Filter: (SubPlan 1) SubPlan 1 -> Limit (cost=0.00..0.29 rows=1 width=4) (actual time=38.269..38.269 rows=1 loops=10) -> Seq Scan on pgbench_accounts a (cost=0.00..28894.00 rows=100000 width=4) (actual time=38.261..38.261 rows=1 loops=10) Filter: (bid = b.bid) Rows Removed by Filter: 450000 Total runtime: 382.816 ms # create index pgbench_accounts_bid on pgbench_accounts(bid); # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid); -------------------------------------------------------------------- Nested Loop Semi Join (cost=0.42..2739.24 rows=10 width=4) (actual time=0.030..0.131 rows=10 loops=1) -> Seq Scan on pgbench_branches b (cost=0.00..1.10 rows=10 width=4) (actual time=0.003..0.011 rows=10 loops=1) -> Index Only Scan using pgbench_accounts_bid on pgbench_accounts a (cost=0.42..2733.62 rows=100000 width=4) (actual time=0.010..0.010 rows=1 loops=10) Index Cond: (bid = b.bid) Heap Fetches: 0 Total runtime: 0.169 ms # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid limit 1); -------------------------------------------------------------------- Seq Scan on pgbench_branches b (cost=0.00..3.99 rows=5 width=4) (actual time=0.016..384.974 rows=10 loops=1) Filter: (SubPlan 1) SubPlan 1 -> Limit (cost=0.00..0.29 rows=1 width=4) (actual time=38.490..38.490 rows=1 loops=10) -> Seq Scan on pgbench_accounts a (cost=0.00..28894.00 rows=100000 width=4) (actual time=38.482..38.482 rows=1 loops=10) Filter: (bid = b.bid) Rows Removed by Filter: 450000 Total runtime: 385.015 ms # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid*100); -------------------------------------------------------------------- Nested Loop Semi Join (cost=0.43..2989.31 rows=10 width=4) (actual time=0.055..0.055 rows=0 loops=1) -> Seq Scan on pgbench_branches b (cost=0.00..1.10 rows=10 width=4) (actual time=0.004..0.009 rows=10 loops=1) -> Index Only Scan using pgbench_accounts_bid on pgbench_accounts a (cost=0.43..2983.63 rows=100000 width=4) (actual time=0.002..0.002 rows=0 loops=10) Index Cond: (bid = (b.bid * 100)) Heap Fetches: 0 Total runtime: 0.087 ms # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid*100 limit 1); ------------------------------------------------------------------- Seq Scan on pgbench_branches b (cost=0.00..4.24 rows=5 width=4) (actual time=1019.139..1019.139 rows=0 loops=1) Filter: (SubPlan 1) Rows Removed by Filter: 10 SubPlan 1 -> Limit (cost=0.00..0.31 rows=1 width=4) (actual time=101.907..101.907 rows=0 loops=10) -> Seq Scan on pgbench_accounts a (cost=0.00..31394.00 rows=100000 width=4) (actual time=101.898..101.898 rows=0 loops=10) Filter: (bid = (b.bid * 100)) Rows Removed by Filter: 1000000 Total runtime: 1019.166 ms # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid offset 1); ------------------------------------------------------------------ Seq Scan on pgbench_branches b (cost=0.00..6.06 rows=5 width=4) (actual time=0.042..0.166 rows=10 loops=1) Filter: (SubPlan 1) SubPlan 1 -> Limit (cost=0.46..3542.39 rows=99997 width=4) (actual time=0.014..0.014 rows=1 loops=10) -> Index Only Scan using pgbench_accounts_bid on pgbench_accounts a (cost=0.42..3542.39 rows=99998 width=4) (actual time=0.011..0.012 rows=2 loops=10) Index Cond: (bid = b.bid) Heap Fetches: 0 Total runtime: 0.200 ms ===== PostgreSQL 8.3 ===== # explain analyze select bid from branches B where exists (select bid from accounts A where A.bid=B.bid); --------------------------------------------------------------------- Seq Scan on branches b (cost=0.00..3.99 rows=5 width=4) (actual time=0.020..436.306 rows=10 loops=1) Filter: (subplan) SubPlan -> Seq Scan on accounts a (cost=0.00..28894.00 rows=100000 width=4) (actual time=43.618..43.618 rows=1 loops=10) Filter: (bid = $0) Total runtime: 436.348 ms # explain analyze select bid from branches B where exists (select bid from accounts A where A.bid=B.bid limit 1); ---------------------------------------------------------------------- Seq Scan on branches b (cost=0.00..3.99 rows=5 width=4) (actual time=0.019..442.392 rows=10 loops=1) Filter: (subplan) SubPlan -> Limit (cost=0.00..0.29 rows=1 width=4) (actual time=44.231..44.231 rows=1 loops=10) -> Seq Scan on accounts a (cost=0.00..28894.00 rows=100000 width=4) (actual time=44.223..44.223 rows=1 loops=10) Filter: (bid = $0) Total runtime: 442.440 ms # create index accounts_bid on accounts(bid); # explain analyze select bid from branches B where exists (select bid from accounts A where A.bid=B.bid); ---------------------------------------------------------------------- Seq Scan on branches b (cost=0.00..1.55 rows=5 width=4) (actual time=0.049..0.211 rows=10 loops=1) Filter: (subplan) SubPlan -> Index Scan using accounts_bid on accounts a (cost=0.00..4493.36 rows=100000 width=4) (actual time=0.017..0.017 rows=1 loops=10) Index Cond: (bid = $0) Total runtime: 0.258 ms # explain analyze select bid from branches B where exists (select bid from accounts A where A.bid=B.bid limit 1); --------------------------------------------------------------------- Seq Scan on branches b (cost=0.00..1.55 rows=5 width=4) (actual time=0.059..0.180 rows=10 loops=1) Filter: (subplan) SubPlan -> Limit (cost=0.00..0.04 rows=1 width=4) (actual time=0.015..0.015 rows=1 loops=10) -> Index Scan using accounts_bid on accounts a (cost=0.00..4493.36 rows=100000 width=4) (actual time=0.014..0.014 rows=1 loops=10) Index Cond: (bid = $0) Total runtime: 0.211 ms # explain analyze select bid from branches B where exists (select bid from accounts A where A.bid=B.bid*100); --------------------------------------------------------------------- Seq Scan on branches b (cost=0.00..1.57 rows=5 width=4) (actual time=0.050..0.050 rows=0 loops=1) Filter: (subplan) SubPlan -> Index Scan using accounts_bid on accounts a (cost=0.00..4493.36 rows=100000 width=4) (actual time=0.003..0.003 rows=0 loops=10) Index Cond: (bid = ($0 * 100)) Total runtime: 0.082 ms # explain analyze select bid from branches B where exists (select bid from accounts A where A.bid=B.bid*100 limit 1); --------------------------------------------------------------------- Seq Scan on branches b (cost=0.00..1.57 rows=5 width=4) (actual time=0.066..0.066 rows=0 loops=1) Filter: (subplan) SubPlan -> Limit (cost=0.00..0.05 rows=1 width=4) (actual time=0.005..0.005 rows=0 loops=10) -> Index Scan using accounts_bid on accounts a (cost=0.00..4493.36 rows=100000 width=4) (actual time=0.003..0.003 rows=0 loops=10) Index Cond: (bid = ($0 * 100)) Total runtime: 0.095 ms # explain analyze select bid from branches B where exists (select bid from accounts A where A.bid=B.bid offset 1); ---------------------------------------------------------------------- Seq Scan on branches b (cost=0.00..2.00 rows=5 width=4) (actual time=0.038..0.181 rows=10 loops=1) Filter: (subplan) SubPlan -> Limit (cost=0.04..4493.36 rows=99999 width=4) (actual time=0.016..0.016 rows=1 loops=10) -> Index Scan using accounts_bid on accounts a (cost=0.00..4493.36 rows=100000 width=4) (actual time=0.013..0.014 rows=2 loops=10) Index Cond: (bid = $0) Total runtime: 0.213 ms ===== Postgres settings ===== PostgreSQL 9.3: version ---------------------------------------------------------------------- PostgreSQL 9.3.3 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.7.2-5) 4.7.2, 64-bit work_mem ---------- 128MB shared_buffers ---------------- 1536MB effective_cache_size ---------------------- 3GB default_statistics_target --------------------------- 100 PostgreSQL 8.3: version ----------------------------------------------------------------------- PostgreSQL 8.3.14 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.3.real (Debian 4.3.2-1.1) 4.3.2 work_mem ---------- 128MB shared_buffers ---------------- 1GB effective_cache_size ---------------------- 2GB default_statistics_target --------------------------- 100 *_cost settings are all the same (default values), and vacuum, analyze or reindex does not make any difference. Any suggestions, what is causes this difference, and how to resolve it globally? Or are these examples are too special/simple, and seqscan plan wins in 9.3? # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid limit 1); ------------------------------------------------------------------- Seq Scan on pgbench_branches b (cost=0.00..3.99 rows=5 width=4) (actual time=0.028..379.426 rows=10 loops=1) Filter: (SubPlan 1) SubPlan 1 -> Limit (cost=0.00..0.29 rows=1 width=4) (actual time=37.934..37.934 rows=1 loops=10) -> Seq Scan on pgbench_accounts a (cost=0.00..28894.00 rows=100000 width=4) (actual time=37.927..37.927 rows=1 loops=10) Filter: (bid = b.bid) Rows Removed by Filter: 450000 Total runtime: 379.464 ms # set enable_seqscan=off; # explain analyze select bid from pgbench_branches B where exists (select bid from pgbench_accounts A where A.bid=B.bid limit 1); ------------------------------------------------------------------- Index Only Scan using pgbench_branches_pkey on pgbench_branches b (cost=0.14..12.82 rows=5 width=4) (actual time=0.039..0.145 rows=10 loops=1) Filter: (SubPlan 1) Heap Fetches: 0 SubPlan 1 -> Limit (cost=0.42..0.45 rows=1 width=4) (actual time=0.012..0.012 rows=1 loops=10) -> Index Only Scan using pgbench_accounts_bid on pgbench_accounts a (cost=0.42..2850.43 rows=100000 width=4) (actual time=0.011..0.011 rows=1 loops=10) Index Cond: (bid = b.bid) Heap Fetches: 0 Total runtime: 0.178 ms Thanks, József Kószó -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs