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 1jLrNd-0007iI-C5 for pgsql-general@arkaria.postgresql.org; Tue, 07 Apr 2020 16:47:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jLrNc-0006H4-AN for pgsql-general@arkaria.postgresql.org; Tue, 07 Apr 2020 16:47:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLrNc-0006Gx-2V for pgsql-general@lists.postgresql.org; Tue, 07 Apr 2020 16:47:16 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jLrNZ-0007AO-M7 for pgsql-general@postgresql.org; Tue, 07 Apr 2020 16:47:15 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 2F7D71BB0 for ; Tue, 7 Apr 2020 19:47:12 +0300 (EEST) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lPxbKOkAjR0R for ; Tue, 7 Apr 2020 19:47:09 +0300 (EEST) Received: from mail.hot.ee (hot-relayhost2.estpak.ee [213.180.2.80]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id C90B41D15 for ; Tue, 7 Apr 2020 19:47:09 +0300 (EEST) Received: from dell2 (66-63-196-88.sta.estpak.ee [88.196.63.66]) (Authenticated sender: kobruleht2) by HOT-Relayhost2.estpak.ee (Postfix) with ESMTPSA id 48xYGj5Pdbz3Gd for ; Tue, 7 Apr 2020 19:47:08 +0300 (EEST) Message-ID: From: "Andrus" To: "pgsql-general" Subject: Performance degradation if query returns no rows and column expression is used after upgrading to 12 Date: Tue, 7 Apr 2020 19:47:03 +0300 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="windows-1257"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal Importance: Normal X-Mailer: Microsoft Windows Live Mail 16.4.3528.331 X-MimeOLE: Produced By Microsoft MimeOLE V16.4.3528.331 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi! Query returns no rows but its execution time in Postgres 12 depends on the column expression. Query with column expression coalesce( (select sum(taitmata) from rid join dok using (dokumnr) where toode=toode.toode and doktyyp='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=toode.toode and doktyyp='T' and not dok.taidetud and dok.kinnitatud and taitmata is not null),0) from toode where toode.ribakood='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=1000.00..505930.82 rows=153 width=32) (actual time=661.419..661.476 rows=0 loops=1)" " Workers Planned: 1" " Workers Launched: 1" " -> Parallel Seq Scan on toode (cost=0.00..10015.31 rows=90 width=21) (actual time=574.922..574.922 rows=0 loops=2)" " Filter: (((ribakood)::text = '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=3234.63..3234.64 rows=1 width=32) (never executed)" " -> Nested Loop (cost=11.26..3234.52 rows=43 width=3) (never executed)" " -> Bitmap Heap Scan on rid (cost=10.84..1191.72 rows=270 width=7) (never executed)" " Recheck Cond: (toode = toode.toode)" " Filter: (taitmata IS NOT NULL)" " -> Bitmap Index Scan on rid_toode_pattern_idx (cost=0.00..10.77 rows=312 width=0) (never executed)" " Index Cond: (toode = toode.toode)" " -> Index Scan using dok_pkey on dok (cost=0.42..7.57 rows=1 width=4) (never executed)" " Index Cond: (dokumnr = rid.dokumnr)" " Filter: ((NOT taidetud) AND kinnitatud AND (doktyyp = '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" Same query with sime column expression 1 run 3.6 times faster: explain analyze select 1 from toode where toode.ribakood='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=1000.00..11030.61 rows=153 width=4) (actual time=182.414..185.648 rows=0 loops=1)" " Workers Planned: 1" " Workers Launched: 1" " -> Parallel Seq Scan on toode (cost=0.00..10015.31 rows=90 width=4) (actual time=155.338..155.339 rows=0 loops=2)" " Filter: (((ribakood)::text = '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.