Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLrYZ-00081i-Oo for pgsql-general@arkaria.postgresql.org; Tue, 07 Apr 2020 16:58:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jLrYY-0000Ja-9o for pgsql-general@arkaria.postgresql.org; Tue, 07 Apr 2020 16:58:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLrYX-0000JI-PS for pgsql-general@lists.postgresql.org; Tue, 07 Apr 2020 16:58:34 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jLrYV-000608-8B for pgsql-general@postgresql.org; Tue, 07 Apr 2020 16:58:32 +0000 Received: by mail-lj1-x232.google.com with SMTP id n17so4528662lji.8 for ; Tue, 07 Apr 2020 09:58:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=R9ejRuw4Nsr8HCoSr5TE39bP6CDNpQgdJXU2Y8eNJxk=; b=SdZLefnOKj1O7kf/n6wsW9c8W5OCk9yj88uuFKudh12Jx3Xed6kx4lRpXwmkG+h9/P 0hdFy3FPVFeQUzkO9B87N42EpypjQpUR3vmSLzOrDJy7bsJmd4spuVZKig42R7OJrZqW YwTYdQtZIh5R39EaOylJxupGkDTA7/yedkbikxREYAX3kzG3LWbPb4mRWnEwmmnB2823 TttCXNIrIs6blxD00sucEnnbo3pg8mabsklRYT3esdOSuPIdPkPUUU84wqQRdv1gZ0S9 jlaPtAH7JevviS7eHd4DaVQIZHcJYZcrat5ro6TfdYTahUqkBnh/0YTHCwrG4yZTXgdz hRGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=R9ejRuw4Nsr8HCoSr5TE39bP6CDNpQgdJXU2Y8eNJxk=; b=XYvm63MoVWCqBWTVitNUfzE5LRlOL7HYX1dybi2PGi6JdV8EEERYBSekygVcz9P+8V GP1HWfPAz91nmc19cl2HyR+gUvGYCs8trfGqFcIQOlAuuJouznD4SHt+3x4sx+qaA4Q8 O7ZsonoZeVS1DT2DWFO+2G8LIenFfEm6EHhKTZAUargrnu4VqWqujyJ/YoZ88oCt2gqK TYMN+xyQfzMmg6qjme10LNi3SfNgIwknYtRTXBideXrkhe2VkjnQSSwQytay60AZaL2G gVSK9GXqzhMXRaQ7XrYnPU5wgs14yn09no2XrjmteD4uakWazTyWFy3xHq74cV5dJWHA +jeQ== X-Gm-Message-State: AGi0PuZJAEw/JBbCdCx5vXq/AVZYsSoVlxpaSSUgRMrKKRMhIj1wSdAM uoA1QHx7jL1hazrnSf2D7k1JISj9DknoTeCC//fYSBM7 X-Google-Smtp-Source: APiQypKYVObrf6UYZRqFdFVTYF6U8lRGU3qgkJ3lmZNmgcdBZzSPDnqHGQzMVOVqbzkMSgTASTLYjKBY7MkU5G4cvzU= X-Received: by 2002:a2e:9585:: with SMTP id w5mr2430474ljh.178.1586278709870; Tue, 07 Apr 2020 09:58:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Pavel Stehule Date: Tue, 7 Apr 2020 18:57:52 +0200 Message-ID: Subject: Re: Performance degradation if query returns no rows and column expression is used after upgrading to 12 To: Andrus Cc: pgsql-general Content-Type: multipart/alternative; boundary="00000000000004486f05a2b64b92" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --00000000000004486f05a2b64b92 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable =C3=BAt 7. 4. 2020 v 18:47 odes=C3=ADlatel Andrus napsa= l: > Hi! > > Query returns no rows but its execution time in Postgres 12 depends on th= e > column expression. > > Query with column expression > > coalesce( (select sum(taitmata) from rid join dok using (dokumnr) > where toode=3Dtoode.toode and doktyyp=3D'T' and not dok.taidetud and > dok.kinnitatud and > taitmata is not null),0) > > takes 666 ms : > > explain analyze select > coalesce( (select sum(taitmata) from rid join dok using (dokumnr) > where toode=3Dtoode.toode and doktyyp=3D'T' and not dok.taidetud and > dok.kinnitatud and > taitmata is not null),0) > from toode > where toode.ribakood=3D'testmiin'::text > or toode.nimetus ilike '%'||'testmiin'||'%' escape '!' > or toode.toode ilike '%'||'testmiin'||'%' escape '!' > or toode.markused ilike '%'||'testmiin'||'%' escape '!' > or to_tsvector('english',toode.nimetus) @@ > plainto_tsquery('testmiin') > or to_tsvector('english',toode.engnimetus) @@ > plainto_tsquery('testmiin') > > "Gather (cost=3D1000.00..505930.82 rows=3D153 width=3D32) (actual > time=3D661.419..661.476 rows=3D0 loops=3D1)" > " Workers Planned: 1" > " Workers Launched: 1" > " -> Parallel Seq Scan on toode (cost=3D0.00..10015.31 rows=3D90 width= =3D21) > (actual time=3D574.922..574.922 rows=3D0 loops=3D2)" > " Filter: (((ribakood)::text =3D 'testmiin'::text) OR (nimetus ~~* > '%testmiin%'::text) OR (toode ~~* '%testmiin%'::text) OR > (markused ~~* '%testmiin%'::text) OR (to_tsvector('english'::regconfig, > (nimetus)::text) @@ plainto_tsquery('testmiin'::text)) OR > (to_tsvector('english'::regconfig, (engnimetus)::text) @@ > plainto_tsquery('testmiin'::text)))" > " Rows Removed by Filter: 7202" > " SubPlan 1" > " -> Aggregate (cost=3D3234.63..3234.64 rows=3D1 width=3D32) (never > executed)" > " -> Nested Loop (cost=3D11.26..3234.52 rows=3D43 width=3D3) (= never > executed)" > " -> Bitmap Heap Scan on rid (cost=3D10.84..1191.72 > rows=3D270 width=3D7) (never executed)" > " Recheck Cond: (toode =3D toode.toode)" > " Filter: (taitmata IS NOT NULL)" > " -> Bitmap Index Scan on rid_toode_pattern_idx > (cost=3D0.00..10.77 rows=3D312 width=3D0) (never executed)" > " Index Cond: (toode =3D toode.toode)" > " -> Index Scan using dok_pkey on dok (cost=3D0.42..7.57 > rows=3D1 width=3D4) (never executed)" > " Index Cond: (dokumnr =3D rid.dokumnr)" > " Filter: ((NOT taidetud) AND kinnitatud AND (doktyy= p > =3D 'T'::bpchar))" > "Planning Time: 2.102 ms" > "JIT:" > " Functions: 24" > " Options: Inlining true, Optimization true, Expressions true, Deforming > true" > " Timing: Generation 5.592 ms, Inlining 95.077 ms, Optimization 463.742 > ms, Emission 277.062 ms, Total 841.473 ms" > "Execution Time: 666.007 ms" > > on your query there is too slow JIT. Is strange how much. So the best way is disable JIT probably set jit to off; or same field in postgresql.conf Regards Pavel > Same query with sime column expression > > 1 > > run 3.6 times faster: > > > explain analyze select 1 > from toode > where toode.ribakood=3D'testmiin'::text > or toode.nimetus ilike '%'||'testmiin'||'%' escape '!' > or toode.toode ilike '%'||'testmiin'||'%' escape '!' > or toode.markused ilike '%'||'testmiin'||'%' escape '!' > or to_tsvector('english',toode.nimetus) @@ > plainto_tsquery('testmiin') > or to_tsvector('english',toode.engnimetus) @@ > plainto_tsquery('testmiin') > > "Gather (cost=3D1000.00..11030.61 rows=3D153 width=3D4) (actual > time=3D182.414..185.648 rows=3D0 loops=3D1)" > " Workers Planned: 1" > " Workers Launched: 1" > " -> Parallel Seq Scan on toode (cost=3D0.00..10015.31 rows=3D90 width= =3D4) > (actual time=3D155.338..155.339 rows=3D0 loops=3D2)" > " Filter: (((ribakood)::text =3D 'testmiin'::text) OR (nimetus ~~* > '%testmiin%'::text) OR (toode ~~* '%testmiin%'::text) OR > (markused ~~* '%testmiin%'::text) OR (to_tsvector('english'::regconfig, > (nimetus)::text) @@ plainto_tsquery('testmiin'::text)) OR > (to_tsvector('english'::regconfig, (engnimetus)::text) @@ > plainto_tsquery('testmiin'::text)))" > " Rows Removed by Filter: 7202" > "Planning Time: 1.729 ms" > "Execution Time: 185.674 ms" > > If there are more column expressions, perfomance difference is bigger. > rid table used in column expression contains 1.8 million of rows. > Performance degradation probably occured if upgraded from Postgres 9.1 to > Postgres 12 > > Since no data is returned query perfomance should be same. > How to fix it ? > > Andrus. > > > --00000000000004486f05a2b64b92 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
=C3=BAt 7. 4. 2020 v=C2=A018:47 odes= =C3=ADlatel Andrus <kobruleht2@hot.= ee> napsal:
Hi!

