Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 2A5BE1BC612F for ; Mon, 21 May 2012 23:34:34 -0300 (ADT) Received: from mahout.postgresql.org ([98.129.198.123]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SWevg-0002x7-IH for pgsql-bugs@postgresql.org; Tue, 22 May 2012 02:34:33 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SWevT-00031P-RR for pgsql-bugs@postgresql.org; Tue, 22 May 2012 02:34:20 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.72) (envelope-from ) id 1SWevT-0007ca-3t for pgsql-bugs@postgresql.org; Tue, 22 May 2012 02:34:19 +0000 Date: Tue, 22 May 2012 02:34:19 +0000 Message-Id: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #6658: Database do not push down static condition to subquery. To: pgsql-bugs@postgresql.org From: maxim.boguk@gmail.com X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/134 X-Sequence-Number: 33105 The following bug has been logged on the website: Bug reference: 6658 Logged by: Maxim Boguk Email address: maxim.boguk@gmail.com PostgreSQL version: 9.1.3 Operating system: Linux Description: = Hi, I have 2 queries which have the same plan/same performance on 8.3 but on 9.1.3 fist one doesn't perform well because condition doesn't pushed down to subquery: --slow on 9.1.3 but fast on 8.3.13 --helper just shell view db=3D# explain (analyze,verbose) select id from (SELECT id, regexp_replace(btrim(((((((COALESCE(NULLIF(start_number, 0) || ' '::text, ''::text) || address) || ' '::text) || suburb) || ' '::text) || state) || ' '::text) || postcode), ' +'::text, ' '::text, 'g'::text) AS search_string FROM helper WHERE start_number IS NOT NULL AND start_alpha IS NOT NULL AND end_number IS NULL ) AS _t WHERE search_string=3D'something'; = = = = QUERY PLAN = = = = = = ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= --------------------------------------------------------------------- Subquery Scan on _t (cost=3D7715.15..2141451.98 rows=3D55630 width=3D4) (= actual time=3D4132.959..4132.959 rows=3D0 loops=3D1) Output: _t.id Filter: (_t.search_string =3D 'something'::text) -> Bitmap Heap Scan on data_table (cost=3D7715.15..1557337.03 rows=3D11125999 width=3D45) (actual time=3D92.290..4066.518 rows=3D403344 l= oops=3D1) Output: id, regexp_replace(btrim((((((((COALESCE(((NULLIF(start_number, 0))::text || ' '::text), ''::text) || upper(regexp_replace(btrim(((COALESCE((name || ' '::text), ''::text) || COALESCE((extension || ' '::text), ''::text)) || COALESCE(direction, ''::text))), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(locality_name, ''::text)), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(state_code, ''::text)), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(post_code, ''::text)), ' +'::text, ' '::text, 'g'::text)))), ' +'::text, ' '::text, 'g'::text) Recheck Cond: ((start_number IS NOT NULL) AND (upper((start_alpha)::text) IS NOT NULL) AND (end_number IS NULL)) -> Bitmap Index Scan on ix_search_full5 (cost=3D0.00..4933.65 rows=3D11125999 width=3D0) (actual time=3D59.118..59.118 rows=3D403344 loop= s=3D1) Total runtime: 4133.001 ms (8 rows) --fast on both 9.1.3 and 8.3.13 --flat version (search_string condition manually inlined) db=3D# explain (analyze, verbose) SELECT id FROM helper WHERE start_number IS NOT NULL AND start_alpha IS NOT NULL AND end_number IS NULL AND regexp_replace(btrim(((((((COALESCE(NULLIF(start_number, 0) || ' '::text, ''::text) || address) || ' '::text) || suburb) || ' '::text) || state) || ' '::text) || postcode), ' +'::text, ' '::text, 'g'::text) =3D 'something'; = = = = QUERY PLAN = = = = = = ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= -------------------------------------------------------------------------- Index Scan using ix_search_full5 on data_table (cost=3D0.07..55842.16 rows=3D55630 width=3D4) (actual time=3D0.006..0.006 rows=3D0 loops=3D1) Output: id Index Cond: (regexp_replace(btrim((((((((COALESCE(((NULLIF(start_number, 0))::text || ' '::text), ''::text) || upper(regexp_replace(btrim(((COALESCE((name || ' '::text), ''::text) || COALESCE((extension || ' '::text), ''::text)) || COALESCE(direction, ''::text))), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(locality_name, ''::text)), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(state_code, ''::text)), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(post_code, ''::text)), ' +'::text, ' '::text, 'g'::text)))), ' +'::text, ' '::text, 'g'::text) =3D 'something'::text) Total runtime: 0.027 ms (4 rows) db=3D# \d+ ix_search_full5 = = = = Index "ix_search_full_address5" Column | Type | = = = = Definition = = = = = | Storage | Description ----------------+------+---------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------------------+----------+------------- regexp_replace | text | regexp_replace(btrim(((((((COALESCE(NULLIF(start_number, 0) || ' '::text, ''::text) || upper(regexp_replace(btrim((COALESCE(name || ' '::text, ''::text) || COALESCE(extension || ' '::text, ''::text)) || COALESCE(direction, ''::text)), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(locality_name, ''::text)), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(state_code, ''::text)), ' +'::text, ' '::text, 'g'::text))) || ' '::text) || upper(regexp_replace(btrim(COALESCE(post_code, ''::text)), ' +'::text, ' '::text, 'g'::text))), ' +'::text, ' '::text, 'g'::text) | extended | btree, for table "address", predicate (start_number IS NOT NULL AND upper(start_alpha::text) IS NOT NULL AND end_number IS NULL) All *_cost parameters default on both databases, no amout play with *_cost setting on 9.1.3 helps. No changes with other settings on 9.1.3 help. I can't yet create selfcontained test case but I working on in. It's visible that planner see fast way is really faster/cheaper, however something prevent planner from flatten subquery. Kind Regards, Maksym