Thread: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Markus Bertheau
Date:
Hi,

why is the following query not allowed:

SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =
'foo' FOR UPDATE OF classes) AS foo

It's clear which rows should be locked here, I think.

Thanks

-- 
Markus Bertheau <twanger@bluetwanger.de>



Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Bruno Wolff III
Date:
On Tue, Aug 17, 2004 at 13:07:43 +0200, Markus Bertheau <twanger@bluetwanger.de> wrote:
> Hi,
> 
> why is the following query not allowed:
> 
> SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =
> 'foo' FOR UPDATE OF classes) AS foo
> 
> It's clear which rows should be locked here, I think.

Even if it was allowed, it probably wouldn't be good enough because it won't
protect against newly inserted records. You really want to lock the table
against concurrent updates when doing this if concurrent updates can cause
a problem. You really want predicate locking, to lock any old or new rows
with name = 'foo', but postgres doesn't have that capability.


Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Markus Bertheau
Date:
В Втр, 17.08.2004, в 16:12, Bruno Wolff III пишет:
> > SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =
> > 'foo' FOR UPDATE OF classes) AS foo
> >
> > It's clear which rows should be locked here, I think.
>
> Even if it was allowed, it probably wouldn't be good enough because it won't
> protect against newly inserted records.

Can you detail an example where this wouldn't be good enough?

In a PL/pgSQL function I'm doing

PERFORM position FROM class_fields WHERE class = arg_class_name;
INSERT INTO class_fields (class, field, position) VALUES
(arg_class_name, arg_field_name, (SELECT MAX(position) FROM class_fields
WHERE class = arg_class_name));

Is this unsafe?

The question initially arose because I wanted to do something similar to

SELECT INTO var_new_position MAX(position) FROM class_fields WHERE class
= arg_class_name FOR UPDATE OF class_fields;

which didn't work.

Thanks

--
Markus Bertheau <twanger@bluetwanger.de>



Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Jean-Luc Lachance
Date:
This query does not make sense to me.
Why would you create an updatable subquery just to get the highest value?

Maybe you are trying to achieve something other than what the query 
suggest. You wou care to put in words what you want to do?


JLL


Markus Bertheau wrote:

> Hi,
> 
> why is the following query not allowed:
> 
> SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =
> 'foo' FOR UPDATE OF classes) AS foo
> 
> It's clear which rows should be locked here, I think.
> 
> Thanks
> 



Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Markus Bertheau
Date:
В Втр, 17.08.2004, в 16:26, Jean-Luc Lachance пишет:
> This query does not make sense to me.
> Why would you create an updatable subquery just to get the highest value?

To make sure that the highest value will be the highest value (or then
second-highest) after I commit the transaction. See my other answer.

--
Markus Bertheau <twanger@bluetwanger.de>



Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Stephan Szabo
Date:
On Tue, 17 Aug 2004, Markus Bertheau wrote:

> В Втр, 17.08.2004, в 16:12, Bruno Wolff III пишет:
> > > SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =
> > > 'foo' FOR UPDATE OF classes) AS foo
> > >
> > > It's clear which rows should be locked here, I think.
> >
> > Even if it was allowed, it probably wouldn't be good enough because it won't
> > protect against newly inserted records.
>
> Can you detail an example where this wouldn't be good enough?

Another transaction can come along and insert a row with name='foo' into
classes with a higher position value after you've done the above but
before you commit.

T1: begin;
T2: begin;
T1: select max(position) from (select position from classes where
name='foo' for update of classes) as foo;-- say this gets 5
T2: insert into classes (name, position) values ('foo', 10);-- This wouldn't be blocked by the for update lock.
T2: commit;-- now if you were to do the T1 select above, you'd get a different-- answer in read committed.

If we had predicate locking, I think you could probably manage these cases
in serializable mode, but for now I'm not sure anything less than a table
lock would do.


Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Jean-Luc Lachance
Date:
If your intent is to insert a new record with position incremented by 1, 
you should use a trigger. Look at the autoincrement thread from few days 
ago.



Markus Bertheau wrote:

> В Втр, 17.08.2004, в 16:12, Bruno Wolff III пишет:
> 
>>>SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =
>>>'foo' FOR UPDATE OF classes) AS foo
>>>
>>>It's clear which rows should be locked here, I think.
>>
>>Even if it was allowed, it probably wouldn't be good enough because it won't
>>protect against newly inserted records.
> 
> 
> Can you detail an example where this wouldn't be good enough?
> 
> In a PL/pgSQL function I'm doing
> 
> PERFORM position FROM class_fields WHERE class = arg_class_name;
> INSERT INTO class_fields (class, field, position) VALUES
> (arg_class_name, arg_field_name, (SELECT MAX(position) FROM class_fields
> WHERE class = arg_class_name));
> 
> Is this unsafe?
> 
> The question initially arose because I wanted to do something similar to
> 
> SELECT INTO var_new_position MAX(position) FROM class_fields WHERE class
> = arg_class_name FOR UPDATE OF class_fields;
> 
> which didn't work.
> 
> Thanks
> 



Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo

From
Bruno Wolff III
Date:
On Tue, Aug 17, 2004 at 16:51:21 +0200, Markus Bertheau <twanger@bluetwanger.de> wrote:
> ?? ??????, 17.08.2004, ?? 16:26, Jean-Luc Lachance ??????????:
> > This query does not make sense to me.
> > Why would you create an updatable subquery just to get the highest value?
> 
> To make sure that the highest value will be the highest value (or then
> second-highest) after I commit the transaction. See my other answer.

If you just need ordering that isn't too sensitive to overlapping transactions,
then using a sequence (using a serial type is a convenient way to do this)
is probably your best bet. You need to make sure the value won't roll over.
But at worst a bigserial should be needed to do this.
There will potentially be gaps in the numbers allocated accross the table.
There will be gaps in the numbers allocated for any value of name. However,
if all you need is ordering that shouldn't matter.