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 D6D185E37CB for ; Tue, 17 Aug 2004 11:21:45 -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 93279-06 for ; Tue, 17 Aug 2004 14:21:38 +0000 (GMT) Received: from web11.manitu.net (web11.manitu.net [217.11.48.111]) by svr1.postgresql.org (Postfix) with ESMTP id B3A705E46E6 for ; Tue, 17 Aug 2004 11:21:35 -0300 (ADT) Received: from [62.165.4.166] (dicaprio.akademie1.de [62.165.4.166]) (authenticated) by web11.manitu.net (8.10.2-SOL3/8.10.2) with ESMTP id i7HELdg07590; Tue, 17 Aug 2004 16:21:39 +0200 Subject: Re: SELECT MAX(c) FROM (SELECT ... FOR UPDATE) AS foo From: Markus Bertheau To: Bruno Wolff III Cc: pgsql-sql@postgresql.org In-Reply-To: <20040817141229.GA15770@wolff.to> References: <1092740863.2627.17.camel@dicaprio.akademie1.de> <20040817141229.GA15770@wolff.to> Content-Type: text/plain; charset=UTF-8 Message-Id: <1092752492.2627.23.camel@dicaprio.akademie1.de> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Tue, 17 Aug 2004 16:21:32 +0200 Content-Transfer-Encoding: quoted-printable 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/165 X-Sequence-Number: 18662 =D0=92 =D0=92=D1=82=D1=80, 17.08.2004, =D0=B2 16:12, Bruno Wolff III =D0=BF= =D0=B8=D1=88=D0=B5=D1=82: > > 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