pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: b.yordanov2@gmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #14020: row_number() over(partition by order by) - weird behavior
Date: Mon, 14 Mar 2016 20:43:48 +0000
Message-ID: <20160314204348.8902.26168@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (5+ messages)  latest in thread

Message-ID: <20160314204348.8902.26168@wrigleys.postgresql.org>
Permalink:  ../20160314204348.8902.26168@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20160314204348.8902.26168@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: b.yordanov2@gmail.com
  Subject: Re: BUG #14020: row_number() over(partition by order by) - weird behavior
  In-Reply-To: <20160314204348.8902.26168@wrigleys.postgresql.org>
  References: <20160314204348.8902.26168@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox