Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id C73FD9FA4F0 for ; Thu, 18 Oct 2007 19:51:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 81040-03 for ; Thu, 18 Oct 2007 19:51:12 -0300 (ADT) X-Greylist: delayed 01:50:18.023854 by SQLgrey-1.7.5 Received: from rgminet02.oracle.com (rgminet02.oracle.com [148.87.113.119]) by postgresql.org (Postfix) with ESMTP id BC9F39FA317 for ; Thu, 18 Oct 2007 19:51:10 -0300 (ADT) Received: from rgminet01.oracle.com (rgminet01.oracle.com [148.87.113.118]) by rgminet02.oracle.com (Switch-3.2.4/Switch-3.1.7) with ESMTP id l9IL0wpq002537 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Thu, 18 Oct 2007 15:00:58 -0600 Received: from agmgw1.us.oracle.com (agmgw1.us.oracle.com [152.68.180.212]) by rgminet01.oracle.com (Switch-3.2.4/Switch-3.1.6) with ESMTP id l9IL0mVX019063; Thu, 18 Oct 2007 15:00:49 -0600 Received: from rcsmt250.oracle.com (rcsmt250.oracle.com [148.87.90.195]) by agmgw1.us.oracle.com (Switch-3.2.0/Switch-3.2.0) with ESMTP id l9IG3FfQ014550; Thu, 18 Oct 2007 15:00:44 -0600 Received: from ailitche-pc.ca.oracle.com by acsmt350.oracle.com with ESMTP id 3303745661192741243; Thu, 18 Oct 2007 14:00:43 -0700 Message-ID: <011701c811ca$03cea740$4c349c0a@ca.oracle.com> From: "Andrei Ilitchev" To: "Kris Jurka" Cc: , "Marina Vatkina" References: <001101c81189$2f3f52d0$4c349c0a@ca.oracle.com> <00d601c811b7$47c1a6e0$4c349c0a@ca.oracle.com> Subject: Re: Fw: postgresql experts please help Date: Thu, 18 Oct 2007 17:01:12 -0400 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.3138 X-Mimeole: Produced By Microsoft MimeOLE V6.00.2900.3198 X-Brightmail-Tracker: AAAAAQAAAAI= X-Brightmail-Tracker: AAAAAQAAAAI= X-Brightmail-Tracker: AAAAAQAAAAI= X-Whitelist: TRUE X-Whitelist: TRUE X-Whitelist: TRUE X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200710/113 X-Sequence-Number: 17685 > If you have an after trigger on the table that you're inserting into and > it inserts into another table with a serial column, the lastval call will > use the wrong sequence. Now this is also a problem with currval if you That's a pretty exotic scenario... Looks like lastval is good enough - the same applies to Sybase @@Identity. Using currval actually seems much more dangerous (as my test shows): Obviously there's more than one man_id_seq in our db, how to find the right one to call currval on? I can select from tables (without providing scheme name) - the same should be right for the sequences. That means that the sequence defined in my schema is NOT the one that was created with CREATE TABLE MAN(ID SERIAL... Then where this (created with the table) sequence is? Is it in some kind of system scheme? If so - what would happen if we create the same named tables in two schema - would then sequences override each other? Apparently I had a preexisting sequence named man_id_seq in my scheme -> and that caused postgresql to create a new sequence somewhere else. As soon as I deleted this sequence my test strated working! Thanks a lot for your help, it's greatly appreciated, Andrei IMO this behaviour (if there's existing sequence create another one with the same name but in some other place) is very wrong - be predictible, throw exception. ----- Original Message ----- From: "Kris Jurka" To: "Andrei Ilitchev" Cc: ; "Marina Vatkina" Sent: Thursday, October 18, 2007 3:09 PM Subject: Re: [JDBC] Fw: postgresql experts please help > > > On Thu, 18 Oct 2007, Andrei Ilitchev wrote: > >> In the docs I discovered "select lastval()" which seems to behave exactly >> like @@Identity in Sybase returning the latest nextval that was obtained >> in the current session by no-matter-which sequence. >> That's exactly the functionality I need - much easier to use because no >> need to track the name. >> >> Can you think of any reason why using "select lastval()" would be a bad >> idea? >> > > If you have an after trigger on the table that you're inserting into and > it inserts into another table with a serial column, the lastval call will > use the wrong sequence. Now this is also a problem with currval if you > have a trigger that inserts into the same table, but that's an unlikely > application design. Inserting into another table is something that you'll > find with systems that do auditing or sometimes horizontal partitioning of > a table. > > Kris Jurka >