Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j56Ia-0004HU-Om for pgsql-general@arkaria.postgresql.org; Fri, 21 Feb 2020 11:16:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j56IY-0002gi-Ha for pgsql-general@arkaria.postgresql.org; Fri, 21 Feb 2020 11:16:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j56IY-0002gb-3X for pgsql-general@lists.postgresql.org; Fri, 21 Feb 2020 11:16:46 +0000 Received: from hm1481-5.locaweb.com.br ([201.76.49.127]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1j56IQ-0007xy-6t for pgsql-general@postgresql.org; Fri, 21 Feb 2020 11:16:44 +0000 Received: from mcbain0008.email.locaweb.com.br (189.126.112.84) by hm1481.locaweb.com.br id h9utv0169v0l for ; Fri, 21 Feb 2020 08:15:56 -0300 (envelope-from ) Received: from proxy.email-ssl.com.br (bartf0025.email.locaweb.com.br [10.31.120.57]) by mcbain0008.email.locaweb.com.br (Postfix) with ESMTP id 9D7FE19FBD7 for ; Fri, 21 Feb 2020 08:15:42 -0300 (-03) x-locaweb-id: qyHzKK_R8aWhjwoIerpCUhsi5PfCUymhK53qXIAegdcRogeYX3-XTadcSpgGGxitWQf3rKGbFuqvLmn5XdJ_XeDnSC7FYZn7HbLXpGF-9IgCFmgQGgrxCDEo15F9LB440m9K1Ks7WpF91ZBvw81jzMVxt2EKEGqqIzIkg6dcUj-wDGpfxhZYSv7g2tr9fc_TjkWl-e-E5Y47eweVIhFOCE0ElwFx-eZqcVXntQdvJEY= NmM3NTY5NzM3MjZmNjI2NTcyNzQ2ZjQwNzM2OTczNjM2ZjYyNzI2MTJlNjM2ZjZkMmU2Mjcy X-LocaWeb-COR: locaweb_2009_x-mail X-AuthUser: luisroberto@siscobra.com.br Received: from [192.168.0.21] (unknown [189.45.198.123]) (Authenticated sender: luisroberto@siscobra.com.br) by proxy.email-ssl.com.br (Postfix) with ESMTPSA id 7ACC7C065D for ; Fri, 21 Feb 2020 08:16:33 -0300 (-03) To: pgsql-general@postgresql.org From: =?UTF-8?Q?Lu=c3=ads_Roberto_Weck?= Subject: Parallel Append Query Message-ID: <5d461f71-85fd-c76c-323a-2f3604ff8fe3@siscobra.com.br> Date: Fri, 21 Feb 2020 08:16:29 -0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------8658C1DD45D142BFF1138C89" Content-Language: pt-BR List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------8658C1DD45D142BFF1138C89 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi! I'm seeing varying number of rows removed by filter on a parallel append query: CREATE TABLE usuario (   id bigint generated always as identity,   nome text ); INSERT INTO usuario (nome) SELECT md5(seq)::text     FROM generate_series(1,500000,1) tab(seq); ANALYZE usuario; EXPLAIN ANALYZE (SELECT * FROM usuario WHERE nome = 'c4ca4238a0b923820dcc509a6f75849b') UNION ALL (SELECT * FROM usuario WHERE nome = 'c81e728d9d4c2f636f067f89cc14862c') Gather  (cost=1000.00..15574.56 rows=2 width=41) (actual time=0.320..56.154 rows=2 loops=1)   Workers Planned: 2   Workers Launched: 2   ->  Parallel Append  (cost=0.00..14574.36 rows=2 width=41) (actual time=13.449..47.899 rows=1 loops=3)         ->  Parallel Seq Scan on usuario (cost=0.00..7287.17 rows=1 width=41) (actual time=8.934..25.476 rows=0 *loops=3*)               Filter: (nome = 'c81e728d9d4c2f636f067f89cc14862c'::text) *Rows Removed by Filter: 166666*         ->  Parallel Seq Scan on usuario usuario_1 (cost=0.00..7287.17 rows=1 width=41) (actual time=13.470..33.630 rows=0 *loops=2*)               Filter: (nome = 'c4ca4238a0b923820dcc509a6f75849b'::text) *Rows Removed by Filter: 250000* Planning Time: 0.106 ms Execution Time: 56.184 ms The execution plan varies just by running the query a few times, and it goes back and forth between these two: Gather  (cost=1000.00..15574.56 rows=2 width=41) (actual time=0.341..55.499 rows=2 loops=1)   Workers Planned: 2   Workers Launched: 2   ->  Parallel Append  (cost=0.00..14574.36 rows=2 width=41) (actual time=15.904..49.821 rows=1 loops=3)         ->  Parallel Seq Scan on usuario (cost=0.00..7287.17 rows=1 width=41) (actual time=0.013..48.591 rows=1 *loops=1*)               Filter: (nome = 'c81e728d9d4c2f636f067f89cc14862c'::text) *Rows Removed by Filter: 499999*         ->  Parallel Seq Scan on usuario usuario_1 (cost=0.00..7287.17 rows=1 width=41) (actual time=15.985..33.621 rows=0 *loops=3*)               Filter: (nome = 'c4ca4238a0b923820dcc509a6f75849b'::text) *Rows Removed by Filter: 166666* Planning Time: 0.107 ms Execution Time: 55.524 ms But I can't understand why does it changes the number of loops from the queries --------------8658C1DD45D142BFF1138C89 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit Hi!

I'm seeing varying number of rows removed by filter on a parallel append query:

CREATE TABLE usuario (
  id bigint generated always as identity,
  nome text
);

INSERT INTO usuario (nome)
SELECT md5(seq)::text
    FROM generate_series(1,500000,1) tab(seq);

ANALYZE usuario;

EXPLAIN ANALYZE
(SELECT * FROM usuario WHERE nome = 'c4ca4238a0b923820dcc509a6f75849b')
UNION ALL
(SELECT * FROM usuario WHERE nome = 'c81e728d9d4c2f636f067f89cc14862c')

Gather  (cost=1000.00..15574.56 rows=2 width=41) (actual time=0.320..56.154 rows=2 loops=1)
  Workers Planned: 2
  Workers Launched: 2
  ->  Parallel Append  (cost=0.00..14574.36 rows=2 width=41) (actual time=13.449..47.899 rows=1 loops=3)
        ->  Parallel Seq Scan on usuario  (cost=0.00..7287.17 rows=1 width=41) (actual time=8.934..25.476 rows=0 loops=3)
              Filter: (nome = 'c81e728d9d4c2f636f067f89cc14862c'::text)
              Rows Removed by Filter: 166666
        ->  Parallel Seq Scan on usuario usuario_1  (cost=0.00..7287.17 rows=1 width=41) (actual time=13.470..33.630 rows=0 loops=2)
              Filter: (nome = 'c4ca4238a0b923820dcc509a6f75849b'::text)
              Rows Removed by Filter: 250000
Planning Time: 0.106 ms
Execution Time: 56.184 ms

The execution plan varies just by running the query a few times, and it goes back and forth between these two:

Gather  (cost=1000.00..15574.56 rows=2 width=41) (actual time=0.341..55.499 rows=2 loops=1)
  Workers Planned: 2
  Workers Launched: 2
  ->  Parallel Append  (cost=0.00..14574.36 rows=2 width=41) (actual time=15.904..49.821 rows=1 loops=3)
        ->  Parallel Seq Scan on usuario  (cost=0.00..7287.17 rows=1 width=41) (actual time=0.013..48.591 rows=1 loops=1)
              Filter: (nome = 'c81e728d9d4c2f636f067f89cc14862c'::text)
              Rows Removed by Filter: 499999
        ->  Parallel Seq Scan on usuario usuario_1  (cost=0.00..7287.17 rows=1 width=41) (actual time=15.985..33.621 rows=0 loops=3)
              Filter: (nome = 'c4ca4238a0b923820dcc509a6f75849b'::text)
              Rows Removed by Filter: 166666
Planning Time: 0.107 ms
Execution Time: 55.524 ms

But I can't understand why does it changes the number of loops from the queries
--------------8658C1DD45D142BFF1138C89--