PostgreSQL Bugs

Collected from the PG bugs email list.

Bug ID15334
PG Version10.2
OSLinux (Centos), Windows
Opened2018-08-16 14:05:17+00
Reported byDamir Ciganović-Janković
StatusNew

Body of first available message related to this bug follows.

The following bug has been logged on the website:

Bug reference:      15334
Logged by:          Damir Ciganović-Janković
Email address:      (redacted)
PostgreSQL version: 10.2
Operating system:   Linux (Centos), Windows
Description:        

Our tables are partitioned by partition key RANGE (resolution, timestamp).

Timestamp is of type 'timestamp without time zone' and resolution is hour
own enum type which contains values 'HOUR' , 'DAY' and 'MONTH':
CREATE TYPE resolution AS ENUM (
  'HOUR',
  'DAY',
  'MONTH'
);

Our table "my_report" has timestamp, resolution and count columns.

This is the query I am executing:
select "my_report"."count" from my_report where ("my_report"."resolution" =
'HOUR'::resolution and "my_report"."timestamp" >= timestamp '2018-08-16
07:00:00' and "my_report"."timestamp" < timestamp '2018-08-16 10:00:00.0')
limit 10; 
(NOTE: I simplified the query so real query and execution plan are
different, but I think you will understand me)

I noticed that when doing this query via psql cmd I get this execution plan

+++++
...
->  Append  (cost=0.00..159.76 rows=1 width=24) (actual time=0.585..0.585
rows=0 loops=1)
    ->  Seq Scan on my_report_hour_20180816  (cost=0.00..159.76 rows=1
width=24) (actual time=0.584..0.584 rows=0 loops=1)
        Filter: (("timestamp" >= '2018-08-16 07:00:00'::timestamp without
time zone) AND ("timestamp" < '2018-08-16 10:00:00'::timestamp without time
zone) AND (resolution = 'HOUR'::resolution))
        Rows Removed by Filter: 4672
...
++++++

This is the correct behaviour, we picked the partition where our timestamps
are in range, and also resolution is HOUR.
Now, when executing this same query via our client (Java), but passing the
resolution as a parameter (and not hardcoding HOUR value in my query like
first time). I would expect that execution plan will be the same, but that
is not the case:

++++++
...
->  Append  (cost=0.00..372.24 rows=3 width=24) (actual time=1.117..1.117
rows=0 loops=1)
    ->  Seq Scan on my_report_hour_20180816  (cost=0.00..183.12 rows=1
width=24) (actual time=0.589..0.589 rows=0 loops=1)
        Filter: (("timestamp" >= '2018-08-16 07:00:00'::timestamp without
time zone) AND ("timestamp" < '2018-08-16 10:00:00'::timestamp without time
zone) AND (resolution = ('HOUR'::cstring)::resolution))
        Rows Removed by Filter: 4672
   ->  Seq Scan on my_report_day_201808  (cost=0.00..94.56 rows=1 width=24)
(actual time=0.265..0.265 rows=0 loops=1)
        Filter: (("timestamp" >= '2018-08-16 07:00:00'::timestamp without
time zone) AND ("timestamp" < '2018-08-16 10:00:00'::timestamp without time
zone) AND (resolution = ('HOUR'::cstring)::resolution))
        Rows Removed by Filter: 2336
    ->  Seq Scan on my_report_month_201808  (cost=0.00..94.56 rows=1
width=24) (actual time=0.261..0.261 rows=0 loops=1)
        Filter: (("timestamp" >= '2018-08-16 07:00:00'::timestamp without
time zone) AND ("timestamp" < '2018-08-16 10:00:00'::timestamp without time
zone) AND (resolution = ('HOUR'::cstring)::resolution))
        Rows Removed by Filter: 2336
...
++++++

As we can see here, our execution will do seq scan on all my_report
partitions for that period (resolutions: day, month and hour) even though we
put HOUR as our query parameter. One thing that I observed is that insead of
(resolution = 'HOUR'::resolution)) like we got in the first example, filters
look like this (resolution = ('HOUR'::cstring)::resolution)).

It seems to me that PostgreSQL is not removing the (::cstring) part before
partition elimination so that it is forced to go through all of them
bassically ignoring the resolution value.

I have a workaround now by putting exact value in the query (I wrote exact
string `"my_report"."resolution" = 'DAY'::resolution`) into my query, and
not passing the value as parameter, but this looks to me as a bug. 

Thanks in advance

Messages

DateAuthorSubject
2018-08-16 14:05:17+00=?utf-8?q?PG_Bug_reporting_form?=BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-17 02:06:47+00Amit LangoteRe: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-17 07:17:16+00=?UTF-8?B?RGFtaXIgQ2lnYW5vdmnEhy1KYW5rb3ZpxIc=?=Re: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-17 08:46:44+00Amit LangoteRe: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-17 10:10:41+00=?UTF-8?B?RGFtaXIgQ2lnYW5vdmnEhy1KYW5rb3ZpxIc=?=Re: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-17 11:21:30+00Andrew GierthRe: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-17 13:18:33+00Amit LangoteRe: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-20 01:00:46+00Amit LangoteRe: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-20 06:06:19+00Andrew GierthRe: BUG #15334: Partition elimination not working as expected when using enum as partition key
2018-08-20 07:38:21+00=?UTF-8?B?RGFtaXIgQ2lnYW5vdmnEhy1KYW5rb3ZpxIc=?=Re: BUG #15334: Partition elimination not working as expected when using enum as partition key