Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 2D1D32E0032 for ; Mon, 28 Apr 2008 23:22:11 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 38861-07 for ; Mon, 28 Apr 2008 23:21:59 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from wx-out-0506.google.com (wx-out-0506.google.com [66.249.82.228]) by developer.postgresql.org (Postfix) with ESMTP id DE97F2E002C for ; Mon, 28 Apr 2008 23:22:07 -0300 (ADT) Received: by wx-out-0506.google.com with SMTP id r21so4362583wxc.28 for ; Mon, 28 Apr 2008 19:22:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=dAJVY3470xuRsAiL4ONU2AtCAyAhMJY1d/X/oISnRqU=; b=tOKTASzvpmZCGyXGWG30idb5OS3IWhDSZNd1Y0gBGsP7xX5/oJDCceNVabE6PeWo00777Y6qBmNP8/TdhUM66RG9gVGhFjTNKrmgDlVfMkm61FyrML/T/AcmpjutzeecrvoDB2Djy2Mv3e0j/LxSEtfKGjjSwhw7k9MrqmmtFnA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=AXKgmhq6K2dQ7ziePOfnx4eg/fMTRAFuT1fiOHP+eqYM5vMD4XSrfaZimrXQjjXS+aFA1+jH8HMEBpSOZpv6gMpoK2h+nWdsqc5eQ2Lnc+ZTkkhTxiE0YdtyqrnfxeboqagJ+jrzTG+Cep83SIrHE/MSpsGVrNOtkm9Y8QcNm9c= Received: by 10.114.157.1 with SMTP id f1mr7339321wae.10.1209435725549; Mon, 28 Apr 2008 19:22:05 -0700 (PDT) Received: by 10.114.137.7 with HTTP; Mon, 28 Apr 2008 19:22:05 -0700 (PDT) Message-ID: Date: Mon, 28 Apr 2008 20:22:05 -0600 From: "Scott Marlowe" To: "Nacef LABIDI" Subject: Re: currval of sequence "my_seq" is not yet defined in this session Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/219 X-Sequence-Number: 30758 On Mon, Apr 28, 2008 at 4:33 PM, Nacef LABIDI wrote: > Hi all, > > I am writing some code to get the current value of a sequence using the > currval function to use this value in a following insert on another table. > But I am having this message "currval of sequence "my_seq" is not yet > defined in this session". > > Can someone explain me what is the problem with that and by the way explain > me the definition of a session in postgres. > > I have tried to avoid he problem using a stocked function and a trigger but > I really want to avoid using triggers for debugging issues. By your mentioning sessions I assume you've read the bit of the docs on sequence functions, and are somewhat familiar with the three functions, nextval, currval, and setval. Now, mental exercise time. You connect to the database. This is YOUR session. I connect to the same database. This is my session. There is a sequence that will give out 6 as its next value. The last value it gave out (to someone other than either of us) was 5. We both execute " select currval('someseq'); " What should we get back? How would we use it in a safe way if we got it? Can you see the problem here? The current value, if we could get it, would be the same, and neither of us could safely do anything with it. What we need is the NEXT value. you select nextval('someseq') and so do I. Let's suppose you get 6, and I get 7. It could be the other way around, but we don't care. We can both go off and do what we want with our own values. After the nextval, currval for you will return 6, and for me it will return 7.