pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
literal vs dynamic partition constraint in plan execution
4+ messages / 2 participants
[nested] [flat]

* literal vs dynamic partition constraint in plan execution
@ 2019-08-29 14:45 Luca Ferrari <fluca1978@gmail.com>
  2019-08-30 06:29 ` Re: literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Luca Ferrari @ 2019-08-29 14:45 UTC (permalink / raw)
  To: pgsql-general

Ok, the title is a little buzz, however I've got a partitioned table
and one "leaf" has a set of checks against a timestamp field to ensure
that tuples within such table belongs to the year and month:

testdb=# \d respi.y2019m08
...
Partition of: respi.y2019 FOR VALUES IN ('8')
Check constraints:
    "y2019_mis_ora_check" CHECK (date_part('year'::text, mis_ora) =
2019::double precision)
    "y2019_mis_ora_check1" CHECK (mis_ora >= make_timestamp(2019, 1,
1, 0, 0, 0::double precision) AND mis_ora <= make_timestamp(2019, 12,
31, 23, 59, 59::double precision))
    "y2019m08_mis_ora_check" CHECK (date_part('month'::text, mis_ora)
= 8::double precision)
    "y2019m08_mis_ora_check1" CHECK (date_part('year'::text, mis_ora)
= 2019::double precision)
    "y2019m08_mis_ora_check2" CHECK (mis_ora >= make_timestamp(2019,
8, 1, 0, 0, 0::double precision) AND mis_ora <= make_timestamp(2019,
8, 31, 23, 59, 59::double precision))
    "y2019m08_mis_ora_check3" CHECK (mis_ora >= make_timestamp(2019,
8, 1, 0, 0, 0::double precision) AND mis_ora <= make_timestamp(2019,
8, 31, 23, 59, 59::double precision))

So y2019m08 accepts only tuples where 'mis_ora' has a timestamp that
is contained into the eigth month of the year.
Now if I look at the plan for this query everything works as expected
(I disabled parallel scans for better see the plan):

testdb=# explain select * from respi.root where ts >= '2019-08-28
23:35:00.007245' and mis_ora >= '2019-08-29 16:28:48.711482'   order
by ts;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Sort  (cost=353986.27..353991.59 rows=2129 width=40)
   Sort Key: y2019m08.ts
   ->  Append  (cost=0.00..353868.58 rows=2129 width=40)
         ->  Seq Scan on y2019m08  (cost=0.00..353409.93 rows=1 width=40)
               Filter: ((ts >= '2019-08-28 23:35:00.007245'::timestamp
without time zone) AND (mis_ora >= '2019-08-29
16:28:48.711482'::timestamp without time zone))
         ->  Seq Scan on y2019m09  (cost=0.00..28.00 rows=133 width=40)
               Filter: ((ts >= '2019-08-28 23:35:00.007245'::timestamp
without time zone) AND (mis_ora >= '2019-08-29
16:28:48.711482'::timestamp without time zone))
         ->  Seq Scan on y2019m10  (cost=0.00..28.00 rows=133 width=40)
               Filter: ((ts >= '2019-08-28 23:35:00.007245'::timestamp
without time zone) AND (mis_ora >= '2019-08-29
16:28:48.711482'::timestamp without time zone))
         ->  Seq Scan on y2019m11  (cost=0.00..28.00 rows=133 width=40)
               Filter: ((ts >= '2019-08-28 23:35:00.007245'::timestamp
without time zone) AND (mis_ora >= '2019-08-29
16:28:48.711482'::timestamp without time zone))
         ->  Seq Scan on y2019m12  (cost=0.00..28.00 rows=133 width=40)
               Filter: ((ts >= '2019-08-28 23:35:00.007245'::timestamp
without time zone) AND (mis_ora >= '2019-08-29
16:28:48.711482'::timestamp without time zone))


The "as I expected" means that the system starts scanning from
y2019m08 and following (in time) tables, and does not scan previous
time tables.
This works if the mis_ora is compared against a literal timestamp, but
if I simply change it with a dynamic timestamp:

testdb=# explain select * from respi.root where ts >= '2019-08-28
23:35:00.007245' and   mis_ora >= current_timestamp   order by ts;
   QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------
 Sort  (cost=4654860.37..4654865.25 rows=1952 width=36)
   Sort Key: r.ts
   ->  Nested Loop  (cost=0.00..4654753.69 rows=1952 width=36)
         Join Filter: (r.sen_id = s.sen_id)
         ->  Append  (cost=0.00..4638927.56 rows=3204 width=32)
               ->  Seq Scan on y2018m01 r  (cost=0.00..31.00 rows=133 width=32)
                     Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND (mis_ora >=
CURRENT_TIMESTAMP))
               ->  Seq Scan on y2018m02 r_1  (cost=0.00..31.00
rows=133 width=32)
                     Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND (mis_ora >=
CURRENT_TIMESTAMP))
               ->  Seq Scan on y2018m03 r_2  (cost=0.00..31.00
rows=133 width=32)
                     Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND (mis_ora >=
CURRENT_TIMESTAMP))
               ->  Seq Scan on y2018m04 r_3  (cost=0.00..31.00
rows=133 width=32)
                     Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND (mis_ora >=
CURRENT_TIMESTAMP))

also the tables for the past year are scanned. Moreover, the planner
thinks I will get 133 rows out of, for instance, y2018m01 which is
impossible.
So, do I have defined the constraint on each table in a wrong manner?


testdb=# select version();
                                                 version
---------------------------------------------------------------------------------------------------------
 PostgreSQL 11.5 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5
20150623 (Red Hat 4.8.5-36), 64-bit
(1 row)

testdb=# show constraint_exclusion ;
 constraint_exclusion
----------------------
 partition



Thanks,
Luca





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: literal vs dynamic partition constraint in plan execution
  2019-08-29 14:45 literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
@ 2019-08-30 06:29 ` Luca Ferrari <fluca1978@gmail.com>
  2019-08-30 08:02   ` Re: literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Luca Ferrari @ 2019-08-30 06:29 UTC (permalink / raw)
  To: pgsql-general

On Thu, Aug 29, 2019 at 4:45 PM Luca Ferrari <fluca1978@gmail.com> wrote:
>
> Ok, the title is a little buzz, however I've got a partitioned table
> and one "leaf" has a set of checks against a timestamp field to ensure
> that tuples within such table belongs to the year and month:

Of course, all the siblings have similar constraints. So my partition
starts at a table named "root", then it it has a level for the year,
and each year has subpartitions for months:
- root
   - y2018
     - y2018m01, y2018m02, ...
  - y2019
   - y2019m01, y2019m02 ....

All partitions have been created equally, and constraints seem fine to me:

testdb=# \d respi.y2018m01
...
Partition of: respi.y2018 FOR VALUES IN ('1')
Check constraints:
    "y2018_mis_ora_check" CHECK (date_part('year'::text, mis_ora) =
2018::double precision)
    "y2018_mis_ora_check1" CHECK (mis_ora >= make_timestamp(2018, 1,
1, 0, 0, 0::double precision) AND mis_ora <= make_timestamp(2018, 12,
31, 23, 59, 59::double precision))
    "y2018m01_mis_ora_check" CHECK (date_part('year'::text, mis_ora) =
2018::double precision)
    "y2018m01_mis_ora_check1" CHECK (mis_ora >= make_timestamp(2018,
1, 1, 0, 0, 0::double precision) AND mis_ora <= make_timestamp(2018,
1, 31, 23, 59, 59::double precision))



testdb=># \d+ respi.y2018
...
Partition of: respi.root FOR VALUES IN ('2018')
Partition constraint: ((date_part('year'::text, mis_ora) IS NOT NULL)
AND (date_part('year'::text, mis_ora) = '2018'::double precision))
Partition key: LIST (date_part('month'::text, mis_ora))
Check constraints:
    "y2018_mis_ora_check" CHECK (date_part('year'::text, mis_ora) =
2018::double precision)
    "y2018_mis_ora_check1" CHECK (mis_ora >= make_timestamp(2018, 1,
1, 0, 0, 0::double precision) AND mis_ora <= make_timestamp(2018, 12,
31, 23, 59, 59::double precision))
Partitions: respi.y2018m01 FOR VALUES IN ('1'),
            respi.y2018m02 FOR VALUES IN ('2'),
            respi.y2018m03 FOR VALUES IN ('3'),
            respi.y2018m04 FOR VALUES IN ('4'),
            respi.y2018m05 FOR VALUES IN ('5'),
            respi.y2018m06 FOR VALUES IN ('6'),
            respi.y2018m07 FOR VALUES IN ('7'),
            respi.y2018m08 FOR VALUES IN ('8'),
            respi.y2018m09 FOR VALUES IN ('9'),
...

With the above constraint, all the branch starting at y2018 should be
excluded when selecting with
mis_ora >= CURRENT_TIMESTAMP
(the date of the server is right, of course).
Why is instead scanned (as reported by the execution plan in the
previous email)?

Thanks,
Luca





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: literal vs dynamic partition constraint in plan execution
  2019-08-29 14:45 literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
  2019-08-30 06:29 ` Re: literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
