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

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

am 17.08.2004 13:07:43 von twanger

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


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

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

am 17.08.2004 16:12:29 von Bruno

On Tue, Aug 17, 2004 at 13:07:43 +0200,
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.

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.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

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

am 17.08.2004 16:21:32 von twanger

В Втр, 17.08.2004, в 16:12, Bruno Wolff III п=
ишет:
> > SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =3D
> > 'foo' FOR UPDATE OF classes) AS foo
> >=20
> > It's clear which rows should be locked here, I think.
>=20
> Even if it was allowed, it probably wouldn't be good enough because it wo=
n'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 =3D 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 =3D 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
=3D arg_class_name FOR UPDATE OF class_fields;

which didn't work.

Thanks

--=20
Markus Bertheau


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

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

am 17.08.2004 16:26:08 von jllachan

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
>


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

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

am 17.08.2004 16:51:21 von twanger

В Втр, 17.08.2004, в 16:26, Jean-Luc Lachance =D0=
¿Ð¸ÑˆÐµÑ=82:
> 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.

--=20
Markus Bertheau


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

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

am 17.08.2004 17:09:19 von sszabo

On Tue, 17 Aug 2004, Markus Bertheau wrote:

> В Втр, 17.08.2004, в 16:12, Bruno Wolff III =D0=
¿Ð¸ÑˆÐµÑ=82:
> > > SELECT MAX(position) FROM (SELECT position FROM classes WHERE name =
=3D
> > > '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=3D'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=3D'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.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

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

am 17.08.2004 17:54:14 von jllachan

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
>


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

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

am 17.08.2004 19:31:50 von Bruno

On Tue, Aug 17, 2004 at 16:51:21 +0200,
Markus Bertheau 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.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster