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 1ejqTP-000199-Ud for pgsql-general@arkaria.postgresql.org; Thu, 08 Feb 2018 17:59:04 +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 1ejqTO-0003Ns-VI for pgsql-general@arkaria.postgresql.org; Thu, 08 Feb 2018 17:59:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ejqTO-0003Nj-O7 for pgsql-general@lists.postgresql.org; Thu, 08 Feb 2018 17:59:02 +0000 Received: from mail-qt0-x242.google.com ([2607:f8b0:400d:c0d::242]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ejqTL-00008p-Mi for pgsql-general@lists.postgresql.org; Thu, 08 Feb 2018 17:59:02 +0000 Received: by mail-qt0-x242.google.com with SMTP id f4so7273690qtj.6 for ; Thu, 08 Feb 2018 09:58:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:in-reply-to:message-id:references:user-agent :mime-version; bh=0iNKYeOwC2NiCeuA3HTNhHRlFBo2/iln02EC1ElbfuQ=; b=FUwCPiVRR4KEMGF4vSYI8t/RsCAXlPqT6XbMO5rJEMEqTKANlZLojzOsaWeQxHFqo/ cohfQFkNTStAgtDeM/GMndcUlSbzeE7KELrLgc9/qeCY4x41/rTC4LoXzZI6FQyCfOHJ t8Pkg1seYEgv7BF1K0TArLlBCnrc0qMZ3zksgxeSd3gKd9dUgDy193jP+PoxvhS46Ki5 UBqy95Q5l4RB6Bncefr8ykNGdD9EDKepM+MWUo6LhwMwiL8teP5nxCV5hvn7bxVZfHPu vbgBj/LYVb5shsGJYBs0CSkpvMSJXIDGvV7w1wkBaZ1DZZmQA5BI7ZhBG3MC5t518QbQ 5a9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=0iNKYeOwC2NiCeuA3HTNhHRlFBo2/iln02EC1ElbfuQ=; b=ShD8z/80241ofrtCT2Yhl/UZs+RbKx66Uf5d5DLYe+LEqHYVSSeNnMEWs+iT5+QISB 1zJeF/e7kR219DkqeZHjAiCRD+GhsCnsXPG+f4o9mve8dH/Y0MAPYaL/sp+CTcxjaaJv YTp3/uuzK9Mue3US9CkiE7gfv/DnxAt+avXK3/sYX2hsdXRtaXGijEJq35IZhuiLJXk7 NW6bu1gdUHnvNP8KQWt9blh+r0osSSEqJiubBw5bhm75Bfd7mq3+KRk+JRquhnRbDSxA /FNjUUMJL6va1uD6sVhu+yUtFtOjiUctBBO2P79Wbj6yB8LuCUY2f8Af8XuSggg76ARw 7yzw== X-Gm-Message-State: APf1xPBIxsL3duD/ISYLwx9fBrC8G4dAKpC+pINs4IYDli68la0WFbvq Cy/4z78VGJeMFF6h2beJoD4spQ== X-Google-Smtp-Source: AH8x226f31WBT+DxFxlsWA17xaAUfNSJGSA0eZr5ezTwyyfmn1lY5FzkuftycVEyeiwuv6OpkzjKlA== X-Received: by 10.200.64.152 with SMTP id p24mr33106qtl.33.1518112737520; Thu, 08 Feb 2018 09:58:57 -0800 (PST) Received: from [192.168.245.27] ([65.183.149.206]) by smtp.gmail.com with ESMTPSA id x2sm356586qkc.62.2018.02.08.09.58.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Feb 2018 09:58:56 -0800 (PST) Date: Thu, 8 Feb 2018 12:58:56 -0500 (EST) From: Steven Hirsch X-X-Sender: hirsch@z87 To: pgsql-general@lists.postgresql.org Subject: Re: Odd behavior with 'currval' In-Reply-To: Message-ID: References: User-Agent: Alpine 2.20 (DEB 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, 8 Feb 2018, Francisco Olarte wrote: > Something must be different. As requested by others, try posting the > SQL code chunks, more eyeballs make bugs shallower ( it's happened > several times to me, make a typo, go over it for half an hour, grab a > colleague, she immediately points to it ) Fair enough. Here is the DDL: CREATE TABLE udm_asset_type_definition ( def_id BIGSERIAL NOT NULL, def_name VARCHAR(32) NOT NULL, PRIMARY KEY (def_id) ); When I look at the column definition, I see: nextval('udm_asset_type_definition_def_id_seq'::regclass) When I look at the catalog, I can see a sequence: udm_asset_type_definition_def_id_seq That appears identical to the column default definition and it has the expected 'last_value'. Here's the odd part: If I issue SELECT pg_get_serial_sequence('udm_asset_type_definition','def_id') I get back NULL (doesn't matter if I qualify with schema - everything is in a schema called 'main' and that is first on the search path). All other sequences in the database (created exactly the same way, through definition as 'BIGSERIAL' type) are properly found. On a hunch, I tried 'SELECT currval(NULL)' to see if it returned '0', but that too returns NULL. So, where is the '0' coming from when I do: SELECT currval( pg_get_serial_sequence('udm_asset_type_definition','def_id')) ? I've already established that the inner expression evaluates to NULL! > It shouldn't be, this I why several perople are requesting to see the > relevant code. Experience says lots of this fails are pilot error. > > As an aside, with recent postgres versions you can normally use the > returning construct to grab autogenerated id. I.e., instead of "insert > blah-blah-blah, select currval(), whatever else" you can many times do > "insert balh-blah-blah returning auto_gen_column, whatever else". I've > used it a lot, and normally leads to shorter/easier/faster code. Yes, I changed the code to 'INSERT .. RETURNING ..' and that works correctly. But, again, not necessary for any of the other tables. This problem is not a transient fluke - I can reproduce it in two different databases on different servers that were created with the same DDL. --