@ 2019-08-30 08:02   ` Luca Ferrari <fluca1978@gmail.com>
  2019-09-03 19:00     ` Re: literal vs dynamic partition constraint in plan execution Michael Lewis <mlewis@entrata.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Luca Ferrari @ 2019-08-30 08:02 UTC (permalink / raw)
  To: pgsql-general

On Fri, Aug 30, 2019 at 8:29 AM Luca Ferrari <fluca1978@gmail.com> wrote:
> testdb=># \d+ respi.y2018
> ...
> Partition of: respi.root FOR VALUES IN ('2018')
> Partition constraint: ((date_part('year'::text, mis_ora) IS NOT NULL)
> AND (date_part('year'::text, mis_ora) = '2018'::double precision))
> Partition key: LIST (date_part('month'::text, mis_ora))
> Check constraints:
>     "y2018_mis_ora_check" CHECK (date_part('year'::text, mis_ora) =
> 2018::double precision)
>     "y2018_mis_ora_check1" CHECK (mis_ora >= make_timestamp(2018, 1,
> 1, 0, 0, 0::double precision) AND mis_ora <= make_timestamp(2018, 12,
> 31, 23, 59, 59::double precision))
> Partitions: respi.y2018m01 FOR VALUES IN ('1'),
>             respi.y2018m02 FOR VALUES IN ('2'),
>             respi.y2018m03 FOR VALUES IN ('3'),
>             respi.y2018m04 FOR VALUES IN ('4'),
>             respi.y2018m05 FOR VALUES IN ('5'),
>             respi.y2018m06 FOR VALUES IN ('6'),
>             respi.y2018m07 FOR VALUES IN ('7'),
>             respi.y2018m08 FOR VALUES IN ('8'),
>             respi.y2018m09 FOR VALUES IN ('9'),
> ...
>


