Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 48D42A97F16 for ; Mon, 26 Dec 2011 09:51:06 -0400 (AST) Received: from mahout.postgresql.org ([98.129.198.123]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RfAxB-0002Y6-OE for pgsql-bugs@postgresql.org; Mon, 26 Dec 2011 13:51:05 +0000 Received: from [204.145.120.229] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RfAwz-0006qT-GC for pgsql-bugs@postgresql.org; Mon, 26 Dec 2011 13:50:49 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.72) (envelope-from ) id 1RfAwz-0006Us-7B for pgsql-bugs@postgresql.org; Mon, 26 Dec 2011 13:50:49 +0000 Date: Mon, 26 Dec 2011 13:50:49 +0000 Message-Id: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #6359: excessively inlining subquery leads to slow queries To: pgsql-bugs@postgresql.org From: maxim.boguk@gmail.com X-Pg-Spam-Score: -3.2 (---) X-Archive-Number: 201112/223 X-Sequence-Number: 32034 The following bug has been logged on the website: Bug reference: 6359 Logged by: Maksym Boguk Email address: maxim.boguk@gmail.com PostgreSQL version: 9.1.2 Operating system: Ubuntu linux Description: = Sometime Postgres inline subrequest even if it produce slower plan (and that slow plan have higher actual cost than non-inlined plan): test case: drop table if exists t1; drop table if exists t2; create table t1 as select id from generate_series(1,1) as g(id); create table t2 as select id from generate_series(1,1000) as g(id); alter table t1 add primary key (id); alter table t2 add primary key (id); analyze t1; analyze t2; --fast non-inlined plan explain (verbose, analyze) select id, = t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_= id+t2_id+t2_id+t2_id+t2_id FROM ( select t1.id, (select t2.id from t2 where t2.id=3Dt1.id) as t2_id from t1 offset 0 ) as t; = QUERY PLAN ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------------------------------------------------------- Subquery Scan on t (cost=3D0.00..0.65 rows=3D1 width=3D8) (actual time=3D0.066..0.069 rows=3D1 loops=3D1) Output: t.id, ((((((((((((((((t.t2_id + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) + t.t2_id) -> Limit (cost=3D0.00..0.60 rows=3D1 width=3D4) (actual time=3D0.053..= 0.056 rows=3D1 loops=3D1) Output: t1.id, ((SubPlan 1)) -> Seq Scan on public.t1 (cost=3D0.00..0.60 rows=3D1 width=3D4) = (actual time=3D0.052..0.053 rows=3D1 loops=3D1) Output: t1.id, (SubPlan 1) SubPlan 1 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0= .49 rows=3D1 width=3D4) (actual time=3D0.025..0.028 rows=3D1 loops=3D1) Output: t2.id Index Cond: (t2.id =3D t1.id) Total runtime: 0.161 ms (11 rows) --slow inlined plan explain (verbose, analyze) select id, = t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_id+t2_= id+t2_id+t2_id+t2_id+t2_id FROM ( select t1.id, (select t2.id from t2 where t2.id=3Dt1.id) as t2_id from t1 -- offset 0 ) as t; = QUERY PLAN = = = ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= --------------------------------------------------------------------- Seq Scan on public.t1 (cost=3D0.00..8.44 rows=3D1 width=3D4) (actual time=3D0.180..0.181 rows=3D1 loops=3D1) Output: t1.id, (((((((((((((((((SubPlan 1) + (SubPlan 2)) + (SubPlan 3)) + (SubPlan 4)) + (SubPlan 5)) + (SubPlan 6)) + (SubPlan 7)) + (SubPlan 8)) + (SubPlan 9)) + (SubPlan 10)) + (SubPlan 11)) + (SubPlan 12)) + (SubPlan 13)) + (SubPlan 14)) + (SubPlan 15)) + (SubPlan 16)) + (SubPlan 17)) SubPlan 1 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.025..0.028 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 2 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.006..0.007 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 3 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.005..0.006 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 4 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.005 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 5 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.004 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 6 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.004 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 7 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.004 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 8 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.005 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 9 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.005 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 10 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.005 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 11 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.005 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 12 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.005 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 13 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.005..0.006 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 14 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.005..0.006 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 15 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.005..0.006 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 16 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.004..0.005 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) SubPlan 17 -> Index Scan using t2_pkey on public.t2 (cost=3D0.00..0.49 rows=3D1 width=3D4) (actual time=3D0.005..0.006 rows=3D1 loops=3D1) Output: public.t2.id Index Cond: (public.t2.id =3D t1.id) Total runtime: 0.466 ms (71 rows) The inlined plan uses 3x more time and have 10x higher cost. I found that problem in much more longer analytical query where subrequest is slow and complicated.