Query returns no rows but its execution time in Postgres 12 depends on the = column expression.

Query with=C2=A0 column expression

coalesce( (select sum(taitmata) from rid join dok using (dokumnr)
where toode=3Dtoode.toode and doktyyp=3D'T' and not dok.taidetud an= d dok.kinnitatud and
taitmata is not null),0)

takes=C2=A0 666 ms :

explain analyze select
coalesce( (select sum(taitmata) from rid join dok using (dokumnr)
where toode=3Dtoode.toode and doktyyp=3D'T' and not dok.taidetud an= d dok.kinnitatud and
taitmata is not null),0)
from toode
=C2=A0 =C2=A0 where toode.ribakood=3D'testmiin'::text
=C2=A0 =C2=A0 =C2=A0 =C2=A0or toode.nimetus ilike '%'||'testmii= n'||'%' escape '!'
=C2=A0 =C2=A0 =C2=A0 =C2=A0or toode.toode ilike '%'||'testmiin&= #39;||'%' escape '!'
=C2=A0 =C2=A0 =C2=A0 =C2=A0or toode.markused ilike '%'||'testmi= in'||'%' escape '!'
=C2=A0 =C2=A0 =C2=A0 =C2=A0or to_tsvector('english',toode.nimetus) = @@ plainto_tsquery('testmiin')
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0or to_tsvector('english',toode.en= gnimetus) @@=C2=A0 plainto_tsquery('testmiin')

