Re: Constraint exclusion failed to prune partition in case of partition expression involves function call

From: Amit Langote <Langote_Amit_f8(at)lab(dot)ntt(dot)co(dot)jp>
To: amul sul <sulamul(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Constraint exclusion failed to prune partition in case of partition expression involves function call
Date: 2017-02-03 01:51:18
Message-ID: 7d0dafb6-8594-3407-2e30-5184af7259c0@lab.ntt.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2017/02/02 21:09, amul sul wrote:
> Hi,
>
> In following case, constraint exclusion not able prune partition (even
> if function is immutable), is this know behaviour?

Yes. The where condition in your example query does not specify the
partition key column, so constraint exclusion won't work, which requires
variable in the condition to be spelled out exactly same as the partition
key column. Here the partitioning code is going to return check
constraints of the form abs(a) = 0 for foo_list1, abs(a) = 1 for foo_list2
and so on, for the constraint exclusion logic to work upon.

> --Explain plan
> postgres=# explain select * from foo_list where a = 2;
> QUERY PLAN
> -----------------------------------------------------------------
> Append (cost=0.00..103.50 rows=25 width=36)
> -> Seq Scan on foo_list (cost=0.00..0.00 rows=1 width=36)
> Filter: (a = 2)
> -> Seq Scan on foo_list1 (cost=0.00..25.88 rows=6 width=36)
> Filter: (a = 2)
> -> Seq Scan on foo_list2 (cost=0.00..25.88 rows=6 width=36)
> Filter: (a = 2)
> -> Seq Scan on foo_list3 (cost=0.00..25.88 rows=6 width=36)
> Filter: (a = 2)
> -> Seq Scan on foo_list4 (cost=0.00..25.88 rows=6 width=36)
> Filter: (a = 2)
> (11 rows)

If you try with where abs(a) = 2, it works:

explain select * from foo_list where abs(a) = 2;
QUERY PLAN
-----------------------------------------------------------------
Append (cost=0.00..29.05 rows=7 width=36)
-> Seq Scan on foo_list (cost=0.00..0.00 rows=1 width=36)
Filter: (abs(a) = 2)
-> Seq Scan on foo_list3 (cost=0.00..29.05 rows=6 width=36)
Filter: (abs(a) = 2)
(5 rows)

See an old exchange at the link below for a kind of similar example and
some explanations about why the thing that one thinks would or should
happen doesn't happen.

https://www.postgresql.org/message-id/CA%2BTgmoaE9NZ_RiqZQLp2aJXPO4E78QxkQYL-FR2zCDop96Ahdg%40mail.gmail.com

Thanks,
Amit

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro HORIGUCHI 2017-02-03 01:56:35 Re: Cannot shutdown subscriber after DROP SUBSCRIPTION
Previous Message Tom Lane 2017-02-03 01:32:23 Re: \if, \elseif, \else, \endif (was Re: PSQL commands: \quit_if, \quit_unless)