X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id B61479FA7B6 for ; Thu, 4 May 2006 15:52:49 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 08135-07-2 for ; Thu, 4 May 2006 15:52:28 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mx2.ipf.is (scania.ipf.is [85.197.192.147]) by postgresql.org (Postfix) with ESMTP id 1DB7F9F9C10 for ; Thu, 4 May 2006 15:52:27 -0300 (ADT) Received: (qmail 19167 invoked by uid 1010); 4 May 2006 18:51:40 +0000 Received: from 85.197.216.186 by scania (envelope-from , uid 1008) with qmail-scanner-1.25-st-qms (clamdscan: 0.88/1235. spamassassin: 3.1.1. perlscan: 1.25-st-qms. Clear:RC:1(85.197.216.186):SA:0(-4.2/4.5):. Processed in 0.401558 secs); 04 May 2006 18:51:40 -0000 X-Antivirus-IPF-Mail-From: gnari@hive.is via scania X-Antivirus-IPF: 1.25-st-qms (Clear:RC:1(85.197.216.186):SA:0(-4.2/4.5):. Processed in 0.401558 secs Process 19160) Received: from dsl-216-186.hive.is (HELO ?192.168.1.34?) (85.197.216.186) by mx2.ipf.is with SMTP; 4 May 2006 18:51:40 +0000 Subject: Re: is an explicit lock necessary? From: Ragnar To: Ash Grove Cc: pgsql-sql@postgresql.org In-Reply-To: <20060504181056.14509.qmail@web52510.mail.yahoo.com> References: <20060504181056.14509.qmail@web52510.mail.yahoo.com> Content-Type: text/plain Date: Thu, 04 May 2006 18:59:58 +0000 Message-Id: <1146769198.31874.3.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/54 X-Sequence-Number: 25085 On fim, 2006-05-04 at 11:10 -0700, Ash Grove wrote: > Hi, > > Does beginning a transaction put locks on the tables > queried within the transaction? > > In the example below, is #2 necessary? My thought was > that I would need to use an explicit lock to make sure > that the sequence value I'm selecting in #4 is the > same one that is generated from #3. I'm worried about > another instance of the application doing an insert on > table1 between #3 and #4. > > 1) From my app, I turn off autocommit. > 2) I lock table1 in access exclusive mode > 3) I do an insert into table1 which generates a > primary key via nextval on sequence1 > 4) I grab grab the primary key value via currval on > sequence1 > 5) I do an insert on table2 which includes table1's > primary key so I can join the records later. > 6) I manually commit No. The locking is not necessary. currval is defined to return the value most recently obtained by nextval for this sequence in the current session. see: http://www.postgresql.org/docs/8.1/interactive/functions-sequence.html gnari