![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Summary: SELECT FOR UPDATE and LIMIT behave oddly when combined |
#3
| |||
| |||
|
|
The FOR UPDATE part executes after the LIMIT part. Arguably this is a bad thing, but I'm concerned about the compatibility issues if we change it. |
#4
| |||
| |||
|
|
The FOR UPDATE part executes after the LIMIT part. Arguably this is a bad thing, but I'm concerned about the compatibility issues if we change it. |
#5
| |||
| |||
|
|
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 |
#6
| |||
| |||
|
|
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? |
|
What if some of the locked rows didn't get returned to the client? |
#7
| |||
| |||
|
|
On Fri, 2004-10-15 at 14:22, Tom Lane wrote: What if some of the locked rows didn't get returned to the client? In the case of SELECT ... FOR UPDATE LIMIT x, exactly the same condition applies: some number of locked rows will not be returned to the client. |
#8
| |||
| |||
|
|
Au contraire: every row that gets locked will be returned to the client. The gripe at hand is that the number of such rows may be smaller than the client wished, because the LIMIT step is applied before we do the FOR UPDATE step |
#9
| |||
| |||
|
|
Au contraire: every row that gets locked will be returned to the client. The gripe at hand is that the number of such rows may be smaller than the client wished, because the LIMIT step is applied before we do the FOR UPDATE step |
#10
| |||
| |||
|
|
I propose that I add this sentence to the Docs: -------------- Please not that, since LIMIT is applied before FOR UPDATE, rows which ^^^ |
|
disappear from the target set while waiting for a lock may result in less than LIMIT # of rows being returned. This can result in unintuitive behavior, so FOR UPDATE and LIMIT should only be combined after significant testing. --------------- |
![]() |
| Thread Tools | |
| Display Modes | |
| |