X-Original-To: pgsql-sql@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 55C18476190 for ; Tue, 25 Mar 2003 15:32:07 -0500 (EST) Received: from ns2.boxsoft.com (dsl092-062-212.lax1.dsl.speakeasy.net [66.92.62.212]) by postgresql.org (Postfix) with ESMTP id 765CC475FC8 for ; Tue, 25 Mar 2003 15:32:05 -0500 (EST) Received: from 3eye.boxsoft.com (3eye.boxsoft.com [66.92.62.215]) by ns2.boxsoft.com (8.12.8/8.12.8) with ESMTP id h2PKW6mG004344; Tue, 25 Mar 2003 12:32:06 -0800 Received: from 3eye.boxsoft.com (localhost [127.0.0.1]) by 3eye.boxsoft.com (8.12.8/8.12.8) with ESMTP id h2PKW4Ca007334; Tue, 25 Mar 2003 12:32:04 -0800 Received: (from patrick@localhost) by 3eye.boxsoft.com (8.12.8/8.12.8/Submit) id h2PKW32A007333; Tue, 25 Mar 2003 12:32:03 -0800 Date: Tue, 25 Mar 2003 12:32:03 -0800 From: patrick To: Roy Souther Cc: pgsql-sql@postgresql.org Subject: Re: Insert race hazard condition. Message-ID: <20030325123203.I255@3eye.boxsoft.com> References: <1048622477.2643.44.camel@UtopiaPlanetia.Ept> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <1048622477.2643.44.camel@UtopiaPlanetia.Ept>; from roy@SiliconTao.com on Tue, Mar 25, 2003 at 01:01:17PM -0700 X-Spam-Status: No, hits=-38.0 required=5.0 tests=BAYES_10,EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REFERENCES,REPLY_WITH_QUOTES,USER_AGENT_MUTT 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/564 X-Sequence-Number: 12602 On Tue, Mar 25, 2003 at 01:01:17PM -0700, Roy Souther wrote: > First of all I am a programmer not an DBA expert. > > For a long time now when I create a table I have an index field that is > of type int4 to be the primary key. All of the queries and activity on > each record uses this field to do the posting, manipulating and reading > of data to and from tables. > > When I add a record to a table I would do it in two steps. > INSERT INTO mytable SELECT MAX(index)+1 FROM mytable > SELECT MAX(index) FROM mytable You probably want to call: Oid oid = PQoidValue( res ); /* assuming C interface */ And use: sprintf( sql, "select index from mytable where oid = %lu", oid ); instead of your second SQL statement. [dd] sidster -- They who would sacrifice freedom for security will have neither. -Ben Franklin