Skip site navigation (1) Skip section navigation (2)

Re: SELECT FOR UPDATE and LIMIT 1 behave oddly

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: SELECT FOR UPDATE and LIMIT 1 behave oddly
Date: 2004-10-15 04:22:33
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-bugs
Neil Conway <neilc(at)samurai(dot)com> writes:
> I agree backward compat is a concern, but it seems pretty clear to me
> that this is not the optimal behavior. If there are any people who
> actually need the old behavior, they can nest the FOR UPDATE in a
> FROM-clause subselect:

> SELECT * FROM foo FOR UPDATE LIMIT 5; -- used to lock the whole table

> SELECT * FROM (SELECT * FROM foo FOR UPDATE) x LIMIT 5; -- will always
> do so

Allowing FOR UPDATE in sub-selects opens a can of worms that I do not
think we'll be able to re-can (at least not without the proverbial
larger size of can).  The fundamental question about the above construct
is: exactly which rows did it lock?  And what's your proof that that set
is what it *should* have locked?  What if some of the locked rows didn't
get returned to the client?  Even if LIMIT happens to work in a
convenient way, allowing FOR UPDATE inside a subselect would expose us
to a lot of other cases (joins and aggregates for instance) that I don't
believe we can guarantee pleasant behavior for.

My recollection is that the original FOR UPDATE and LIMIT behaviors were
both implemented at the top level in execMain.c, and at that time LIMIT
effectively executed after FOR UPDATE.  We later pushed LIMIT down to
become a plan node, which was a good idea in every respect except that
it changed the order of application of these two behaviors.  I'm afraid
of the semantic consequences of pushing down FOR UPDATE into a plan node
however.  Maybe it can be made to work but I think a lot of very careful
thought will need to go into it.

			regards, tom lane

In response to


pgsql-bugs by date

Next:From: Tom LaneDate: 2004-10-15 04:47:49
Subject: Re: 'configure' bug on Mac OS X 10.3.5
Previous:From: Tom LaneDate: 2004-10-15 03:48:37
Subject: Re: BUG #1286: indices not used after a pg_restore

Privacy Policy | About PostgreSQL
Copyright © 1996-2018 The PostgreSQL Global Development Group