Re: Different results between PostgreSQL and Oracle for "for update" statement - Mailing list pgsql-hackers

From Andreas Karlsson
Subject Re: Different results between PostgreSQL and Oracle for "for update" statement
Date
Msg-id dd65f7f8-7f67-e52f-a54b-d63c7e091c05@proxel.se
Whole thread Raw
In response to Re: Different results between PostgreSQL and Oracle for "for update" statement  (Andy Fan <zhihui.fan1213@gmail.com>)
Responses Re: Different results between PostgreSQL and Oracle for "for update" statement  (Andy Fan <zhihui.fan1213@gmail.com>)
List pgsql-hackers
On 11/20/20 9:57 AM, Andy Fan wrote:
> Thank you for your attention. Your suggestion would fix the issue.  However
> The difference will cause some risks when users move their application 
> from Oracle
> to PostgreSQL. So I'd like to think which behavior is more reasonable.

I think PostgreSQL's behavior is more reasonable since it only locks the 
rows it claims to lock and no extra rows. This makes the code easy to 
reason about. And PostgreSQL does not re-evaluate sub queries after 
grabbing the lock which while it might be surprising to some people is 
also a quite nice consistent behavior in practice as long as you are 
aware of it.

I do not see why these two scenarios should behave differently (which I 
think they would with your proposed patch):

== Scenario 1

create table su (a int, b int);
insert into su values(1, 1);

- session 1:
begin;
update su set b = 2 where b = 1;

- sess 2:
select * from su where a in (select a from su where b = 1) for update;

- sess 1:
commit;

== Scenario 2

create table su (a int, b int);
insert into su values(1, 1);

create table su2 (a int, b int);
insert into su2 values(1, 1);

- session 1:
begin;
update su set b = 2 where b = 1;
update su2 set b = 2 where b = 1;

- sess 2:
select * from su where a in (select a from su2 where b = 1) for update;

- sess 1:
commit;

Andreas




pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: parsing pg_ident.conf
Next
From: Alvaro Herrera
Date:
Subject: Re: VACUUM (DISABLE_PAGE_SKIPPING on)