From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Correct way of using complex expressions as partitioning key
Date: Fri, 27 Oct 2023 11:08:39 +0200
Message-ID: <d95d1362-dc57-49f0-b5fd-ce99a247236e@gmx.net> (raw)
In-Reply-To: <20D8B5BB-DB0B-400D-8560-2F5765361CB1@rumyantsev.com>
References: <20D8B5BB-DB0B-400D-8560-2F5765361CB1@rumyantsev.com>
Alexander Rumyantsev schrieb am 27.10.2023 um 06:27:
> Hello!
>
> Is there some correct way to use complex expressions as a key for partitioned table?
> Inserting works as expected, but select runs over all partitions until use complete partition key expression as predicate
>
> test=# create table test (
> id text,
> v1 bigint,
> v2 bigint
> )
> partition by range (((v1 + v2) % 10));
> CREATE TABLE
>
> test=# create table test_1 partition of test for values from (0) to (1);
> CREATE TABLE
>
> test=# create table test_2 partition of test for values from (1) to (2);
> CREATE TABLE
>
> test=# insert into test values (1, 100, 101);
> INSERT 0 1
>
> test=# insert into test values (1, 100, 100);
> INSERT 0 1
>
> test=# select * from test_1;
> id | v1 | v2
> ----+-----+-----
> 1 | 100 | 100
> (1 row)
>
> test=# select * from test_2;
> id | v1 | v2
> ----+-----+-----
> 1 | 100 | 101
> (1 row)
It seems you are trying to simulate hash partitioning using that expression.
Why not use hash partitioning directly then?
create table test (
id text,
v1 bigint,
v2 bigint
)
partition by hash (v1, v2);
create table test_1
partition of test
for values with (modulus 2, remainder 0);
create table test_2
partition of test
for values with (modulus 2, remainder 1);
Then the select will result in:
QUERY PLAN
-----------------------------------------------------------------------------------------------------
Seq Scan on test_1 test (cost=0.00..17.49 rows=1 width=48) (actual time=0.015..0.016 rows=1 loops=1)
Filter: ((v1 = 100) AND (v2 = 100))
Planning Time: 0.159 ms
Execution Time: 0.037 ms
Demo: https://dbfiddle.uk/wuopLYeQ
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: shammat@gmx.net
Subject: Re: Correct way of using complex expressions as partitioning key
In-Reply-To: <d95d1362-dc57-49f0-b5fd-ce99a247236e@gmx.net>
References: <20D8B5BB-DB0B-400D-8560-2F5765361CB1@rumyantsev.com> <d95d1362-dc57-49f0-b5fd-ce99a247236e@gmx.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox