Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1afflW-0008N6-QW for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Mar 2016 03:35:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1afflV-00058o-QY for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Mar 2016 03:35:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1afZLT-000151-HO for pgsql-bugs@postgresql.org; Mon, 14 Mar 2016 20:44:07 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1afZLQ-0002bg-5L for pgsql-bugs@postgresql.org; Mon, 14 Mar 2016 20:44:07 +0000 MIME-Version: 1.0 Subject: BUG #14020: row_number() over(partition by order by) - weird behavior To: pgsql-bugs@postgresql.org From: b.yordanov2@gmail.com Date: Mon, 14 Mar 2016 20:43:48 +0000 Message-ID: <20160314204348.8902.26168@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.1 (/) Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" 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: 14020 Logged by: Boyko Email address: b.yordanov2@gmail.com PostgreSQL version: 9.5.0 Operating system: CentOS 6.4 Description: Hi, db=# \d+ offers_past_data; Table "public.offers_past_data" Column | Type | Modifiers | Storage | Stats target | Description ----------+---------+-----------+---------+--------------+------------- id | integer | not null | plain | | feed | integer | not null | plain | | position | integer | not null | plain | | db=# \d+ offers_testing; Table "public.offers_testing" Column | Type | Modifiers | Storage | Stats target | Description --------------------+-----------------------------+-----------------------------------------------------+----------+--------------+------------- id | bigint | not null default nextval('offers_id_seq'::regclass) | plain | | grossprice | numeric(11,2) | | main | | feed | integer | not null | plain | | product | integer | | plain | | db=# update offers_past_data a set position = b.position from (select id, feed, row_number() over(partition by product order by grossprice asc) as position from offers_testing) b where a.id = b.id and a.feed = b.feed and a.position <> b.position; UPDATE 0 This should update every row in offers_past_data when its “position” changes. In the example above no changes were introduced since the last run so nothing is updated (expected). db=# select count(*) from offers_testing where product = 2; count ------- 99 (1 row) So there are 99 offers for product 2. Getting a single offer: db=# select id,grossprice from offers_testing where product = 2 limit 1; id | grossprice ---------+------------ 4127918 | 5000.00 (1 row) Updating its grossprice: db=# update offers_testing set grossprice = 20 where id = 4127918; UPDATE 1 Now when executing the first query again I expect that no more than 99 rows get updated in offers_past_data since this is the maximum amount of positions that would be affected by offer 4127918 grossprice change. db=# update offers_past_data a set position = b.position from (select id, feed, row_number() over(partition by product order by grossprice asc) as position from offers_testing) b where a.id = b.id and a.feed = b.feed and a.position <> b.position; UPDATE 104 104 rows get updated. Executing the same query again a few minutes later (no changes meanwhile in either table): db=# update offers_past_data a set position = b.position from (select id, feed, row_number() over(partition by product order by grossprice asc) as position from offers_testing) b where a.id = b.id and a.feed = b.feed and a.position <> b.position; UPDATE 28058 This time it updates 28058 rows. This is a test environment and nothing reads or writes to these tables. Is this a bug or am I missing something obvious? Regards, Boyko -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs