Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bVbey-00089B-K0 for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Aug 2016 09:43:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bVbey-0006cJ-1k for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Aug 2016 09:43:20 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bVbev-0006b4-Tn for pgsql-hackers@postgresql.org; Fri, 05 Aug 2016 09:43:18 +0000 Received: from post.visena.com ([46.226.10.50]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bVben-0007aZ-Qj for pgsql-hackers@postgresql.org; Fri, 05 Aug 2016 09:43:16 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=visena.com; s=20141101.wh; h=Content-Type:MIME-Version:Subject:In-Reply-To:Message-ID:Cc:To:From:Date; bh=zbFEXi5969vgCSvkYffz3kpH/Vdbh/QxMkLsXQalVxo=; b=nnXeJxDSzQvtc9up5mw79XsU/x/7Mq9OA/Gb6A03bjlqE6jSuSrJzxTPmjT3FZ+XAD3K2fs4NVGJwtAnTZgl4Qv6MH3Pw3icmfey1eY8yP4eDGXyNyiQX5xUrm4cDYW25PBMel2Z7A0u5k+88m86BlS6UKvakiPDUpBFVLXm59I=; Received: from [10.0.1.10] (helo=tc7-visena.wh.internal.visena.com) by post.visena.com with esmtp (Exim 4.82) (envelope-from ) id 1bVbei-0007Xg-7N; Fri, 05 Aug 2016 11:43:06 +0200 Received: from localhost ([127.0.0.1] helo=tc7-visena.wh.internal.visena.com) by tc7-visena.wh.internal.visena.com with esmtp (Exim 4.82) (envelope-from ) id 1bVbei-0002Rz-57; Fri, 05 Aug 2016 11:43:04 +0200 Date: Fri, 5 Aug 2016 11:43:04 +0200 (CEST) From: Andreas Joseph Krogh To: Tom Lane Cc: pgsql-hackers@postgresql.org Message-ID: In-Reply-To: <18487.1470351666@sss.pgh.pa.us> Subject: Re: Bogus ANALYZE results for an otherwise-unique column with many nulls MIME-Version: 1.0 X-Mailer: Visena Mail 2.0.2-SNAPSHOT X-Spam-Score: -1.0 X-Spam-Report: SpamAssasin (score=-1.0, required 5.0 ALL_TRUSTED=-1,HTML_MESSAGE=0.001) X-Pg-Spam-Score: -3.2 (---) Content-Type: multipart/related; boundary="----=_Part_406_336258014.1470390184059" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------=_Part_405_406474200.1470390184059 Content-Type: multipart/related; boundary="----=_Part_406_336258014.1470390184059" ------=_Part_406_336258014.1470390184059 Content-Type: multipart/alternative; boundary="----=_Part_407_774994441.1470390184078" ------=_Part_407_774994441.1470390184078 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable P=C3=A5 fredag 05. august 2016 kl. 01:01:06, skrev Tom Lane >: I wrote: > Looking around, there are a couple of places outside commands/analyze.c > that are making the same mistake, so this patch isn't complete, but it > illustrates what needs to be done. Here's a more complete patch. regards, tom lane =C2=A0 (Sorry for HTML-email, but we live in 2016, and I think it improves=20 readability) =C2=A0 Great! =C2=A0 I have tested it (with PG-9.6 from HEAD,=20 e7caacf733f3ee77a555aa715ab6fbf4434e6b52) and it sure looks like it fixes t= he=20 problem for my query. =C2=A0 The=C2=A0query: =C2=A0 explain analyze SELECT log.relation_id as company_id , sum(log.duration) A= S=20 durationFROM onp_crm_activity_log log JOIN onp_crm_person logfor ON=20 logfor.onp_user_id =3Dlog.logged_for AND logfor.is_resource =3D FALSE WHERE= 1 =3D 1=20 -- Filter out already invoiced AND NOT EXISTS( SELECT * FROM=20 onp_crm_calendarentry_invoice_membership cemJOIN onp_crm_invoice_line il ON= =20 cem.invoice_line_id =3D il.idJOIN onp_crm_invoice inv ON il.invoice_id =3D= =20 inv.entity_idWHERE cem.calendar_entry_id =3D log.id AND NOT EXISTS( SELECT = * FROM=20 onp_crm_invoice creditnoteWHERE il.invoice_id =3D creditnote.credit_against= ) )=20 GROUP BY log.relation_id ;=20 =C2=A0 Gave the following explain-plan (before the patch), with enable_nestloop=3D= off=20 (needed to prevent nest-loop-anti-join which caused this query to execute i= n=20 ~26 min.): =C2=A0 QUERY PLAN=20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------------------------------------------------------------- HashAggregate (cost=3D25201.62..25220.06 rows=3D1475 width=3D36) (actual ti= me =3D381.191..381.281rows=3D404 loops=3D1) Group Key: log.relation_id -> Hash= Anti Join=20 (cost=3D4782.34..25148.28 rows=3D10667 width=3D12) (actual time=3D103.683..= 361.409 rows=3D 148330loops=3D1) Hash Cond: (log.id =3D cem.calendar_entry_id) -> Hash Join= (cost =3D81.46..20312.73rows=3D10667 width=3D20) (actual time=3D0.100..156.432 ro= ws=3D380617=20 loops=3D1) Hash Cond: (log.logged_for =3D logfor.onp_user_id) -> Seq Scan o= n=20 onp_crm_activity_loglog (cost=3D0.00..18696.71 rows=3D380771 width=3D24) (a= ctual time =3D0.005..49.195rows=3D380774 loops=3D1) -> Hash (cost=3D25.02..25.02 rows= =3D4515 width=3D8 ) (actualtime=3D0.078..0.078 rows=3D119 loops=3D1) Buckets: 8192 Batches: 1= Memory=20 Usage: 69kB -> Index Scan using onp_crm_person_onp_id_idx on onp_crm_person= =20 logfor (cost=3D0.14..25.02 rows=3D4515 width=3D8) (actual time=3D0.005..0.0= 63 rows=3D119=20 loops=3D1) Filter: (NOT is_resource) Rows Removed by Filter: 8 -> Hash (cos= t =3D4700.87..4700.87rows=3D1 width=3D4) (actual time=3D103.575..103.575 rows= =3D232412=20 loops=3D1) Buckets: 262144 (originally 1024) Batches: 1 (originally 1) Memo= ry=20 Usage: 10219kB -> Hash Join (cost=3D474.41..4700.87 rows=3D1 width=3D4) (ac= tual time =3D9.724..76.015rows=3D232412 loops=3D1) Hash Cond: (cem.invoice_line_id = =3D il.id) ->=20 Seq Scanon onp_crm_calendarentry_invoice_membership cem (cost=3D0.00..3354.= 28 rows =3D232528 width=3D8) (actual time=3D0.004..17.626 rows=3D232528 loops=3D1) = -> Hash (cost =3D474.40..474.40rows=3D1 width=3D4) (actual time=3D9.710..9.710 rows=3D115= 35 loops=3D1)=20 Buckets:16384 (originally 1024) Batches: 1 (originally 1) Memory Usage: 534= kB=20 -> MergeJoin (cost=3D415.33..474.40 rows=3D1 width=3D4) (actual time=3D4.14= 9..8.467 rows =3D11535 loops=3D1) Merge Cond: (il.invoice_id =3D inv.entity_id) -> Sort (= cost =3D415.05..415.06rows=3D1 width=3D8) (actual time=3D4.138..4.701 rows=3D115= 35 loops=3D1)=20 SortKey: il.invoice_id Sort Method: quicksort Memory: 925kB -> Hash Anti Jo= in ( cost=3D77.13..415.04 rows=3D1 width=3D8) (actual time=3D0.257..2.716 rows= =3D11535 loops=3D1) HashCond: (il.invoice_id =3D creditnote.credit_against) -> Seq Scan on=20 onp_crm_invoice_line il (cost=3D0.00..294.30 rows=3D11630 width=3D8) (actua= l time =3D0.003..0.995rows=3D11630 loops=3D1) -> Hash (cost=3D50.38..50.38 rows=3D= 2140 width=3D4)=20 (actualtime=3D0.247..0.247 rows=3D37 loops=3D1) Buckets: 4096 Batches: 1 Me= mory Usage: 34kB -> Index Only Scan using origo_invoice_credit_against_idx on=20 onp_crm_invoice creditnote (cost=3D0.28..50.38 rows=3D2140 width=3D4) (actu= al time =3D0.013..0.182rows=3D2140 loops=3D1) Heap Fetches: 0 -> Index Only Scan us= ing=20 origo_invoice_id_status_sent_idxon onp_crm_invoice inv (cost=3D0.28..53.98 = rows=3D 2140width=3D8) (actual time=3D0.008..1.274 rows=3D11576 loops=3D1) Heap Fet= ches: 0=20 Planningtime: 0.955 ms Execution time: 381.884 ms (35 rows)=20 =C2=A0 AFAIU, this is the problematic estimate:=C2=A0-> Hash Anti Join (cost=3D77.= 13..415.04=20 rows=3D1 width=3D8) (actual time=3D0.257..2.716 rows=3D11535 loops=3D1) Right? =C2=A0 now (after the patch, and without needing to disable nest_loop)=C2=A0gives = the=20 following explain-plan: QUERY PLAN=20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ------------------------------------------------------------------ HashAggregate (cost=3D44409.89..44428.47 rows=3D1486 width=3D36) (actual ti= me =3D366.502..366.594rows=3D404 loops=3D1) Group Key: log.relation_id -> Hash= Anti Join=20 (cost=3D10157.30..43650.11 rows=3D151956 width=3D12) (actual time=3D99.420.= .347.309 rows =3D148327 loops=3D1) Hash Cond: (log.id =3D cem.calendar_entry_id) -> Merge= Join (cost =3D0.56..26729.34rows=3D379984 width=3D20) (actual time=3D0.011..150.415 ro= ws=3D380614=20 loops=3D1) Merge Cond: (logfor.onp_user_id =3D log.logged_for) -> Index Sca= n using=20 onp_crm_person_onp_id_idxon onp_crm_person logfor (cost=3D0.14..25.02 rows= =3D4515=20 width=3D8) (actual time=3D0.006..0.030 rows=3D35 loops=3D1) Filter: (NOT is= _resource)=20 RowsRemoved by Filter: 2 -> Index Scan using origo_activity_log_logged_for_= idx=20 ononp_crm_activity_log log (cost=3D0.42..21977.16 rows=3D380771 width=3D24)= (actual=20 time=3D0.003..69.406 rows=3D380623 loops=3D1) -> Hash (cost=3D7300.48..7300= .48 rows=3D 228500width=3D4) (actual time=3D99.226..99.226 rows=3D232412 loops=3D1) Buc= kets: 262144=20 Batches:1 Memory Usage: 10219kB -> Hash Join (cost=3D789.13..7300.48 rows= =3D228500=20 width=3D4) (actual time=3D8.043..67.079 rows=3D232412 loops=3D1) Hash Cond:= =20 (cem.invoice_line_id =3D il.id) -> Seq Scanon=20 onp_crm_calendarentry_invoice_membership cem (cost=3D0.00..3354.28 rows=3D2= 32528=20 width=3D8) (actual time=3D0.007..16.230 rows=3D232528 loops=3D1) -> Hash (c= ost =3D646.27..646.27rows=3D11429 width=3D4) (actual time=3D8.003..8.003 rows= =3D11535 loops=3D1 ) Buckets:16384 Batches: 1 Memory Usage: 534kB -> Merge Anti Join (cost =3D0.85..646.27rows=3D11429 width=3D4) (actual time=3D0.021..6.393 rows=3D1= 1535 loops=3D1)=20 Merge Cond: (il.invoice_id =3D creditnote.credit_against) -> MergeJoin (cos= t =3D0.57..613.99rows=3D11630 width=3D8) (actual time=3D0.010..4.771 rows=3D1= 1630 loops=3D1)=20 Merge Cond: (inv.entity_id =3D il.invoice_id) ->Index Only Scan using=20 origo_invoice_id_status_sent_idxon onp_crm_invoice inv (cost=3D0.28..53.98 = rows=3D 2140width=3D8) (actual time=3D0.006..0.318 rows=3D2140 loops=3D1) Heap Fetc= hes: 0 ->=20 IndexScan using onp_crm_invoice_line_invoice_id_lineno_key on=20 onp_crm_invoice_line il (cost=3D0.29..409.28 rows=3D11630 width=3D8) (actua= l time =3D0.003..2.323rows=3D11630 loops=3D1) -> Index Only Scan using=20 onp_crm_invoice_credit_against_keyon onp_crm_invoice creditnote (cost =3D0.28..47.98rows=3D2140 width=3D4) (actual time=3D0.011..0.016 rows=3D38 = loops=3D1) Heap=20 Fetches:0 Planning time: 0.931 ms Execution time: 366.710 ms (28 rows)=20 =C2=A0 And, for fun with=C2=A0max_parallel_workers_per_gather =3D 6 QUERY PLAN=20 ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= ---------------------------------------------------------------------------= --------------- Finalize GroupAggregate (cost=3D39788.73..39851.89 rows=3D1486 width=3D36) = (actual=20 time=3D234.289..234.747 rows=3D404 loops=3D1) Group Key: log.relation_id ->= Sort (cost =3D39788.73..39799.88rows=3D4458 width=3D36) (actual time=3D234.276..234.30= 7 rows=3D1000=20 loops=3D1) Sort Key: log.relation_id Sort Method: quicksort Memory: 103kB -= >=20 Gather (cost=3D39054.15..39518.53 rows=3D4458 width=3D36) (actual time =3D233.012..234.110rows=3D1000 loops=3D1) Workers Planned: 3 Workers Launch= ed: 3 ->=20 PartialHashAggregate (cost=3D38054.15..38072.73 rows=3D1486 width=3D36) (ac= tual time =3D225.607..225.701rows=3D250 loops=3D4) Group Key: log.relation_id -> Hash= Join (cost =3D10238.19..37294.37rows=3D151956 width=3D12) (actual time=3D163.060..219.= 652 rows=3D 37082loops=3D4) Hash Cond: (log.logged_for =3D logfor.onp_user_id) -> Hash = Anti Join (cost=3D10156.73..28460.48 rows=3D152271 width=3D16) (actual time=3D162.618= ..212.351=20 rows=3D37121 loops=3D4) Hash Cond: (log.id =3D cem.calendar_entry_id) -> Pa= rallel Seq=20 Scanon onp_crm_activity_log log (cost=3D0.00..16117.29 rows=3D122829 width= =3D24)=20 (actualtime=3D0.010..15.532 rows=3D95193 loops=3D4) -> Hash (cost=3D7300.48= ..7300.48=20 rows=3D228500 width=3D4) (actual time=3D161.270..161.270 rows=3D232412 loop= s=3D4) Buckets: 262144Batches: 1 Memory Usage: 10219kB -> Hash Join (cost=3D789.13..7300.48= rows=3D 228500width=3D4) (actual time=3D18.276..112.034 rows=3D232412 loops=3D4) Ha= sh Cond:=20 (cem.invoice_line_id =3D il.id) -> Seq Scanon=20 onp_crm_calendarentry_invoice_membership cem (cost=3D0.00..3354.28 rows=3D2= 32528=20 width=3D8) (actual time=3D0.045..26.061 rows=3D232528 loops=3D4) -> Hash (c= ost =3D646.27..646.27rows=3D11429 width=3D4) (actual time=3D18.116..18.116 rows= =3D11535 loops=3D 4) Buckets: 16384 Batches: 1 Memory Usage: 534kB -> Merge Anti Join (cost =3D0.85..646.27rows=3D11429 width=3D4) (actual time=3D0.179..14.505 rows=3D= 11535 loops=3D4)=20 Merge Cond: (il.invoice_id =3D creditnote.credit_against) -> MergeJoin (cos= t =3D0.57..613.99rows=3D11630 width=3D8) (actual time=3D0.113..10.871 rows=3D= 11630 loops=3D4)=20 Merge Cond: (inv.entity_id =3D il.invoice_id) ->Index Only Scan using=20 origo_invoice_id_status_sent_idxon onp_crm_invoice inv (cost=3D0.28..53.98 = rows=3D 2140width=3D8) (actual time=3D0.063..0.811 rows=3D2140 loops=3D4) Heap Fetc= hes: 0 ->=20 IndexScan using onp_crm_invoice_line_invoice_id_lineno_key on=20 onp_crm_invoice_line il (cost=3D0.29..409.28 rows=3D11630 width=3D8) (actua= l time =3D0.046..5.213rows=3D11630 loops=3D4) -> Index Only Scan using=20 onp_crm_invoice_credit_against_keyon onp_crm_invoice creditnote (cost =3D0.28..47.98rows=3D2140 width=3D4) (actual time=3D0.063..0.071 rows=3D38 = loops=3D4) Heap=20 Fetches:0 -> Hash (cost=3D25.02..25.02 rows=3D4515 width=3D8) (actual time =3D0.325..0.325rows=3D119 loops=3D4) Buckets: 8192 Batches: 1 Memory Usage:= 69kB ->=20 IndexScan using onp_crm_person_onp_id_idx on onp_crm_person logfor (cost =3D0.14..25.02rows=3D4515 width=3D8) (actual time=3D0.032..0.270 rows=3D119= loops=3D4)=20 Filter: (NOT is_resource) Rows Removed by Filter: 8 Planning time: 2.443 ms= =20 Executiontime: 239.979 ms (38 rows)=20 =C2=A0 All in all, thanks for looking into this, and +1 for=C2=A0backpatching to 9= .5. =C2=A0 -- Andreas Joseph Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963 andreas@visena.com www.visena.com =C2=A0 ------=_Part_407_774994441.1470390184078 Content-Type: text/html;charset=UTF-8 Content-Transfer-Encoding: quoted-printable
P=C3=A5 fredag 05. august 2016 kl. 01:01:06, skrev Tom Lane <tgl@sss.pgh.pa.us>:
I w= rote:
> Looking around, there are a couple of places outside commands/analyze.= c
> that are making the same mistake, so this patch isn't complete, but it=
> illustrates what needs to be done.

Here's a more complete patch.

regards, tom lane
=C2=A0
(Sorry for HTML-email, but we live in 2016, and I think it improves re= adability)
=C2=A0
Great!
=C2=A0
I have tested it (with PG-9.6 from HEAD, e7caacf733f3ee77a555aa715ab6f= bf4434e6b52) and it sure looks like it fixes the problem for my query.
=C2=A0
The=C2=A0query:
=C2=A0
explain analyze
SELECT
      log.rel=
ation_id as company_=
id
    , sum(log.duration) AS duration
FROM onp_crm_activit=
y_log log
    JOIN onp_=
crm_person logfor ON logfor.onp_user_id =3D log=
.logged_for AND logfor.is_resource =3D F=
ALSE
WHERE 1 =3D 1
      -- Filter out already invoiced
      AND NOT EXISTS(
    SELECT * FROM onp_crm_calendarentry_=
invoice_membership cem
        JOIN onp_crm=
_invoice_line il ON =
cem.invoice_line_id =3D il.id
        JOIN onp_crm=
_invoice inv ON il.i=
nvoice_id =3D inv.entity_id
    WHERE cem.calend=
ar_entry_id =3D log.=
id
          AND NOT EXISTS(
        SELECT * FROM onp_crm_invoice cr=
editnote
        WHERE il.inv=
oice_id =3D creditnote.credit_against
    )
)
GROUP BY
    log.relat=
ion_id
;
=C2=A0
Gave the following explain-plan (before the patch), with enable_nestlo= op=3Doff (needed to prevent nest-loop-anti-join which caused this query to = execute in ~26 min.):
=C2=A0
                                           =
                                                               QUERY PLAN                     =
                                                                           =
          =20
--------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
-----------------------------------------------
 HashAggrega=
te  (cost=3D25201.62=
..25220.06 rows=3D1475 width=3D36) (actual tim=
e=3D381.191..381.281 rows=3D404 loops=3D1)
   Group Key: log.relation_id
   ->  Hash Anti =
Join  (cost=3D4782.34..25148.28 rows=3D10667 width=3D12) =
(actual time=3D103.6=
83..361.409 rows=3D<=
span style=3D"color:#0000ff;">148330 loops=3D1)
         Hash Cond: =
(log.id =3D cem.cale=
ndar_entry_id)
         ->  Hash Join  =
(cost=3D81.46=
..20312.73 rows=3D10667 width=3D20) (actual ti=
me=3D0.100..156.432 =
rows=3D380617 loops=3D1)
               Hash =
Cond: (log.logged_fo=
r =3D logfor.onp_user_id)
               ->  Seq Scan on onp_crm_activity_log log  (cost=3D0.00..18696.71 rows=
=3D380771 width=3D24) (actual time=3D0.005..49.195 rows=3D380774 loo=
ps=3D1)
               ->  Hash =
 (cost=3D25.0=
2..25.02 rows=3D4515 width=3D8) (actual time=3D0.078..0.078 rows=3D119 loops=3D1)
                     Buckets: 8192  B=
atches: 1  Memory Usage: 6=
9kB
                     ->  Index Scan using onp_crm_person_onp_id_idx on onp_crm_person logfor  (cost=3D0.14..25.02 rows=3D4515 width=3D=
8) (actual time=3D0.005..0.063 rows=3D119 loops=3D1)
                           =
Filter: (NOT =
is_resource)
                           =
Rows Removed by Filt=
er: 8
         ->  Hash  (cost=3D4700.87..4700.87 rows=3D1 width=3D4) (actual time=3D103.575..103.575 rows=3D232412 loops=3D1)
               Buckets: 262144 (origi=
nally 1024)  Batches: 1 (originally 1)  Memory Usage: 10219kB
               ->  Hash =
Join  (cost=
=3D474.41..4700.87 rows=3D1 width=3D4) (actual time=3D9.724..76.015 rows=3D232412 loops=3D1)
                     Hash <=
/span>Cond: (cem.invoice_line_id =3D il.id)
                     ->  Seq Scan on onp_crm_calendarentry_invoice_membership cem  (cost=3D0.00..3354.28 rows=3D232528 width=3D8) (=
actual time=3D0.004.=
.17.626 rows=3D232528 loops=3D1)
                     ->  Hash  (cost=
=3D474.40..474.40 rows=3D1 width=3D4) (actual =
time=3D9.710..9.710 =
rows=3D11535 loops=3D1)
                           Buckets: 16384 (originally 1024)  Batches: 1 (originally 1)  Memory Usage=
: 534kB
                           ->  Merge Join  (=
cost=3D415.33..474.40 rows=3D1 width=3D4) (actual time=3D4.149..8.467 rows=3D11535 loops=
=3D1)
                                 Merge Cond: (il.invoice_id =3D inv.entity_=
id)
                                 ->  Sort  (cost=3D415.05..415.06 rows=3D1 width=3D8) (actual time=3D4.138..4.701 rows=3D11535 loops=3D1)
                                       Sort Key: il.invoice_id
                                       Sort Method: quicksort  Memory: 925kB
                                       ->  Hash Anti Join  (cost=3D77.13..415.04 rows=3D=
1 width=3D8) (actual time=3D0.=
257..2.716 rows=3D11535 loops=3D1)
                                             Hash Cond: (il.invoice_id =3D creditnote.credit_ag=
ainst)
                                             ->  Seq Scan on onp_crm_invoice_line il  (cost=3D0.00..294.30 rows=3D11630 width=3D8) =
(actual time=3D0.003=
..0.995 rows=3D11630 loops=3D1)
                                             ->  Hash  (cost=3D50.38..50.38 rows=3D2140 width=
=3D4) (actual time=3D0.247..0.247 rows=3D37 loops=3D1)
                                                   Buckets: 4096  Batches: 1  Memory Usage: 34kB
                                                   ->  Index Only Scan using origo_invoice_credit_against_idx on onp_crm_invoice cre=
ditnote  (cost=3D0.2=
8..50.38 rows=3D2140 width=3D4) (actual time=3D0.013..0.182 rows=3D2140 loops=3D1)
                                                         Heap Fetches: 0
                                 ->  Index Only Scan using origo_in=
voice_id_status_sent_idx on=
 onp_crm_invoice inv  (cost=3D0.28..53.98 rows=3D2140 width=3D8) (actual time=3D0.008..1.274 rows=3D11576 loo=
ps=3D1)
                                       Heap Fetches: 0
 Planning time: 0.9=
55 ms
 Execution time: 381.884 ms
(35 rows)
=C2=A0
AFAIU, this is the problematic estimate:=C2=A0-> Hash Anti Join (cost=3D77.13..415.04 rows=3D1 width=3D= 8) (actual time=3D0.257..2.716= rows=3D11535 loops=3D1)
Right?
=C2=A0
now (after the patch, and without needing to disable nest_loop)=C2=A0g= ives the following explain-plan:
                                           =
                                                            QUERY PLAN                         =
                                                                           =
  =20
--------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
----------------------------------------
 HashAggrega=
te  (cost=3D44409.89=
..44428.47 rows=3D1486 width=3D36) (actual tim=
e=3D366.502..366.594 rows=3D404 loops=3D1)
   Group Key: log.relation_id
   ->  Hash Anti =
Join  (cost=3D10157.30..43650.11 rows=3D151956 width=3D12=
) (actual time=3D99.=
420..347.309 rows=3D=
148327 loops=3D1)
         Hash Cond: =
(log.id =3D cem.cale=
ndar_entry_id)
         ->  Merge Join =
 (cost=3D0.56=
..26729.34 rows=3D379984 width=3D20) (actual t=
ime=3D0.011..150.415 rows=3D380614 loops=3D1)
               Merge Cond: (logfor.onp_user_id =3D log.logged_for)
               ->  Index=
 Scan using o=
np_crm_person_onp_id_idx on=
 onp_crm_person logfor  (cost=3D0.14..25.02 rows=3D4515 width=3D8) (actual time=3D0.006..0.030 rows=3D35 loop=
s=3D1)
                     Filter=
: (NOT is_res=
ource)
                     Rows <=
/span>Removed by Filter: 2
               ->  Index Scan using origo_activity_log_logged_for_i=
dx on onp_crm_activi=
ty_log log  (cost=3D0.42..21977.16 rows=3D380771 width=3D24=
) (actual time=3D0.0=
03..69.406 rows=3D380623 loops=3D1)
         ->  Hash  (cost=3D7300.48..7=
300.48 rows=3D228500 width=3D4) (actual time=3D99.226..99.226 rows=
=3D232412 loops=3D1)
               Buckets: 262144  Batch=
es: 1  Memory Usage: 10219=
kB
               ->  Hash =
Join  (cost=
=3D789.13..7300.48 rows=3D228500 width=3D4) (actual time=3D8.043..67.079 rows=3D232412 loops=3D<=
span style=3D"color:#0000ff;">1)
                     Hash <=
/span>Cond: (cem.invoice_line_id =3D il.id)
                     ->  Seq Scan on onp_crm_calendarentry_invoice_membership cem  (cost=3D0.00..3354.28 rows=3D232528 width=3D8) (=
actual time=3D0.007.=
.16.230 rows=3D232528 loops=3D1)
                     ->  Hash  (cost=
=3D646.27..646.27 rows=3D11429 width=3D4) (actual time=3D8.003..8.003 rows=3D11535 loops=3D1)
                           Buckets: 16384  <=
/span>Batches: 1  Memory Usage: 534kB
                           ->  Merge Anti Join  (cost=3D0.85..646.27 rows=3D11429 width=3D4) (actual time=3D0.021..6.393 rows=3D11535 loops=3D1)
                                 Merge Cond: (il.invoice_id =3D creditnote.=
credit_against)
                                 ->  Merge Join  (cost=3D0.57..613.99 rows=3D11630 width=3D8) (actual time=3D0.010..4.771 rows=3D11630 loops=3D1)
                                       Merge Cond: (inv.entity_id =3D il.in=
voice_id)
                                       ->  Index Only Scan using origo_invoice_id_status_sent_idx on onp_crm_invoice inv  (cost=3D0.28..53.98 rows=3D2140 width=3D8) (actua=
l time=3D0.006..0.31=
8 rows=3D2140 loops=3D1)
                                             Heap Fetches: 0
                                     =
  ->  Index Scan using onp_crm=
_invoice_line_invoice_id_lineno_key on onp_crm_invoice_line il  (cost=3D0.29..409.28 rows=3D11630 width=3D8) (actual time=3D0.003..2.323 rows=3D11630 loops=3D1)
                                 ->  Index Only Scan using onp_crm_invoice_credit_against_key on onp_crm_invoice creditnote  (cost=3D0.28..47.98 rows=3D2140 width=3D4) (ac=
tual time=3D0.011..0=
.016 rows=3D38 loops=3D1)
                                       Heap Fetches: 0
 Planning time: 0.9=
31 ms
 Execution time: 366.710 ms
(28 rows)
=C2=A0
And, for fun with=C2=A0max_parallel_workers_per_gather =3D 6
                                           =
                                                                        QUERY PLAN             =
                                                                           =
                          =20
--------------------------=
---------------------------------------------------------------------------=
---------------------------------------------------------------------------=
----------------------------------------------------------------
 Finalize Gr=
oupAggregate  (cost=
=3D39788.73..39851.89 rows<=
/span>=3D1486 width=3D36) (actual time=3D234.289..234.747 rows=3D404 loops=
=3D1)
   Group Key: log.relation_id
   ->  Sort  (cost=3D39788.73..39799.88 rows=
=3D4458 width=3D36) (actual time=3D234.276..234.307 rows=3D1000 loops=
=3D1)
         Sort Key: <=
span style=3D"color:#000080;font-weight:bold;">log.relation_id
         Sort Method: quicksort  Memory: 103=
kB
         ->  Gather  (co=
st=3D39054.15..39518.53 rows=3D4458 width=3D36) (actual time=3D233.012..234.110 rows=3D1000 loops=3D1)
               Workers Planned: 3
               Workers Launche=
d: 3
               ->  Partial HashAggregate  (cost=3D38054.15..38072.7=
3 rows=3D1486 width=3D36) (actual time=
=3D225.607..225.701 rows=3D250 loops=3D4)
                     Group =
Key: log.rela=
tion_id
                     ->  Hash Join  (cost=3D10238.19..37294.37 r=
ows=3D151956 width=3D12) (actual time=3D163.060..219.652 rows=3D37082 loops=3D4)
                           =
Hash Cond: (log.logged_for =3D logfor.onp_user_id)
                           ->  Hash Anti Joi=
n  (cost=3D10=
156.73..28460.48 rows=3D152271 width=3D16) (actual time=3D162.618..212.351 rows=3D37121 loops=3D=
4)
                                 Hash Cond: (lo=
g.id =3D cem.calendar_entry_id)
                                 ->  Parallel Seq Scan on onp_crm_activity_log log  (cost=3D0.00..16117.29 rows=3D12282=
9 width=3D24) (actual time=3D0.010..15.532 rows=3D95193 loops=3D4)
                                 ->  Hash  (=
cost=3D7300.48..7300.48 rows=3D228500 width=3D4) (actual time=3D161.270..161.270 rows=3D232412 <=
/span>loops=3D4)
                                       Buckets: 262144  Batches: 1  Memory=
 Usage: 10219kB
                                       ->  Hash Join  (cost=3D789.13..7300.48 rows=3D228500 wi=
dth=3D4) (actual time=3D18.276..112.034 rows=3D232412 loops=3D4)
                                             Hash Cond: (cem.invoice_line_id =3D il.id)
                                             ->  Seq Scan on onp_crm_calendarentry_invoice_m=
embership cem  (cost=
=3D0.00..3354.28 rows=3D232528 width=3D8) (actual time=3D0.045..26.061 rows=3D232528 loops=3D4)
                                             ->  Hash  (cost=3D646.27..646.27 rows=3D11429 wid=
th=3D4) (actual time=3D18.116..18.116 rows=3D1=
1535 loops=3D4)
                                                   Buckets: 16384  Batches: 1  Memory Usage: <=
span style=3D"color:#0000ff;">534kB
                                                   ->  Merge Anti Join  (cost=3D0.85..646.27 rows=3D1=
1429 width=3D4) (actual time=3D0.179..14.505 rows=3D11535 loops=3D4)
                                                         Merge Cond: (il.in=
voice_id =3D creditnote.credit_against)
                                                         ->  Merge Join  (cost=3D0.57..613.99 rows=3D=
11630 width=3D8) (actual time=3D0.113..10.871 rows=3D11630 loops=3D4)
                                                               Merge Cond: =
(inv.entity_id =3D il.invoice_id)
                                                               ->  Index Only Scan using origo_invoice_id_status=
_sent_idx on onp_crm=
_invoice inv  (cost=
=3D0.28..53.98 rows=
=3D2140 width=3D8) (actual time=3D0.063..0.811 rows=3D2140 loops=3D4)
                                                                     Heap F=
etches: 0
                                     =
                          ->  Index Scan using onp_crm_invoice_line_invoice_id_lineno_key on onp_crm_invoice_line il  (cost=3D0.29..409.28 rows=3D11630 width=3D8) (=
actual time=3D0.046.=
.5.213 rows=3D11630 loops=3D4)
                                                         ->  Index Only Scan using onp_crm_invoice_credit_agains=
t_key on onp_crm_inv=
oice creditnote  (cost=3D0.28..47.98 rows=3D2140 width=3D4) (actual time=3D0.063..0.071 rows=3D38 loops=3D4)
                                                               Heap Fetches=
: 0
                           -&g=
t;  Hash  (cost=3D25.02..25.02 rows=3D4515 width=3D8) (actu=
al time=3D0.325..0.3=
25 rows=3D119 loops=3D4)
                                 Buckets: 81=
92  Batches: 1  Memory Usage: 69kB
                                 ->  Index Scan using onp_crm_person_onp_id_idx on onp_crm_person logfor  (cost=3D0.14..25.02 rows=3D4515 width=3D8) (actual time=3D0.032..0.270 rows=3D119 loops=3D4)
                                       Filter: (NOT is_resource)
                                       Rows Removed by Filter: 8
 Planning time: 2.4=
43 ms
 Execution time: 239.979 ms
(38 rows)
=C2=A0
All in all, thanks for looking into this, and +1 for=C2=A0backpatching= to 9.5.
=C2=A0
--
Andrea= s Joseph Krogh
CTO / Partner<= /span> - Visena AS
Mobile: +47 90= 9 56 963
=3D""
=C2=A0
------=_Part_407_774994441.1470390184078-- ------=_Part_406_336258014.1470390184059 Content-Type: image/png Content-Transfer-Encoding: base64 Content-Disposition: inline Content-ID: iVBORw0KGgoAAAANSUhEUgAAAIUAAAAYCAYAAADUIj6hAAAABHNCSVQICAgIfAhkiAAABzBJREFU aEPtmNFxHDcMhmVP3i1VECpvnjzkVIHWFfhcgVcVRKrAUgWRK/C6Al8H3lTgy0PGbzFdQc4VJP/H ADs43q6kROeJNbOYgQACIAgCWJKng4MZ5gxUGXh0U0b+SIuF9G+EWXj2Q15vbrE/NPtk9uub7Gfd t5mBx1NhqSEo8HshjbEUtlO2yIM9tsx5dZP9rPt2MzDZFAqZE4LGcFjdsg3saQaHt7fY70X949On aS+OZidDBkavD33157L4xaw2os+Mp/DAC10l2XhOCeStj0W5ajrJL8WfCi80Xgf9vVlrhg9yRONe /f7xI2vNsIcM7JwU9o6oGyJrLT8JOA1aX9sKP4wl94bAniukEbo/n7YPmuTETzIab4Y9ZWCrKexd 8M58lxPCvvD6aihfvexbEQoPYO8NQRO0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