Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VDOtc-0001rd-Qn for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Aug 2013 01:13:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VDOtc-0003dw-5d for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Aug 2013 01:13:36 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VD84r-0001aa-9t for pgsql-bugs@postgresql.org; Sat, 24 Aug 2013 07:16:05 +0000 Received: from mahout.postgresql.org ([2001:4800:7903:4::123]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VD84n-0006hG-U7 for pgsql-bugs@postgresql.org; Sat, 24 Aug 2013 07:16:04 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VD84m-0002bA-8U for pgsql-bugs@postgresql.org; Sat, 24 Aug 2013 07:16:00 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.80) (envelope-from ) id 1VD84g-0000yS-TU for pgsql-bugs@postgresql.org; Sat, 24 Aug 2013 07:15:55 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: BUG #8396: Window function results differ when selecting from table and view, with where clause To: pgsql-bugs@postgresql.org From: paul@weotta.com Message-Id: Date: Sat, 24 Aug 2013 07:15:54 +0000 X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 8396 Logged by: Paul M. Email address: paul@weotta.com PostgreSQL version: 9.3rc1 Operating system: Ubuntu Linux Description: When I select from a view, the where clause in my select statement does not restrict the rows processed by window functions referenced inside the view definition. Thus, if window functions are involved, using a where clause when selecting from a view and using a where clause when selecting directly from the underlying table produces different results. Without wanting to speculate on the cause of the differing results, I will say that this seems to be a case of an issue noted a year ago on Stack Overflow: "Will Postgres push down a WHERE clause into a VIEW with a Window Function (Aggregate)?" http://stackoverflow.com/questions/7533877/ At that time, responder Evan Carroll noted, "I can't think of anyway an un-referenced Window function can change the result if the WHERE was pushed down." This seems to be just such a case. Thanks in advance for looking into this. I've written a test case, which I hope will be helpful... create table plant ( plant_id character varying( 6 ) , city character varying( 25 ) , constraint p_pk_pid primary key ( plant_id ) ); insert into plant ( plant_id, city ) values ( '14 ST', 'San Francisco' ) , ( 'FOLSOM', 'San Francisco' ) , ( 'CHAVEZ', 'San Francisco' ) , ( 'HEINZ', 'Berkeley' ) ; create view plant_extend as select plant_id as plant_id , row_number() over ( partition by city order by plant_id ) as plant_number , count(*) over ( partition by city ) as of_plants_in_city , city as city from plant; -- Despite the where clause, the window functions see all 3 San Francisco plants: select * from plant_extend where plant_id = 'FOLSOM' ; -- But when the query is expressed this way, the window functions see only the Folsom Street plant: select plant_id as plant_id , row_number() over ( partition by city order by plant_id ) as plant_number , count(*) over ( partition by city ) as of_plants_in_city , city as city from plant where plant_id = 'FOLSOM' ; -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs