Thread: Odd behaviour of SELECT ... ORDER BY ... FOR UPDATE

Odd behaviour of SELECT ... ORDER BY ... FOR UPDATE

From
Etsuro Fujita
Date:
Hi,

While working on the foreign-join-pushdown issue, I noticed that in READ
COMMITTED isolation level it's possible that the result of SELECT ...
ORDER BY ... FOR UPDATE is not sorted correctly due to concurrent
updates that replaced the sort key columns with new values as shown in
the below example.  That seems odd to me.  So, I'd like to propose
raising an error rather than returning a possibly-incorrect result for
cases where the sorted tuples to be locked were modified by concurrent
updates.  Patch attached.  Is it OK to add this to the current CF?

Create an environment:

postgres=# create table test (a int);
CREATE TABLE
postgres=# insert into test values (1);
INSERT 0 1
postgres=# insert into test values (2);
INSERT 0 1

Run an example:

[Terminal 1]
postgres=# begin;
BEGIN
postgres=# update test set a = 3 where a = 1;
UPDATE 1

[Terminal 2]
postgres=# select * from test order by a for update;

[Terminal 1]
postgres=# commit;
COMMIT

[Terminal 2]
(The following result will be shown after the commit in Terminal 1.
Note that the output ordering is not correct.)
 a
---
 3
 2
(2 rows)

Best regards,
Etsuro Fujita

Attachment

Re: Odd behaviour of SELECT ... ORDER BY ... FOR UPDATE

From
Marko Tiikkaja
Date:
On 7/2/15 9:15 AM, Etsuro Fujita wrote:
> While working on the foreign-join-pushdown issue, I noticed that in READ
> COMMITTED isolation level it's possible that the result of SELECT ...
> ORDER BY ... FOR UPDATE is not sorted correctly due to concurrent
> updates that replaced the sort key columns with new values as shown in
> the below example.  That seems odd to me.  So, I'd like to propose
> raising an error rather than returning a possibly-incorrect result for
> cases where the sorted tuples to be locked were modified by concurrent
> updates.

I don't like the idea of READ COMMITTED suddenly throwing errors due to
concurrency problems.  Using FOR UPDATE correctly is really tricky, and
this is just one example.  And a documented one, at that, too.


.m



Re: Odd behaviour of SELECT ... ORDER BY ... FOR UPDATE

From
Etsuro Fujita
Date:
Hi Marko,

On 2015/07/02 16:27, Marko Tiikkaja wrote:
> On 7/2/15 9:15 AM, Etsuro Fujita wrote:
>> While working on the foreign-join-pushdown issue, I noticed that in READ
>> COMMITTED isolation level it's possible that the result of SELECT ...
>> ORDER BY ... FOR UPDATE is not sorted correctly due to concurrent
>> updates that replaced the sort key columns with new values as shown in
>> the below example.  That seems odd to me.  So, I'd like to propose
>> raising an error rather than returning a possibly-incorrect result for
>> cases where the sorted tuples to be locked were modified by concurrent
>> updates.

> I don't like the idea of READ COMMITTED suddenly throwing errors due to
> concurrency problems.  Using FOR UPDATE correctly is really tricky, and
> this is just one example.  And a documented one, at that, too.

Ah, you are right.  I'll withdraw this.  Sorry for the noise.

Best regards,
Etsuro Fujita



Re: Odd behaviour of SELECT ... ORDER BY ... FOR UPDATE

From
Robert Haas
Date:
On Thu, Jul 2, 2015 at 3:59 AM, Etsuro Fujita
<fujita.etsuro@lab.ntt.co.jp> wrote:
> Hi Marko,
>
> On 2015/07/02 16:27, Marko Tiikkaja wrote:
>> On 7/2/15 9:15 AM, Etsuro Fujita wrote:
>>> While working on the foreign-join-pushdown issue, I noticed that in READ
>>> COMMITTED isolation level it's possible that the result of SELECT ...
>>> ORDER BY ... FOR UPDATE is not sorted correctly due to concurrent
>>> updates that replaced the sort key columns with new values as shown in
>>> the below example.  That seems odd to me.  So, I'd like to propose
>>> raising an error rather than returning a possibly-incorrect result for
>>> cases where the sorted tuples to be locked were modified by concurrent
>>> updates.
>
>> I don't like the idea of READ COMMITTED suddenly throwing errors due to
>> concurrency problems.  Using FOR UPDATE correctly is really tricky, and
>> this is just one example.  And a documented one, at that, too.
>
> Ah, you are right.  I'll withdraw this.  Sorry for the noise.

Does 385f337c9f39b21dca96ca4770552a10a6d5af24 make any difference to
the issue described here?

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company