pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedpartition by range or by list constraint check (was Re: literal vs dynamic partition constraint in plan execution)
2+ messages / 2 participants
[nested] [flat]
* partition by range or by list constraint check (was Re: literal vs dynamic partition constraint in plan execution)
@ 2019-09-02 08:40 Luca Ferrari <fluca1978@gmail.com>
0 siblings, 1 reply; 2+ messages in thread
From: Luca Ferrari @ 2019-09-02 08:40 UTC (permalink / raw)
To: pgsql-general
I've done a simple test case, and find out that probably the problem I
got was due to the partition schema I'm using.
I want a table to be partitioned by a timestamp field with a first
level partition by year, and a second level by month. Therefore, I did
a BY LIST partitioning, but that produces a wrong constraint check
when executing a query.
This is a reproducible example.
BEGIN;
CREATE TABLE root( pk int generated always as identity, v int, ts
timestamp default current_timestamp )
PARTITION BY LIST( extract( year from ts ) );
CREATE TABLE y2018
PARTITION OF root
FOR VALUES IN ( 2018 );
CREATE TABLE y2019
PARTITION OF root
FOR VALUES IN ( 2019 );
ALTER TABLE y2018 ADD CHECK( ts >= make_timestamp( 2018, 1, 1, 0, 0, 0 )
AND ts <= make_timestamp( 2018, 12, 31, 23, 59, 59 ) );
ALTER TABLE y2019 ADD CHECK( ts >= make_timestamp( 2019, 1, 1, 0, 0, 0 )
AND ts <= make_timestamp( 2019, 12, 31, 23, 59, 59 ) );
INSERT INTO root( v )
SELECT generate_series( 1, 100 ); -- same ts here
COMMIT;
Now if I try to explain a query with the current timestamp (which is
of course in 2019):
testdb=# explain select * from root where ts = current_timestamp;
QUERY PLAN
-------------------------------------------------------------
Append (cost=0.00..75.59 rows=18 width=16)
-> Seq Scan on y2018 (cost=0.00..37.75 rows=9 width=16)
Filter: (ts = CURRENT_TIMESTAMP)
-> Seq Scan on y2019 (cost=0.00..37.75 rows=9 width=16)
Filter: (ts = CURRENT_TIMESTAMP)
(5 rows)
I got y2018 scanned too, which of course could not be the case since
y2018 cannot contain values that are equal to current_timestamp.
However, if I use a literal the query works fine:
testdb=# explain select * from root where ts = '2019-09-01 09:00:00.000000';
QUERY PLAN
---------------------------------------------------------------------------
Append (cost=0.00..33.17 rows=9 width=16)
-> Seq Scan on y2019 (cost=0.00..33.12 rows=9 width=16)
Filter: (ts = '2019-09-01 09:00:00'::timestamp without time zone)
(3 rows)
Now, if I change the partition schema using a range, the query works
fine with current_timestamp too:
CREATE TABLE root( pk int generated always as identity, v int, ts
timestamp default current_timestamp )
PARTITION BY RANGE( ts );
CREATE TABLE y2018
PARTITION OF root
FOR VALUES FROM ('2018-01-01 00:00:00.000000')
TO ('2018-12-31 23:59:59.000000');
CREATE TABLE y2019
PARTITION OF root
FOR VALUES FROM ('2019-01-01 00:00:00.000000')
TO ('2019-12-31 23:59:59.000000');
testdb=# explain select * from root where ts = current_timestamp;
QUERY PLAN
-------------------------------------------------------------
Append (cost=0.00..75.59 rows=18 width=16)
Subplans Removed: 1
-> Seq Scan on y2019 (cost=0.00..37.75 rows=9 width=16)
Filter: (ts = CURRENT_TIMESTAMP)
(4 rows)
So my end with this is that:
- the list partitioning prevents the current_timestamp to be compared
against the list of possible values (extract year from
current_timestamp) and therefore the planner has no chance but to get
into all the tables, even if the constraints on the ts field
explicitly state some tables can be removed;
- in range partitioning, since the partition is built on the very
range of values, the planner gets the correct path.
I still don't get why using a literal in the first case can lead to a
"more correct" plan.
And I'm curious to know if there's a way to force constraints in the
list partitioning to make the planner really aware of tables that can
be excluded.
Luca
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: partition by range or by list constraint check (was Re: literal vs dynamic partition constraint in plan execution)
@ 2019-09-02 15:32 Tom Lane <tgl@sss.pgh.pa.us>
parent: Luca Ferrari <fluca1978@gmail.com>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2019-09-02 15:32 UTC (permalink / raw)
To: Luca Ferrari <fluca1978@gmail.com>; +Cc: pgsql-general
Luca Ferrari <fluca1978@gmail.com> writes:
> I've done a simple test case, and find out that probably the problem I
> got was due to the partition schema I'm using.
> I want a table to be partitioned by a timestamp field with a first
> level partition by year, and a second level by month. Therefore, I did
> a BY LIST partitioning,
> CREATE TABLE root( pk int generated always as identity, v int, ts
> timestamp default current_timestamp )
> PARTITION BY LIST( extract( year from ts ) );
No, that's not going to work, unless your queries all explicitly use
"extract( year from ts ) = something" in their WHERE clauses. There
is nothing in the system that would derive a constraint like that
from a constraint that just mentions ts.
In your example, the partition routing logic was entirely ineffective
because of this, so you tried to make up for that by adding
CHECK constraints. But pruning based on CHECK constraints is done
at planning time, so it can't do anything with run-time-mutable
values such as CURRENT_TIMESTAMP.
> CREATE TABLE root( pk int generated always as identity, v int, ts
> timestamp default current_timestamp )
> PARTITION BY RANGE( ts );
> CREATE TABLE y2018
> PARTITION OF root
> FOR VALUES FROM ('2018-01-01 00:00:00.000000')
> TO ('2018-12-31 23:59:59.000000');
This is mostly the right way to do it; you forgot that range bounds
use "low <= variable < high" logic. So the correct way to declare
the partitions is like
regression=# CREATE TABLE y2018
regression-# PARTITION OF root
regression-# FOR VALUES FROM ('2018-01-01') TO ('2019-01-01');
CREATE TABLE
regression=# CREATE TABLE y2019
PARTITION OF root
FOR VALUES FROM ('2019-01-01') TO ('2020-01-01');
CREATE TABLE
That's easier and doesn't leave a gap between partitions.
This is already sufficient to allow partition routing based on
equality or simple inequality involving "ts", so you don't need
extra CHECK constraints.
> I still don't get why using a literal in the first case can lead to a
> "more correct" plan.
With a literal, plan-time pruning based on the CHECK constraints
was possible.
> And I'm curious to know if there's a way to force constraints in the
> list partitioning to make the planner really aware of tables that can
> be excluded.
No. The short answer here is that your query WHERE clauses have to
be things that the planner or partition routing code can relate to
the partitioning rules. In the case of LIST partitioning, that means
there had better be WHERE constraints on the values specified in
the LIST clause, not values that perhaps could be shown to be related
to those values given extensive knowledge about the behaviors of
certain functions. By and large, the system doesn't have such
knowledge.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2019-09-02 15:32 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-09-02 08:40 partition by range or by list constraint check (was Re: literal vs dynamic partition constraint in plan execution) Luca Ferrari <fluca1978@gmail.com>
2019-09-02 15:32 ` Tom Lane <tgl@sss.pgh.pa.us>
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