Re: Status of issue 4593

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Lee McKeeman" <lmckeeman(at)opushealthcare(dot)com>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: Status of issue 4593
Date: 2009-01-05 20:42:01
Message-ID: 25894.1231188121@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

"Lee McKeeman" <lmckeeman(at)opushealthcare(dot)com> writes:
> Description: order by is not honored after select ... for update

The reason for this behavior is that SELECT FOR UPDATE substitutes the
latest version of the row at the time the row lock is acquired, which is
the very last step after the selection and ordering have been done.
In your example, what the ORDER BY sees is 20/25/30/300, and it sorts on
that basis, and then the 20/1 row is discovered not to be live anymore
so the 40/1 row is locked and substituted.

The only way to avoid this would be to lock before the sort, which could
have the effect of locking more rows than are returned (if you also use
LIMIT); or to repeat the sort operation after locking the rows, which I
doubt anyone is going to want it to do. I suggest sorting on the client
side if you really need this to work in this particular way.

[ thinks for awhile... ] Actually you could make it work entirely on
the server if you were willing to interpose a SQL function, along the
lines of

create function foo () returns setof test as
$$ select * from test order by value for update $$
language sql;

select * from foo() order by value;

which would accomplish the desired result of having two levels of sort.
(You might or might not need the ORDER BY inside the function --- does
your real case use ORDER BY/LIMIT, or does it really lock every row
of the table?)

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Lee McKeeman 2009-01-05 21:25:38 Re: Status of issue 4593
Previous Message Jeff Davis 2009-01-05 20:08:22 Re: Status of issue 4593

Browse pgsql-hackers by date

  From Date Subject
Next Message Simon Riggs 2009-01-05 20:48:20 Re: Time to finalize patches for 8.4 beta
Previous Message Pavel Stehule 2009-01-05 20:35:02 Re: an idea, language SPI