BUG #6658: Database do not push down static condition to subquery. - Mailing list pgsql-bugs

From maxim.boguk@gmail.com
Subject BUG #6658: Database do not push down static condition to subquery.
Date
Msg-id E1SWevT-0007ca-3t@wrigleys.postgresql.org
Whole thread Raw
List pgsql-bugs
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:=20=20=20=20=20=20=20=20

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';

=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
                                                           QUERY PLAN=20=20=
=20=20=20=20=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------
 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';
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
                                                             QUERY PLAN=20=
=20=20=20=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
--------------------------------------------------------------------------
 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
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
                                 Index "ix_search_full_address5"
     Column     | Type |=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
                                                   Definition=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=
=20
            | 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

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #6657: Documentation bug for PQconnectdbParams
Next
From: Craig Ringer
Date:
Subject: Re: pltclu install problems