Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1afqW7-0005GA-5W for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Mar 2016 15:04:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1afqW6-0005pO-Oo for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Mar 2016 15:04:14 +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 1afkDT-0001qj-LW for pgsql-bugs@postgresql.org; Tue, 15 Mar 2016 08:20:35 +0000 Received: from mail-wm0-x22a.google.com ([2a00:1450:400c:c09::22a]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1afkDL-0000Qc-T8 for pgsql-bugs@postgresql.org; Tue, 15 Mar 2016 08:20:35 +0000 Received: by mail-wm0-x22a.google.com with SMTP id l68so14532876wml.1 for ; Tue, 15 Mar 2016 01:20:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:mime-version:from:in-reply-to:date:cc:message-id:references :to; bh=kTFry9ivd1NeitllVR5Er08/Kj4qPeHZXmYZ3SKQNiw=; b=SrREYGOBFALKX+f9PbhWj+/c0uiayNGuS4qImzK3ENgWS6nmstgTJW3cq2XNkcIxKC pKfQR9dwq/4qB9ga61JzHa6dOkpEEcttPQZ9Vt21jhErq/bRU6sKrsKqCRAuhSUC3GvI g8q1fko1Le7QTMnPIYwQvx4MzS0cEDyYQeTQTUcWm/5fpQJ7GXUv4xcjIa8B4EfA5pUn nRWG5n3m7uxPVk64sMn9pn177X8mmB82uyrDw4LMCS2xdljxlDbi7sBdhytavf3yBdqO kPnYjPlt4XlmrLN6QepFylsS9+GCQtrfTehUhBDuGH+Q4bVAh1OEA4j3JgoFgigAUJz2 XJaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:mime-version:from:in-reply-to:date:cc :message-id:references:to; bh=kTFry9ivd1NeitllVR5Er08/Kj4qPeHZXmYZ3SKQNiw=; b=YoyBN9w7nnfP9Y2dM3tS8LG5luXBnVn9sq3FfLCZoYayqBJaDzNqvgkNLxre2J8lQG nCrgKothSy+GbCp245Z2XWtbgO7W+ujpvxoowcARTHgJ5h2F4CHgjwW0HS+ZN4RySBjK wuLvDc+dSHpR4E3CBzSl2GCiwxl3vrmp7KxDScc+uh/qMeMwqMdDOAjMwE8esei01hfq 37U9GsWqe5zemorkkgP1AqVUGJlzRLNbFNFX3VAJ0LrCpf2x676d93bQ/2L1bXz27De/ 1rMVCybTjXT4dRkx33Fbt8PxcRZkuN9FkXghVjhUZ0eVDM60rKXQNK9dhJM8TgKKMB6S wjsA== X-Gm-Message-State: AD7BkJLxs9EP8IZ+AH0W81g0gYtEoquTRy7WxseRxnZdy4VPMAR3+t+YCSsCmvoPITfUUw== X-Received: by 10.28.178.208 with SMTP id b199mr21088839wmf.51.1458030026984; Tue, 15 Mar 2016 01:20:26 -0700 (PDT) Received: from [192.168.0.100] ([77.70.1.153]) by smtp.gmail.com with ESMTPSA id w188sm19580067wmw.19.2016.03.15.01.20.25 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 15 Mar 2016 01:20:26 -0700 (PDT) Subject: Re: BUG #14020: row_number() over(partition by order by) - weird behavior Mime-Version: 1.0 (Mac OS X Mail 9.2 \(3112\)) Content-Type: multipart/alternative; boundary="Apple-Mail=_AFA27E34-141E-430F-A6A8-F3FF6DEFBF0E" From: Boyko Yordanov In-Reply-To: Date: Tue, 15 Mar 2016 10:20:25 +0200 Cc: pgsql-bugs@postgresql.org Message-Id: <926E6EDE-8959-4209-B3F0-E894987FD4E0@gmail.com> References: <20160314204348.8902.26168@wrigleys.postgresql.org> To: "David G. Johnston" X-Mailer: Apple Mail (2.3112) X-Pg-Spam-Score: -1.4 (-) 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 --Apple-Mail=_AFA27E34-141E-430F-A6A8-F3FF6DEFBF0E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi and thanks for your time on this. You haven't proven to us that a single row in offers_testing cannot = match more than one row in offers_past_data. Assuming a 1-to-many = situation the update count for offers_past_data can definitely be more = than the number of rows returned by the sub-query. It is a one-to-one relationship between the tables as there is a primary = key on (id, feed) on both tables (which I missed to point out): Indexes: "offers_past_data_id_feed" PRIMARY KEY, btree (id, feed) Indexes: "offers_testing_id_feed" PRIMARY KEY, btree (id, feed) I assume that this guarantees that a single grossprice change in = offers_testing where product =3D 2 translates to up to (count(id,feed) = where product =3D 2) position updates in both offers_testing and = offers_past_data. Adding "returning *" to the questionable query, it seems to update rows = that are not related to product 2 (and on my opinion should not have = changed positions). Also, "ORDER BY grossprice" seems inadequate. The potential for = duplicates here - which would then make the assignment of row numbers = within the product partition random - is non-zero and is a quite likely = source of your problem - along with the probable one-to-many = relationship between offers_testing and offers_past_data. Dismissing the one-to-many relationship suggestion as it isn't the case. Your point on duplicate grossprices is valid, but I believe that if I = update a single grossprice, even in the case of duplicate grossprices, = this should not translate in more position updates than the rows in the = modified product partition. And in offers_testing there are no more than = 148 rows per product partition: db=3D# select max(partition_count) from (select count(*) over (partition = by product) as partition_count from offers_testing) sq; max ----- 148 (1 row) And yet the update query updates 28k records for some reason, most of = which are outside the modified product partition. Boyko -- Boyko 2016-03-15 6:00 GMT+02:00 David G. Johnston >: On Mon, Mar 14, 2016 at 1:43 PM, > wrote: db=3D# update offers_past_data a set position =3D 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 =3D b.id = and a.feed =3D b.feed and a.position <> b.position; UPDATE 0 =E2=80=8BUpdating offers_past_data =E2=80=8B=20 This should update every row in offers_past_data when its =E2=80=9Cpositio= n=E2=80=9D changes. In the example above no changes were introduced since the last = run so nothing is updated (expected). db=3D# select count(*) from offers_testing where product =3D 2; count ------- 99 (1 row) So there are 99 offers for product 2. =E2=80=8BCounting offers_testing=E2=80=8B Getting a single offer: db=3D# select id,grossprice from offers_testing where product =3D 2 = limit 1; id | grossprice ---------+------------ 4127918 | 5000.00 (1 row) =E2=80=8BCounting offers_testing=E2=80=8B Updating its grossprice: db=3D# update offers_testing set grossprice =3D 20 where id =3D 4127918; UPDATE 1 =E2=80=8BUpdating offers_testing=E2=80=8B 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. You haven't proven to us that a single row in offers_testing cannot = match more than one row in offers_past_data. Assuming a 1-to-many = situation the update count for offers_past_data can definitely be more = than the number of rows returned by the sub-query. =E2=80=8B=E2=80=8B db=3D# update offers_past_data a set position =3D 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 =3D b.id = and a.feed =3D 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=3D# update offers_past_data a set position =3D 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 =3D b.id = and a.feed =3D 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? =E2=80=8BIts likely data related, not a bug. Using the "UPDATE ... RETURNING *" form should provide good insight. = Specifically, look for all rows having the same (id, feed) pair. Also, "ORDER BY grossprice" seems inadequate. The potential for = duplicates here - which would then make the assignment of row numbers = within the product partition random - is non-zero and is a quite likely = source of your problem - along with the probable one-to-many = relationship between offers_testing and offers_past_data. David J. =E2=80=8B=20 --Apple-Mail=_AFA27E34-141E-430F-A6A8-F3FF6DEFBF0E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
Hi and thanks for = your time on this.

You haven't proven to = us that a single row in offers_testing cannot match more than one row in = offers_past_data.  Assuming a 1-to-many situation the update count = for offers_past_data can definitely be more than the number of rows = returned by the sub-query.

It is a one-to-one relationship between = the tables as there is a primary key on (id, feed) on both tables (which = I missed to point out):

Indexes:
  =   "offers_past_data_id_feed" PRIMARY KEY, btree (id, = feed)

Indexes:
    = "offers_testing_id_feed" PRIMARY KEY, btree (id, feed)

I assume that this = guarantees that a single grossprice change in offers_testing where = product =3D 2 translates to up to (count(id,feed) where product =3D 2) = position updates in both offers_testing and offers_past_data.

Adding "returning *" to the questionable query, it seems to = update rows that are not related to product 2 (and on my opinion should = not have changed positions).

Also, "ORDER BY grossprice" seems = inadequate.  The potential for duplicates here - which would then = make the assignment of row numbers within the product partition random - = is non-zero and is a quite likely source of your problem - along with = the probable one-to-many relationship between offers_testing and = offers_past_data.

Dismissing= the one-to-many relationship suggestion as it isn't the = case.

Your = point on duplicate grossprices is valid, but I believe that if I update = a single grossprice, even in the case of duplicate grossprices, this = should not translate in more position updates than the rows in the = modified product partition. And in offers_testing there are no more than = 148 rows per product partition:

db=3D# select max(partition_count) = from (select count(*) over (partition by product) as partition_count = from offers_testing) sq;
 max
-----
 148
(1 = row)

And yet the update query updates 28k records for = some reason, most of which are outside the modified product = partition.

Boyko

--

Boyko

2016-03-15 6:00 GMT+02:00 = David G. Johnston <david.g.johnston@gmail.com>:
On Mon, Mar 14, 2016 = at 1:43 PM, <b.yordanov2@gmail.com> wrote:

db=3D# update offers_past_data a set position =3D 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 =3D b.id and a.feed =3D b.feed and
a.position <> b.position;
UPDATE 0

=E2=80=8BU= pdating offers_past_data
=E2=80=8B<= /div> 

This should update every row in offers_past_data when its = =E2=80=9Cposition=E2=80=9D
changes. In the example above no changes were introduced since the last = run
so nothing is updated (expected).

db=3D# select count(*) from offers_testing where product =3D 2;
 count
-------
    99
(1 row)
So there are 99 offers for product 2.

=E2=80=8BCounting = offers_testing=E2=80=8B


Getting a single offer:

db=3D# select id,grossprice from offers_testing where product =3D 2 = limit 1;
   id    | grossprice
---------+------------
 4127918 |    5000.00
(1 row)


=E2=80=8BCounting = offers_testing=E2=80=8B

Updating its grossprice:

db=3D# update offers_testing set grossprice =3D 20 where id =3D = 4127918;
UPDATE 1


=E2=80=8BUpdating = offers_testing=E2=80=8B

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.

You haven't proven to = us that a single row in offers_testing cannot match more than one row in = offers_past_data.  Assuming a 1-to-many situation the update count = for offers_past_data can definitely be more than the number of rows = returned by the sub-query.
=E2=80=8B=E2=80=8B
<= br class=3D"">

db=3D# update offers_past_data a set position =3D 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 =3D b.id and a.feed =3D 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=3D# update offers_past_data a set position =3D 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 =3D b.id and a.feed =3D 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?

=E2=80=8BI= ts likely data related, not a bug.

Using = the "UPDATE ... RETURNING *" form should provide good insight.  = Specifically, look for all rows having the same (id, feed) = pair.

Also, = "ORDER BY grossprice" seems inadequate.  The potential for = duplicates here - which would then make the assignment of row numbers = within the product partition random - is non-zero and is a quite likely = source of your problem - along with the probable one-to-many = relationship between offers_testing and = offers_past_data.

David = J.
=E2=80=8B<= /div> 


= --Apple-Mail=_AFA27E34-141E-430F-A6A8-F3FF6DEFBF0E--