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 1j4Nc7-0000rD-Tt for pgsql-general@arkaria.postgresql.org; Wed, 19 Feb 2020 11:34:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j4Nc6-00035x-JO for pgsql-general@arkaria.postgresql.org; Wed, 19 Feb 2020 11:33:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j4Nc6-00035q-4x for pgsql-general@lists.postgresql.org; Wed, 19 Feb 2020 11:33:58 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1j4Nc2-00050I-F8 for pgsql-general@lists.postgresql.org; Wed, 19 Feb 2020 11:33:57 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 6F15621C5F32 for ; Wed, 19 Feb 2020 14:33:53 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.175] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 28AD121C5F31 for ; Wed, 19 Feb 2020 14:33:53 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1582112033; bh=P+ShEWMDu9j0Knw5y8SAtFusjZjPglHXt1j6MJU4ym4=; h=To:From:Subject:Date; b=gBiKb1FZ3+FQyCfD4rP9hOGc/Vzj6rzzstFJPHk+Ps2cL74uphr8/EkzHxd+3aB+R vMGg6aVVrrSzczU4kjKDcUlJf76DGO055j90OqxXo+wyY5Jy2Fykb0A/bsVjNx+Jfl BJtilC0P1V/eQbZr/YfE7OXTZeH88Uo6yL93Gwps= To: pgsql-general From: Pavel Luzanov Subject: Runtime partition pruning with hash partitioning Message-ID: <462eedce-e97c-f974-a9a2-d15670289153@postgrespro.ru> Date: Wed, 19 Feb 2020 14:33:53 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: ru-RU List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, Runtime partition pruning don't work without index on a hash partitioned column. Consider this test case on version 12: create table data (   key_id    integer not null,   value     real not null ) partition by hash(key_id); create table data_0 partition of data for values with (modulus 3, remainder 0); create table data_1 partition of data for values with (modulus 3, remainder 1); create table data_2 partition of data for values with (modulus 3, remainder 2); insert into data (key_id, value) select floor(random() * 100), random() from generate_series(0, 1000000) as g (i); vacuum analyze data; explain (analyze, settings, costs off, timing off, summary off) with keys(id) as (     values (1),(2) ) select * from data join keys on (data.key_id = keys.id);                                  QUERY PLAN ----------------------------------------------------------------------------  Gather (actual rows=19845 loops=1)    Workers Planned: 2    Workers Launched: 2    ->  Hash Join (actual rows=6615 loops=3)          Hash Cond: (data_2.key_id = "*VALUES*".column1)          ->  Parallel Append (actual rows=333334 loops=3)                ->  Parallel Seq Scan on data_2 (actual rows=126670 loops=3)                ->  Parallel Seq Scan on data_1 (actual rows=160458 loops=2)                ->  Parallel Seq Scan on data_0 (actual rows=299075 loops=1)          ->  Hash (actual rows=2 loops=3)                Buckets: 1024  Batches: 1  Memory Usage: 5kB                ->  Values Scan on "*VALUES*" (actual rows=2 loops=3) We see that all partitions scanned. But after creating index postgres actually looks only to selected partitions: create index on data(key_id); explain (analyze, settings, costs off, timing off, summary off) with keys(id) as (     values (1),(2) ) select * from data join keys on (data.key_id = keys.id);                                      QUERY PLAN -------------------------------------------------------------------------------------  Nested Loop (actual rows=19845 loops=1)    ->  Values Scan on "*VALUES*" (actual rows=2 loops=1)    ->  Append (actual rows=9922 loops=2)          ->  Bitmap Heap Scan on data_0 (actual rows=9926 loops=1)                Recheck Cond: (key_id = "*VALUES*".column1)                Heap Blocks: exact=1324                ->  Bitmap Index Scan on data_0_key_id_idx (actual rows=9926 loops=1)                      Index Cond: (key_id = "*VALUES*".column1)          ->  Bitmap Heap Scan on data_1 (never executed)                Recheck Cond: (key_id = "*VALUES*".column1)                ->  Bitmap Index Scan on data_1_key_id_idx (never executed)                      Index Cond: (key_id = "*VALUES*".column1)          ->  Bitmap Heap Scan on data_2 (actual rows=9919 loops=1)                Recheck Cond: (key_id = "*VALUES*".column1)                Heap Blocks: exact=1679                ->  Bitmap Index Scan on data_2_key_id_idx (actual rows=9919 loops=1)                      Index Cond: (key_id = "*VALUES*".column1) Why runtime partition pruning needs index? Is it intended behavior? -- ----- Pavel Luzanov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company