Re: [PERFORM] DATE_TRUNC() and GROUP BY?

2014-01-02 Thread Dave Johansen
On Fri, Dec 20, 2013 at 10:46 PM, David Rowley wrote: > On Fri, Dec 20, 2013 at 1:35 PM, Dave Johansen wrote: > >> I just ran into an interesting issue on Postgres 8.4. I have a database >> with about 3 months of data and when I do following query: >> SELECT DATE_TRUNC('day', time) AS time_t, COU

Re: [PERFORM] DATE_TRUNC() and GROUP BY?

2014-01-02 Thread Dave Johansen
On Thu, Jan 2, 2014 at 11:39 AM, Dave Johansen wrote: > On Fri, Dec 20, 2013 at 10:46 PM, David Rowley wrote: > >> On Fri, Dec 20, 2013 at 1:35 PM, Dave Johansen wrote: >> >>> I just ran into an interesting issue on Postgres 8.4. I have a database >>> with about 3 months of data and when I do foll

Re: [PERFORM] DATE_TRUNC() and GROUP BY?

2014-01-02 Thread Dave Johansen
On Thu, Jan 2, 2014 at 12:36 PM, Dave Johansen wrote: > On Thu, Jan 2, 2014 at 11:39 AM, Dave Johansen wrote: > >> On Fri, Dec 20, 2013 at 10:46 PM, David Rowley wrote: >> >>> On Fri, Dec 20, 2013 at 1:35 PM, Dave Johansen >>> wrote: >>> I just ran into an interesting issue on Postgres 8.4.

[PERFORM] window function induces full table scan

2014-01-02 Thread Thomas Mayer
When querying a view with a WHERE condition, postgresql normally is able to perform an index scan which reduces time for evaluation dramatically. However, if a window function is evaluated in the view, postgresql is evaluating the window function before the WHERE condition is applied. This ind

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Tom Lane
Thomas Mayer writes: > When querying a view with a WHERE condition, postgresql normally is able > to perform an index scan which reduces time for evaluation dramatically. > However, if a window function is evaluated in the view, postgresql is > evaluating the window function before the WHERE co

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Jeff Janes
On Thu, Jan 2, 2014 at 1:52 PM, Tom Lane wrote: > Thomas Mayer writes: > > When querying a view with a WHERE condition, postgresql normally is able > > to perform an index scan which reduces time for evaluation dramatically. > > > However, if a window function is evaluated in the view, postgresq

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Tom Lane
Jeff Janes writes: > On Thu, Jan 2, 2014 at 1:52 PM, Tom Lane wrote: >> It's possible that in the specific case you exhibit here, pushing down >> the clause wouldn't result in changes in the window function's output for >> the selected rows, but the optimizer doesn't have enough knowledge about >

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Thomas Mayer
You understood me correctly, Tom. As you mention, the result would be correct in my case: - The window function is performing a "PARTITION BY user_id". - user_id is used for the WHERE condition. I agree, that in general (PARTITION BY and WHERE don't use the same set of attributes), incorrect re

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Thomas Mayer
Am 02.01.2014 23:43, schrieb Tom Lane: Jeff Janes writes: On Thu, Jan 2, 2014 at 1:52 PM, Tom Lane wrote: It's possible that in the specific case you exhibit here, pushing down the clause wouldn't result in changes in the window function's output for the selected rows, but the optimizer does

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Tom Lane
I wrote: > If the restriction clause must give the same answer for any two rows of > the same partition, then yeah, we could in principle push it down without > knowing anything about the specific window function. It'd be a less than > trivial test to make, I think. On reflection, really this con

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Thomas Mayer
Just to track it down: The limitation can also be reproduced without using views. Using views is just a use case where the suggested optimization is actually needed. Plus, when I remove the condition "WHERE datepos = 1", the same behaviour still occurs. Here, I wanted to see if postgresql is

Re: [PERFORM] window function induces full table scan

2014-01-02 Thread Thomas Mayer
I have just cloned the postgresql git repository and checked out the REL9_3_2 tagged version to have a look at the src/backend/optimizer/path/allpaths.c file. As Tom already mentioned, quals are currently not pushed down when subqueries with window functions occur: There is a function subque