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

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

From: Jean-Luc Lachance <jllachan(at)sympatico(dot)ca>
To: pgsql-sql(at)postgresql(dot)org
Cc: Markus Bertheau <twanger(at)bluetwanger(dot)de>
Subject: Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo
Date: 2004-08-17 15:54:14
Message-ID: 41222A26.1080204@sympatico.ca (view raw or flat)
Thread:
Lists: pgsql-sql
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
> 


In response to

pgsql-sql by date

Next:From: Adam LancasterDate: 2004-08-17 17:21:22
Subject: Possible rounding error of large float values?
Previous:From: Tom LaneDate: 2004-08-17 15:38:14
Subject: Re: multi column foreign key for implicitly unique columns

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