While the condition
mis_ora >= current_timestamp
does not cut off the 2018 branch, the following does

=# explain select * from respi.root where ts >= '2019-08-28 23:35:00.007245'
and  extract( year from mis_ora ) = extract( year from current_timestamp )
and extract( month from mis_ora ) >= extract( month from
current_timestamp )    order by ts;

 Sort  (cost=7246692.21..7246692.28 rows=26 width=36)
   Sort Key: r.ts
   ->  Nested Loop  (cost=0.00..7246691.60 rows=26 width=36)
         Join Filter: (r.sen_id = s.sen_id)
         ->  Seq Scan on sensori s  (cost=0.00..13.57 rows=329 width=16)
               Filter: interesting
         ->  Materialize  (cost=0.00..7246465.93 rows=43 width=32)
               ->  Append  (cost=0.00..7246465.72 rows=43 width=32)
                     Subplans Removed: 31
                     ->  Seq Scan on y2019m08 r  (cost=0.00..623008.30
rows=2 width=32)
                           Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND
(date_part('month'::text, mis_ora) >= date_part('month'::text,
CURRENT_TIMESTAMP)) AND (date_part('year'::text, mis_ora) =
date_part('year'::text, CURRENT_TIMESTAMP)))
                     ->  Seq Scan on y2019m09 r_1  (cost=0.00..49.00
rows=1 width=32)
                           Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND
(date_part('month'::text, mis_ora) >= date_part('month'::text,
CURRENT_TIMESTAMP)) AND (date_part('year'::text, mis_ora) =
date_part('year'::text, CURRENT_TIMESTAMP)))
                     ->  Seq Scan on y2019m10 r_2  (cost=0.00..49.00
rows=1 width=32)
                           Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND
(date_part('month'::text, mis_ora) >= date_part('month'::text,
CURRENT_TIMESTAMP)) AND (date_part('year'::text, mis_ora) =
date_part('year'::text, CURRENT_TIMESTAMP)))
                     ->  Seq Scan on y2019m11 r_3  (cost=0.00..49.00
rows=1 width=32)
                           Filter: ((ts >= '2019-08-28
23:35:00.007245'::timestamp without time zone) AND
(date_part('month'::text, mis_ora) >= date_part('month'::text,
CURRENT_TIMESTAMP)) AND (date_part('year'::text, mis_ora) =
date_part('year'::text, CURRENT_TIMESTAMP)))

The fact that making explicit the condition against the year and the
month, which are the top level partition constraint, makes me think
that the executor will try to go down all the branches to the leaf if
the condition is not filtered at the top level. Even if I don't
understand why.

Luca





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: literal vs dynamic partition constraint in plan execution
  2019-08-29 14:45 literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
  2019-08-30 06:29 ` Re: literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
  2019-08-30 08:02   ` Re: literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
@ 2019-09-03 19:00     ` Michael Lewis <mlewis@entrata.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Michael Lewis @ 2019-09-03 19:00 UTC (permalink / raw)
  To: Luca Ferrari <fluca1978@gmail.com>; +Cc: pgsql-general

I think I would expect this behavior with how you have defined the
constraints as the function results rather than just being strict
comparisons to the timestamp field.

Instead of this-
Check constraints:
>     "y2018_mis_ora_check" CHECK (date_part('year'::text, mis_ora) =
> 2018::double precision)

I would expect this-
Check constraints:
>     "y2018_mis_ora_check" CHECK mis_ora) >= make_timestamp(2018, 1,
> 1, 0, 0, 0::double precision) AND < make_timestamp(2019, 1,
> 1, 0, 0, 0::double precision)

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2019-09-03 19:00 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-29 14:45 literal vs dynamic partition constraint in plan execution Luca Ferrari <fluca1978@gmail.com>
2019-08-30 06:29 ` Luca Ferrari <fluca1978@gmail.com>
2019-08-30 08:02   ` Luca Ferrari <fluca1978@gmail.com>
2019-09-03 19:00     ` Michael Lewis <mlewis@entrata.com>

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