pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Strange influence of default_statistics_target
5+ messages / 2 participants
[nested] [flat]

* Strange influence of default_statistics_target
@ 2017-01-18 07:54  Вадим Акбашев <ufaowl@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Вадим Акбашев @ 2017-01-18 07:54 UTC (permalink / raw)
  To: pgsql-bugs

Hello!
I have encountered a problem with querry plan building:
I'd set  default_statistics_target=700, run analyze. Postgres optimize had
chosen plan with hash_join and it took ~1 min for qerry to complete.
Then i set default_statistics_target=500 and the plan was significantly
changed and was using merge_join instead, complition time reduced in
hundreds times, cost reduced drastically.
Now i can't understand why more precise statistics leads to less optimized
plan and what is the right way to use default_statistics_target parameter?
I attach both good and bad querry plans and the querry itself

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

dst500.txt (text/plain, 5.3K) download default_statistics_target=500 (good plan) Merge Join (cost=36209583.45..93584000.61 rows=24889787 width=27) (actual time=0.057..0.057 rows=0 loops=1)" " Merge Cond: (av1.number_value = av_1.card_id)" " Join Filter: (SubPlan 2)" " -> Index Scan using xif8number_value on attribute_value av1 (cost=0.57..81759294.58 rows=548296097 width=23) (actual time=0.017..0.017 rows=1 loops=1)" " -> Sort (cost=12192441.38..12193162.13 rows=288300 width=37) (actual time=0.034..0.034 rows=0 loops=1)" " Sort Key: av_1.card_id" " Sort Method: quicksort Memory: 25kB" " -> Merge Join (cost=2820197.11..12166296.59 rows=288300 width=37) (actual time=0.027..0.027 rows=0 loops=1)" " Merge Cond: ((a_2.attribute_code)::text = (ao_1.attribute_code)::text)" " Join Filter: (SubPlan 1)" " -> Nested Loop (cost=2793472.67..12137623.79 rows=576600 width=65) (actual time=0.026..0.026 rows=0 loops=1)" " -> Merge Join (cost=46.31..46.35 rows=1 width=46) (actual time=0.025..0.025 rows=0 loops=1)" " Merge Cond: ((a_2.attribute_code)::text = (ao1.attribute_code)::text)" " -> Sort (cost=18.32..18.33 rows=1 width=21) (actual time=0.025..0.025 rows=0 loops=1)" " Sort Key: a_2.attribute_code" " Sort Method: quicksort Memory: 25kB" " -> Nested Loop (cost=1.11..18.31 rows=1 width=21) (actual time=0.009..0.009 rows=0 loops=1)" " -> Nested Loop (cost=0.84..13.02 rows=9 width=18) (actual time=0.009..0.009 rows=0 loops=1)" " -> Index Only Scan using card_and_status_idx on card c_2 (cost=0.56..8.58 rows=1 width=11) (actual time=0.008..0.008 rows=0 loops=1)" " Index Cond: (card_id = '0'::numeric)" " Heap Fetches: 0" " -> Index Only Scan using xpktemplate_block on template_block tb_1 (cost=0.28..4.38 rows=6 width=17) (never executed)" " Index Cond: (template_id = c_2.template_id)" " Heap Fetches: 0" " -> Index Scan using xif1attribute on attribute a_2 (cost=0.28..0.58 rows=1 width=27) (never executed)" " Index Cond: ((block_code)::text = (tb_1.block_code)::text)" " Filter: ((data_type = 'B'::bpchar) AND ((attribute_code)::text = ANY ('{JBR_IMPL_ONCONT,_STATE,ADMIN_290575,JBR_IMPL_RESOLUT,JBR_DOCL_RELATDOC,ADMIN_221237,JBR_REGD_REGNUM,JBR_REGD_DATEREG (...)" " -> Sort (cost=27.99..28.00 rows=4 width=25) (never executed)" " Sort Key: ao1.attribute_code" " -> Bitmap Heap Scan on attribute_option ao1 (cost=23.69..27.95 rows=4 width=25) (never executed)" " Recheck Cond: ((option_code)::text = 'UPLINK'::text)" " -> Bitmap Index Scan on xpkattribute_option (cost=0.00..23.69 rows=4 width=0) (never executed)" " Index Cond: ((option_code)::text = 'UPLINK'::text)" " -> Bitmap Heap Scan on attribute_value av_1 (cost=2793426.36..10801306.74 rows=133627070 width=23) (never executed)" " Recheck Cond: (number_value = '0'::numeric)" " -> Bitmap Index Scan on xif8number_value (cost=0.00..2760019.59 rows=133627070 width=0) (never executed)" " Index Cond: (number_value = '0'::numeric)" " -> Sort (cost=29.69..29.82 rows=49 width=25) (never executed)" " Sort Key: ao_1.attribute_code" " -> Bitmap Heap Scan on attribute_option ao_1 (cost=23.70..28.32 rows=49 width=25) (never executed)" " Recheck Cond: ((option_code)::text = 'LINK'::text)" " -> Bitmap Index Scan on xpkattribute_option (cost=0.00..23.69 rows=49 width=0) (never executed)" " Index Cond: ((option_code)::text = 'LINK'::text)"
dst700.txt (text/plain, 5.8K) download default_statistics_target=700 (bad plan) Hash Join (cost=16773308.60..82433071.21 rows=18200167 width=27) (actual time=66330.619..66330.619 rows=0 loops=1)" " Hash Cond: (av1.number_value = av_1.card_id)" " Join Filter: (SubPlan 2)" " -> Bitmap Heap Scan on attribute_value av1 (cost=4568706.91..13354005.00 rows=545981533 width=23) (actual time=66327.191..66327.191 rows=1 loops=1)" " Recheck Cond: (number_value IS NOT NULL)" " Heap Blocks: exact=1" " -> Bitmap Index Scan on xif8number_value (cost=0.00..4432211.53 rows=244924709 width=0) (actual time=61346.173..61346.173 rows=248556906 loops=1)" " -> Hash (cost=12200971.35..12200971.35 rows=290427 width=37) (actual time=0.041..0.041 rows=0 loops=1)" " Buckets: 524288 Batches: 1 Memory Usage: 4096kB" " -> Merge Join (cost=2834039.76..12200971.35 rows=290427 width=37) (actual time=0.041..0.041 rows=0 loops=1)" " Merge Cond: ((a_2.attribute_code)::text = (ao_1.attribute_code)::text)" " Join Filter: (SubPlan 1)" " -> Nested Loop (cost=2807242.54..12172087.09 rows=580855 width=65) (actual time=0.034..0.034 rows=0 loops=1)" " -> Merge Join (cost=46.87..46.91 rows=1 width=46) (actual time=0.033..0.033 rows=0 loops=1)" " Merge Cond: ((a_2.attribute_code)::text = (ao1.attribute_code)::text)" " -> Sort (cost=18.88..18.89 rows=1 width=21) (actual time=0.031..0.031 rows=0 loops=1)" " Sort Key: a_2.attribute_code" " Sort Method: quicksort Memory: 25kB" " -> Nested Loop (cost=1.11..18.87 rows=1 width=21) (actual time=0.018..0.018 rows=0 loops=1)" " -> Nested Loop (cost=0.84..13.02 rows=9 width=18) (actual time=0.017..0.017 rows=0 loops=1)" " -> Index Only Scan using card_and_status_idx on card c_2 (cost=0.56..8.58 rows=1 width=11) (actual time=0.016..0.016 rows=0 loops=1)" " Index Cond: (card_id = '0'::numeric)" " Heap Fetches: 0" " -> Index Only Scan using xpktemplate_block on template_block tb_1 (cost=0.28..4.38 rows=6 width=17) (never executed)" " Index Cond: (template_id = c_2.template_id)" " Heap Fetches: 0" " -> Index Scan using xif1attribute on attribute a_2 (cost=0.28..0.64 rows=1 width=27) (never executed)" " Index Cond: ((block_code)::text = (tb_1.block_code)::text)" " Filter: ((data_type = 'B'::bpchar) AND ((attribute_code)::text = ANY ('{ADMIN_702311,JBR_HOWFAST,_STATE,ADMIN_6849958,_PRELIMINARY_TERM,_ALL_DOCLINKS,JBR_INCOMEDATE,JBR_RASSM_PARENT_ (...)" " -> Sort (cost=27.99..28.00 rows=4 width=25) (never executed)" " Sort Key: ao1.attribute_code" " -> Bitmap Heap Scan on attribute_option ao1 (cost=23.69..27.95 rows=4 width=25) (never executed)" " Recheck Cond: ((option_code)::text = 'UPLINK'::text)" " -> Bitmap Index Scan on xpkattribute_option (cost=0.00..23.69 rows=4 width=0) (never executed)" " Index Cond: ((option_code)::text = 'UPLINK'::text)" " -> Bitmap Heap Scan on attribute_value av_1 (cost=2807195.67..10825909.73 rows=134613045 width=23) (never executed)" " Recheck Cond: (number_value = '0'::numeric)" " -> Bitmap Index Scan on xif8number_value (cost=0.00..2773542.41 rows=134613045 width=0) (never executed)" " Index Cond: (number_value = '0'::numeric)" " -> Sort (cost=29.69..29.82 rows=49 width=25) (never executed)" " Sort Key: ao_1.attribute_code" " -> Bitmap Heap Scan on attribute_option ao_1 (cost=23.70..28.32 rows=49 width=25) (never executed)" " Recheck Cond: ((option_code)::text = 'LINK'::text)" " -> Bitmap Index Scan on xpkattribute_option (cost=0.00..23.69 rows=49 width=0) (never executed)" " Index Cond: ((option_code)::text = 'LINK'::text)"
querry.sql (application/sql, 4.1K) download ^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: Strange influence of default_statistics_target
@ 2017-01-18 14:18  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Вадим Акбашев <ufaowl@gmail.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Tom Lane @ 2017-01-18 14:18 UTC (permalink / raw)
  To: Вадим Акбашев <ufaowl@gmail.com>; +Cc: pgsql-bugs

=?UTF-8?B?0JLQsNC00LjQvCDQkNC60LHQsNGI0LXQsg==?= <ufaowl@gmail.com> writes:
> I have encountered a problem with querry plan building:
> I'd set  default_statistics_target=700, run analyze. Postgres optimize had
> chosen plan with hash_join and it took ~1 min for qerry to complete.
> Then i set default_statistics_target=500 and the plan was significantly
> changed and was using merge_join instead, complition time reduced in
> hundreds times, cost reduced drastically.
> Now i can't understand why more precise statistics leads to less optimized
> plan and what is the right way to use default_statistics_target parameter?
> I attach both good and bad querry plans and the querry itself

Are those really the same query?  Plan 2 is enforcing a "number_value IS
NOT NULL" condition on "attribute_value av1" that I don't see in plan 1.
And neither plan seems to have much to do with the query, since the
query has UNIONs that aren't in the plans.

But the short answer seems to be that in both cases, the only reason that
the plan doesn't take forever to run is that one sub-join chances to yield
precisely zero rows, and the PG executor happens to be more efficient
about that corner case in the one plan shape than the other.  The planner
doesn't take the possibility of that short-circuit happening into account,
since it generally cannot be sure that a sub-join wouldn't yield any rows.
So it's just luck that one plan is noticeably faster in this case.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Strange influence of default_statistics_target
@ 2017-01-19 06:24  Вадим Акбашев <ufaowl@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 5+ messages in thread

From: Вадим Акбашев @ 2017-01-19 06:24 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

Hi, Tom!
Thank you for your answer.
Those plans indeed were for the querry i've sent. But plans where not
complete as i sent only parts of them that,as i tought, contain the
problem. In this letter i attach full plans and also statistics for tables
the querry works with. For default_statistics_target=700
and default_statistics_target=500.
This querry works much faster in our test enviroment which is 75% of our
production server in size.
Also i've noticed that the plan becomes "bad" after 9-10 hours after it was
fixed. And if I run ANALYZE again without changing effective_cache_size
in postgresql.conf it remains "bad".

Thank you in advance
Vadim


2017-01-18 19:18 GMT+05:00 Tom Lane <tgl@sss.pgh.pa.us>:

> =?UTF-8?B?0JLQsNC00LjQvCDQkNC60LHQsNGI0LXQsg==?= <ufaowl@gmail.com>
> writes:
> > I have encountered a problem with querry plan building:
> > I'd set  default_statistics_target=700, run analyze. Postgres optimize
> had
> > chosen plan with hash_join and it took ~1 min for qerry to complete.
> > Then i set default_statistics_target=500 and the plan was significantly
> > changed and was using merge_join instead, complition time reduced in
> > hundreds times, cost reduced drastically.
> > Now i can't understand why more precise statistics leads to less
> optimized
> > plan and what is the right way to use default_statistics_target
> parameter?
> > I attach both good and bad querry plans and the querry itself
>
> Are those really the same query?  Plan 2 is enforcing a "number_value IS
> NOT NULL" condition on "attribute_value av1" that I don't see in plan 1.
> And neither plan seems to have much to do with the query, since the
> query has UNIONs that aren't in the plans.
>
> But the short answer seems to be that in both cases, the only reason that
> the plan doesn't take forever to run is that one sub-join chances to yield
> precisely zero rows, and the PG executor happens to be more efficient
> about that corner case in the one plan shape than the other.  The planner
> doesn't take the possibility of that short-circuit happening into account,
> since it generally cannot be sure that a sub-join wouldn't yield any rows.
> So it's just luck that one plan is noticeably faster in this case.
>
>                         regards, tom lane
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Strange influence of default_statistics_target
@ 2017-01-19 09:11  Вадим Акбашев <ufaowl@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 5+ messages in thread

From: Вадим Акбашев @ 2017-01-19 09:11 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

Sorry, i've forgot to attach files themselves

2017-01-18 19:18 GMT+05:00 Tom Lane <tgl@sss.pgh.pa.us>:

> =?UTF-8?B?0JLQsNC00LjQvCDQkNC60LHQsNGI0LXQsg==?= <ufaowl@gmail.com>
> writes:
> > I have encountered a problem with querry plan building:
> > I'd set  default_statistics_target=700, run analyze. Postgres optimize
> had
> > chosen plan with hash_join and it took ~1 min for qerry to complete.
> > Then i set default_statistics_target=500 and the plan was significantly
> > changed and was using merge_join instead, complition time reduced in
> > hundreds times, cost reduced drastically.
> > Now i can't understand why more precise statistics leads to less
> optimized
> > plan and what is the right way to use default_statistics_target
> parameter?
> > I attach both good and bad querry plans and the querry itself
>
> Are those really the same query?  Plan 2 is enforcing a "number_value IS
> NOT NULL" condition on "attribute_value av1" that I don't see in plan 1.
> And neither plan seems to have much to do with the query, since the
> query has UNIONs that aren't in the plans.
>
> But the short answer seems to be that in both cases, the only reason that
> the plan doesn't take forever to run is that one sub-join chances to yield
> precisely zero rows, and the PG executor happens to be more efficient
> about that corner case in the one plan shape than the other.  The planner
> doesn't take the possibility of that short-circuit happening into account,
> since it generally cannot be sure that a sub-join wouldn't yield any rows.
> So it's just luck that one plan is noticeably faster in this case.
>
>                         regards, tom lane
>

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

  plan_500 (application/octet-stream, 12.3K) download

  plan_700 (application/octet-stream, 21.3K) download

  query (application/octet-stream, 7.2K) download

  stat_500 (application/octet-stream, 10.3K) download

  stat_700 (application/octet-stream, 10.6K) download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Strange influence of default_statistics_target
@ 2017-01-24 07:18  Вадим Акбашев <ufaowl@gmail.com>
  parent: Вадим Акбашев <ufaowl@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Вадим Акбашев @ 2017-01-24 07:18 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

Hello.
Information i've sent, is it sufficient?
Thank you.

2017-01-19 14:11 GMT+05:00 Вадим Акбашев <ufaowl@gmail.com>:

> Sorry, i've forgot to attach files themselves
>
> 2017-01-18 19:18 GMT+05:00 Tom Lane <tgl@sss.pgh.pa.us>:
>
>> =?UTF-8?B?0JLQsNC00LjQvCDQkNC60LHQsNGI0LXQsg==?= <ufaowl@gmail.com>
>> writes:
>> > I have encountered a problem with querry plan building:
>> > I'd set  default_statistics_target=700, run analyze. Postgres optimize
>> had
>> > chosen plan with hash_join and it took ~1 min for qerry to complete.
>> > Then i set default_statistics_target=500 and the plan was significantly
>> > changed and was using merge_join instead, complition time reduced in
>> > hundreds times, cost reduced drastically.
>> > Now i can't understand why more precise statistics leads to less
>> optimized
>> > plan and what is the right way to use default_statistics_target
>> parameter?
>> > I attach both good and bad querry plans and the querry itself
>>
>> Are those really the same query?  Plan 2 is enforcing a "number_value IS
>> NOT NULL" condition on "attribute_value av1" that I don't see in plan 1.
>> And neither plan seems to have much to do with the query, since the
>> query has UNIONs that aren't in the plans.
>>
>> But the short answer seems to be that in both cases, the only reason that
>> the plan doesn't take forever to run is that one sub-join chances to yield
>> precisely zero rows, and the PG executor happens to be more efficient
>> about that corner case in the one plan shape than the other.  The planner
>> doesn't take the possibility of that short-circuit happening into account,
>> since it generally cannot be sure that a sub-join wouldn't yield any rows.
>> So it's just luck that one plan is noticeably faster in this case.
>>
>>                         regards, tom lane
>>
>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2017-01-24 07:18 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-01-18 07:54 Strange influence of default_statistics_target Вадим Акбашев <ufaowl@gmail.com>
2017-01-18 14:18 ` Tom Lane <tgl@sss.pgh.pa.us>
2017-01-19 06:24   ` Вадим Акбашев <ufaowl@gmail.com>
2017-01-19 09:11   ` Вадим Акбашев <ufaowl@gmail.com>
2017-01-24 07:18     ` Вадим Акбашев <ufaowl@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox