[HACKERS] Perfomance bug in v10 - Mailing list pgsql-hackers

From Teodor Sigaev
Subject [HACKERS] Perfomance bug in v10
Date
Msg-id 0a12c9c9-be4e-0859-89e8-f61cfc3d01dc@sigaev.ru
Whole thread Raw
List pgsql-hackers
Hi!

I found an example where v10 chooses extremely non-optimal plan:
select
     i::int as a,
     i::int + 1 as b,
     0 as c
into t
from
     generate_series(1,32) as i;

create unique index i on t (c, a);

explain analyze
SELECT
     t1.a, t1.b,
     t2.a, t2.b,
     t3.a, t3.b,
     t4.a, t4.b,
     t5.a, t5.b,
     t6.a, t6.b
     /*
     ,
     t7.a, t7.b,
     t8.a, t8.b,
     t9.a, t9.b,
     t10.a, t10.b
     */
FROM t T1
LEFT OUTER JOIN t T2
ON T1.b = T2.a AND T2.c = 0
LEFT OUTER JOIN t T3
ON T2.b = T3.a AND T3.c = 0
LEFT OUTER JOIN t T4
ON T3.b = T4.a AND T4.c = 0
LEFT OUTER JOIN t T5
ON T4.b = T5.a AND T5.c = 0
LEFT OUTER JOIN t T6
ON T5.b = T6.a AND T6.c = 0
LEFT OUTER JOIN t T7
ON T6.b = T7.a AND T7.c = 0
LEFT OUTER JOIN t T8
ON T7.b = T8.a AND T8.c = 0
LEFT OUTER JOIN t T9
ON T8.b = T9.a AND T9.c = 0
LEFT OUTER JOIN t T10
ON T9.b = T10.a AND T10.c = 0
WHERE T1.c = 0 AND T1.a = 5
;

It takes 4 seconds on my laptop, uncommenting commented lines causes run 
forever. analyzing table or removing index reduces execution time to 
milliseconds regardless on commented or uncommented lines.

The commit
commit 9c7f5229ad68d7e0e4dd149e3f80257893e404d4
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date:   Fri Apr 7 22:20:03 2017 -0400

     Optimize joins when the inner relation can be proven unique.

seems a root this problem - before it the query takes milliseconds. In 
attachment there is a output of explain analyze with commented lines, my 
attention was attracted by a huge number of loops:

  ->  Materialize  (cost=0.00..1.40 rows=1 width=8) (actual time=0.000..0.001 
rows=17 loops=1048576)



-- 
Teodor Sigaev                                   E-mail: teodor@sigaev.ru
                                                    WWW: http://www.sigaev.ru/

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

Attachment

pgsql-hackers by date:

Previous
From: Magnus Hagander
Date:
Subject: [HACKERS] Re: [GENERAL] pg_basebackup error: replication slot"pg_basebackup_2194" already exists
Next
From: "Joshua D. Drake"
Date:
Subject: Re: [HACKERS] TAP backpatching policy