Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id B2EB52E344A for ; Tue, 22 Jan 2008 03:36:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 67845-04 for ; Tue, 22 Jan 2008 03:36:37 -0400 (AST) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.183]) by postgresql.org (Postfix) with ESMTP id 378D32E343A for ; Tue, 22 Jan 2008 03:36:40 -0400 (AST) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrelayeu5) with ESMTP (Nemesis) id 0ML25U-1JHDgU1iqV-0004Eb; Tue, 22 Jan 2008 08:36:38 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1JHDgT-0006OB-00 for pgsql-sql@postgresql.org; Tue, 22 Jan 2008 08:36:37 +0100 Date: Tue, 22 Jan 2008 08:36:37 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: currval() within one statement Message-ID: <20080122073637.GB21676@a-kretschmer.de> References: <4795984E.7030904@bankir.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4795984E.7030904@bankir.ru> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1/gW/2wrwfvNgkeetbq/uEvdJ5EsCQkppFGNLI 0t5HF9qHjypbwuuK5x0QomAhjJWipH0Im0zYmmUeJ7FjEDh0Zf PED/38yhzDeTCMTfi0LpPz6c7P3s4MC X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/163 X-Sequence-Number: 30016 am Tue, dem 22.01.2008, um 10:16:30 +0300 mailte silly_sad folgendes: > Helo > > is it expected that the currval() changes its value between calls within > one statement ? > > Look the following call: > > INSERT INTO ttt (a,b) SELECT currval('ttt_id_seq'), 'const' FROM ttt2; This fails if you never call nextval() for this sequence within this session. > > Where the trigger before insert on ttt is defined and this trigger calls > nextval('ttt_id_seq'). You don't need a TRIGGER. Just define your table with (a serial, ...) and omit the column a if you INSERT a new row. > I was surprised having different values of currval() in ttt.a If you call nextval() befor the insert, then returns the currval(), for instance, 5. If you call your insert with the TRIGGER like above, the currval() returns this value 5, but your trigger fires and increase the value. And, maybe, an other process has increased the sequence also. Conclusion, don't call nextval() within a TRIGGER, and insert either nextval() for the column or omit this column. Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net