"Gather=C2=A0 (cost=3D1000.00..505930.82 rows=3D153 width=3D32) (actua= l time=3D661.419..661.476 rows=3D0 loops=3D1)"
"=C2=A0 Workers Planned: 1"
"=C2=A0 Workers Launched: 1"
"=C2=A0 ->=C2=A0 Parallel Seq Scan on toode=C2=A0 (cost=3D0.00..100= 15.31 rows=3D90 width=3D21) (actual time=3D574.922..574.922 rows=3D0 loops= =3D2)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 Filter: (((ribakood)::text =3D 'testm= iin'::text) OR (nimetus ~~* '%testmiin%'::text) OR (toode ~~* &= #39;%testmiin%'::text) OR
(markused ~~* '%testmiin%'::text) OR (to_tsvector('english'= ::regconfig, (nimetus)::text) @@ plainto_tsquery('testmiin'::text))= OR
(to_tsvector('english'::regconfig, (engnimetus)::text) @@ plainto_t= squery('testmiin'::text)))"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 Rows Removed by Filter: 7202"
"=C2=A0 SubPlan 1"
"=C2=A0 =C2=A0 ->=C2=A0 Aggregate=C2=A0 (cost=3D3234.63..3234.64 ro= ws=3D1 width=3D32) (never executed)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 Nested Loop=C2=A0 (cos= t=3D11.26..3234.52 rows=3D43 width=3D3) (never executed)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 B= itmap Heap Scan on rid=C2=A0 (cost=3D10.84..1191.72 rows=3D270 width=3D7) (= never executed)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 Recheck Cond: (toode =3D toode.toode)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 Filter: (taitmata IS NOT NULL)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 ->=C2=A0 Bitmap Index Scan on rid_toode_pattern_idx=C2=A0 (cost= =3D0.00..10.77 rows=3D312 width=3D0) (never executed)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 Index Cond: (toode =3D toode.toode)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 I= ndex Scan using dok_pkey on dok=C2=A0 (cost=3D0.42..7.57 rows=3D1 width=3D4= ) (never executed)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 Index Cond: (dokumnr =3D rid.dokumnr)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 Filter: ((NOT taidetud) AND kinnitatud AND (doktyyp =3D 'T'= ::bpchar))"
"Planning Time: 2.102 ms"
"JIT:"
"=C2=A0 Functions: 24"
"=C2=A0 Options: Inlining true, Optimization true, Expressions true, D= eforming true"
"=C2=A0 Timing: Generation 5.592 ms, Inlining 95.077 ms, Optimization = 463.742 ms, Emission 277.062 ms, Total 841.473 ms"
"Execution Time: 666.007 ms"


