Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ejrRZ-0005G6-IL for pgsql-general@arkaria.postgresql.org; Thu, 08 Feb 2018 19:01:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ejrRY-0003a9-8y for pgsql-general@arkaria.postgresql.org; Thu, 08 Feb 2018 19:01:12 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ejrRX-0003a0-UG for pgsql-general@lists.postgresql.org; Thu, 08 Feb 2018 19:01:12 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ejrRT-0006ib-Vx for pgsql-general@lists.postgresql.org; Thu, 08 Feb 2018 19:01:10 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id C997220D87; Thu, 8 Feb 2018 14:01:06 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Thu, 08 Feb 2018 14:01:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=EJiyRLPDLgtHDdqNzI8flBxt8vPt+ TyCnRb6mes5SFc=; b=RxoKM4VFFtKAOIbNf4B12Azm9GJcia9leDSf0nzDVbKfF 91xdOImobwlZHweGhoeTfE10SiOQnHQfn7UAQ89LVLhRCeR3yNhviYTcYjp9YAIT GdYSmG2k71mPMbqQREL0x2ylDN2TPPsDqGDdSRiLCLloIEtEu/saCc+5j/8SiRxq wfi73fJcEc+1mqrKc157nR7VDXkAaSueP+bau1513BmqKzTFx1VCbsfYn26q2PBh 68e01ZVBu2AVxwRsohLvrpYcxwRAW9FSpqNvXnqHLCGNKABbK9UyTL1rE9R/rwSq uZ0y3rlCJ45YK1ABfX2vmNahpcXkgeNHM1CgOImrw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=EJiyRL PDLgtHDdqNzI8flBxt8vPt+TyCnRb6mes5SFc=; b=nepV0rnGPfuNQO3iJP+DlB PS7Qctlffj4Nvi/a0TUiQdbtSaCGkQVwwqJDHHZHK7wWf2UHlhQu5pNDjryMXdUf yWNKqadzeoqCrr29gI89LVd14mG3wlphOpoUJGpE7fnDc1mYHxALXVPhlc9fbhH9 DTgXY2xn7IxS9DoRSiyd63h9wLpj4KA7s0IjK7Jsxj7N9+bGBVubSilMT1kksuUV FDA+fv4YVfK3EIrtl8UXMEbthCyW/JWsKwbqeYY0GTEu0ISKoxOq2lqUNh1lBLI+ a2o0byuNd1W8f4dYgxS8SDo3OIB121clxcjy9C7lcNl4gnmElaCmzikuoF2/yRBg == X-ME-Sender: Received: from [192.168.1.3] (71-212-36-41.tukw.qwest.net [71.212.36.41]) by mail.messagingengine.com (Postfix) with ESMTPA id 30CC524804; Thu, 8 Feb 2018 14:01:06 -0500 (EST) Subject: Re: Odd behavior with 'currval' To: Steven Hirsch , pgsql-general@lists.postgresql.org References: <31344c43-4f1d-78bd-1a07-4e4e544a1deb@aklaver.com> From: Adrian Klaver Message-ID: Date: Thu, 8 Feb 2018 11:01:05 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/08/2018 10:54 AM, Steven Hirsch wrote: > On Thu, 8 Feb 2018, Adrian Klaver wrote: > >>>> SELECT currval('udm_asset_type_definition_id_seq'); >> >> Arrgh my mistake, the above should have been >> >> SELECT currval('udm_asset_type_definition_def_id_seq'); > > [Code: , SQL State: 55000]  ERROR: currval of sequence > "udm_asset_type_definition_def_id_seq" is not yet defined in this session > > Which is what I'd expect at the psql command line. > > In real-life, I am calling from JDBC in a single session - in fact, from > inside a single transaction. > > I will post the JDBC code as soon as I can cut it down to the pertinent > area. > > Also, since you request it: > > [hirschs@ui24-lin ~]$ psql -d asset_registry -U hirschs > psql (9.6.5) > Type "help" for help. > > asset_registry=# \d udm_asset_type_definition_def_id_seq >       Sequence "main.udm_asset_type_definition_def_id_seq" >     Column     |  Type   |                Value > ---------------+---------+-------------------------------------- >  sequence_name | name    | udm_asset_type_definition_def_id_seq >  last_value    | bigint  | 21 >  start_value   | bigint  | 1 >  increment_by  | bigint  | 1 >  max_value     | bigint  | 9223372036854775807 >  min_value     | bigint  | 1 >  cache_value   | bigint  | 1 >  log_cnt       | bigint  | 32 >  is_cycled     | boolean | f >  is_called     | boolean | t For comparison, from one of my databases: hplc=> \d student_attendance_attendance_id_seq Sequence "public.student_attendance_attendance_id_seq" Column | Type | Value ---------------+---------+-------------------------------------- sequence_name | name | student_attendance_attendance_id_seq last_value | bigint | 39590 start_value | bigint | 1 increment_by | bigint | 1 max_value | bigint | 9223372036854775807 min_value | bigint | 1 cache_value | bigint | 1 log_cnt | bigint | 0 is_cycled | boolean | f is_called | boolean | t Owned by: public.student_attendance.attendance_id > > To me, the greater mystery is why 'pg_get_serial_sequence' is unable to > find that sequence when invoked by table + column. Assuming you showed the complete output I am not seeing the Owned by: for your sequence. I would do the \d on one of your sequences that 'works', I am guessing you will see Owned by: . To correct see: https://www.postgresql.org/docs/10/static/sql-altersequence.html "OWNED BY table_name.column_name OWNED BY NONE The OWNED BY option causes the sequence to be associated with a specific table column, such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well. If specified, this association replaces any previously specified association for the sequence. The specified table must have the same owner and be in the same schema as the sequence. Specifying OWNED BY NONE removes any existing association, making the sequence “free-standing”. " > > -- Adrian Klaver adrian.klaver@aklaver.com