X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4F14B5E46F4 for ; Tue, 17 Aug 2004 12:52:54 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 36867-07 for ; Tue, 17 Aug 2004 15:52:58 +0000 (GMT) Received: from simmts7-srv.bellnexxia.net (simmts7.bellnexxia.net [206.47.199.165]) by svr1.postgresql.org (Postfix) with ESMTP id 4523D5E46EC for ; Tue, 17 Aug 2004 12:52:49 -0300 (ADT) Received: from localhost ([67.71.24.121]) by simmts7-srv.bellnexxia.net (InterMail vM.5.01.06.10 201-253-122-130-110-20040306) with ESMTP id <20040817155118.CTTV1662.simmts7-srv.bellnexxia.net@localhost>; Tue, 17 Aug 2004 11:51:18 -0400 Received: from sympatico.ca ([192.168.250.6]) by (8.11.6/8.11.6) with ESMTP id i7HFqsg03995; Tue, 17 Aug 2004 11:52:54 -0400 Message-ID: <41222A26.1080204@sympatico.ca> Date: Tue, 17 Aug 2004 11:54:14 -0400 From: Jean-Luc Lachance User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en, en-us, en-gb, fr, fr-ca MIME-Version: 1.0 To: pgsql-sql@postgresql.org Cc: Markus Bertheau Subject: Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo References: <1092740863.2627.17.camel@dicaprio.akademie1.de> <20040817141229.GA15770@wolff.to> <1092752492.2627.23.camel@dicaprio.akademie1.de> In-Reply-To: <1092752492.2627.23.camel@dicaprio.akademie1.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/176 X-Sequence-Number: 18673 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 >