on your query there is too slow JIT. I= s strange how much. So the best way is disable JIT probably
=
set jit to off;

or same field in po= stgresql.conf

Regards

Pav= el


Same query with sime column expression

1

run 3.6 times faster:


explain analyze select 1
from toode
=C2=A0 =C2=A0 where toode.ribakood=3D'testmiin'::text
=C2=A0 =C2=A0 =C2=A0 =C2=A0or toode.nimetus ilike '%'||'testmii= n'||'%' escape '!'
=C2=A0 =C2=A0 =C2=A0 =C2=A0or toode.toode ilike '%'||'testmiin&= #39;||'%' escape '!'
=C2=A0 =C2=A0 =C2=A0 =C2=A0or toode.markused ilike '%'||'testmi= in'||'%' escape '!'
=C2=A0 =C2=A0 =C2=A0 =C2=A0or to_tsvector('english',toode.nimetus) = @@ plainto_tsquery('testmiin')
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0or to_tsvector('english',toode.en= gnimetus) @@
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0plainto_tsquery('testmiin')

"Gather=C2=A0 (cost=3D1000.00..11030.61 rows=3D153 width=3D4) (actual = time=3D182.414..185.648 rows=3D0 loops=3D1)"
"=C2=A0 Workers Planned: 1"
"=C2=A0 Workers Launched: 1"
"=C2=A0 ->=C2=A0 Parallel Seq Scan on toode=C2=A0 (cost=3D0.00..100= 15.31 rows=3D90 width=3D4) (actual time=3D155.338..155.339 rows=3D0 loops= =3D2)"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 Filter: (((ribakood)::text =3D 'testm= iin'::text) OR (nimetus ~~* '%testmiin%'::text) OR (toode ~~* &= #39;%testmiin%'::text) OR
(markused ~~* '%testmiin%'::text) OR (to_tsvector('english'= ::regconfig, (nimetus)::text) @@ plainto_tsquery('testmiin'::text))= OR
(to_tsvector('english'::regconfig, (engnimetus)::text) @@ plainto_t= squery('testmiin'::text)))"
"=C2=A0 =C2=A0 =C2=A0 =C2=A0 Rows Removed by Filter: 7202"
"Planning Time: 1.729 ms"
"Execution Time: 185.674 ms"

If there are more column expressions, perfomance difference is bigger.
rid=C2=A0 table used in column expression contains 1.8 million of rows.
Performance degradation probably occured if upgraded from Postgres 9.1 to P= ostgres 12

Since no data is returned query perfomance should be same.
How to fix it ?

Andrus.


--00000000000004486f05a2b64b92--