Re: Date vs Timestamp without timezone Partition Key - Mailing list pgsql-performance

From Cedric Leong
Subject Re: Date vs Timestamp without timezone Partition Key
Date
Msg-id CAD6i=X3nfFCTPm-GuKgvwKxgUHR7++Kq2d=O2SO7dmXpWWg04Q@mail.gmail.com
Whole thread Raw
In response to Re: Date vs Timestamp without timezone Partition Key  (David Rowley <dgrowleyml@gmail.com>)
List pgsql-performance
I can confirm that was the issue, after removing the expression and using only what was indexed it definitely fixed the query plan. I appreciate all the help you've given me, I didn't really think to look there but it makes a ton of sense that a filter on the database would only work well if it's indexed.

Thanks again,

On Fri, Jun 5, 2020 at 11:13 PM David Rowley <dgrowleyml@gmail.com> wrote:
On Sat, 6 Jun 2020 at 14:49, Cedric Leong <cedricleong@gmail.com> wrote:
> It's less of a complaint rather than just a warning not to do what I did.

My point was really that nobody really knew what you did or what you
did it on. So it didn't seem like a worthwhile warning as it
completely lacked detail.

> These tests are running the exact same query on two different tables with the exception that they use their respective partition keys.

Are you sure?  It looks like the old one does WHERE date =
((now())::date - '7 days'::interval) and the new version does
(date(created_at) = ((now())::date - '7 days'::interval). I guess you
renamed date to "created_at" and changed the query to use date(). If
that expression is not indexed then I imagine that would be a good
reason for the planner to have moved away from using the index on that
column. Also having date(created_at) will also not allow run-time
pruning to work since your partition key is "created_at".

You might be able to change the query to query a range of value on the
new timestamp column. This will allow you to get rid of the date()
function. For example:

where created_at >= date_trunc('day', now() - '7 days'::interval) and
created_at < date_trunc('day', now() - '6 days'::interval)

David

pgsql-performance by date:

Previous
From: David Rowley
Date:
Subject: Re: Date vs Timestamp without timezone Partition Key
Next
From: MichaelDBA
Date:
Subject: Re: When to use PARTITION BY HASH?