Re: Partitioning and constraint exclusion

From: Stephen Frost <sfrost(at)snowman(dot)net>
To: Samuel Smith <pgsql(at)net153(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Partitioning and constraint exclusion
Date: 2015-03-04 14:47:07
Message-ID: 20150304144707.GT29780@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Samuel,

* Samuel Smith (pgsql(at)net153(dot)net) wrote:
> I noticed that I could get very nice partition elimination using
> constant values in the where clause.
>
> Ex:
> select * from <table> where <constraint_col> between '2015-01-01'
> and '2015-02-15'
>
> However, I could not get any partition elimination for queries that
> did not have constant values in the where clause.
>
> Ex:
> select * from <table> where <constraint_col> >= (select max(date)
> from <other_table>)

That's correct.

> Unfortunately all of our queries on the analytics team need to be
> dynamic like this and summarize data based around certain recorded
> events and dates from other tables. I saw the note in the docs about
> not being able to use current_timestamp in the where clause but I
> really need to be able to use a sub select or CTE in the where
> clause for the needed dates.

Not sure if this will help, but the planner is smart enough to implement
one-time filters for certain cases. Instead of using inheiritance-based
partitioning, you can use a view like so:

CREATE VIEW v AS
SELECT * FROM table1 WHERE column1 = 5
UNION ALL
SELECT * FROM table2 WHERE column1 = 6
;

Then for cases where we can prove that no results will be returned from
the individual union-all branch, we'll skip it:

SELECT * FROM v WHERE column1 = (select max(column1) from table3);

Unfortunately, we don't appear to support that for an inequality as you
show above. I'm not sure offhand why not but it didn't work in my
testing.

Another approach to dealing with this is to use plpgsql functions and
'return execute' which essentially compute the constant and then build a
dyanmic SQL query using the constant and return the results. It's a bit
awkward compared to just writing the query, but it does work.

Thanks!

Stephen

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Francisco Olarte 2015-03-04 14:47:52 Re: Copy Data between different databases
Previous Message Igor Stassiy 2015-03-04 14:36:56 Postgres not using GiST index in a lateral join