X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 4D78A475C98 for ; Tue, 25 Mar 2003 15:10:09 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id 467EF474E44 for ; Tue, 25 Mar 2003 15:10:05 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 9FA48D600; Tue, 25 Mar 2003 12:10:05 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 955E95C04; Tue, 25 Mar 2003 12:10:05 -0800 (PST) Date: Tue, 25 Mar 2003 12:10:05 -0800 (PST) From: Stephan Szabo To: Roy Souther Cc: Subject: Re: Insert race hazard condition. In-Reply-To: <1048622477.2643.44.camel@UtopiaPlanetia.Ept> Message-ID: <20030325120530.B43277-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Status: No, hits=-26.0 required=5.0 tests=BAYES_01,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200303/563 X-Sequence-Number: 12601 On 25 Mar 2003, Roy Souther wrote: > First of all I am a programmer not an DBA expert. > > There seems to be many ways to solve this. My first idea would be if I > could make the INSERT some how return the index+1 that it creates but I > can not find any way to make that work. > > My current idea is to have a short function that does both steps then > returns the index value. This works and I think it should not cause any > race hazard because the SQL execution should be completed before it > returns to the program. I think that it won't be guaranteed to work unless you lock the table. > Is there a better way to do this? How do DBAs with a lot of connections > safely create new records? You might want to look at using a sequence to do your number generation.