Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jsRRG-0007Ta-Ak for pgsql-general@arkaria.postgresql.org; Mon, 06 Jul 2020 13:45:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jsRRF-0005Ae-98 for pgsql-general@arkaria.postgresql.org; Mon, 06 Jul 2020 13:45:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jsRRD-0005AV-QX for pgsql-general@lists.postgresql.org; Mon, 06 Jul 2020 13:45:41 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jsRR6-0006dk-NB for pgsql-general@lists.postgresql.org; Mon, 06 Jul 2020 13:45:38 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 8BC3C5C01BF; Mon, 6 Jul 2020 09:45:30 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 06 Jul 2020 09:45:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=J 2bzoWZdbqWPjPhpUKZvi6gAg1dGuGTpkIlG4DjjteI=; b=Cs6iBInVr4nLFI0/w bK677QARoWWqFOFVjOrTAnhtcVtXFGeM3baSXnC1y8PLlf+I+iLtgACgilwCPD34 27RhF4czSWxqEv5P7oomcn2iS+WjkYp8zSStckEVTEBIP0grgWc7NwkwBUoStNMB nE0M0KWSozTL3c/4TWsgROZvuTv9zRGx/RUfLFhC0/mbQ6s0sf7pkjKyJezm/RRJ Gz8q2jJly/3Meo1pOfrgZNlZ8mANEqwUnflg9tKCJemw5WF3WZ6Qy3wypvWPVScd QJOir70rd5kzhJxTfUOye51Ybq1/pAIegg/fmRcs7ULOoOqeyxoTKbdklJICz3FJ bYI9g== 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-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=J2bzoWZdbqWPjPhpUKZvi6gAg1dGuGTpkIlG4Djjt eI=; b=b9VUAyEC5WysSkI3BrjwZAjmHvXqqp2qyyRUAiUbwNinSIooDL8phV95M kvnvlcLYCe/aN9yW+6N2uEN58jtA+VmrO9xBd1eZDBWMVzW/dFmsaMzXLqA8zn2W nZLON4gtvneoimo3F59hwQfNLWye/A2T4t3yE1jbMiE2pr1LCfZN9OwD2qvaz55s zheJv2DLTlL5vIs8cIZkVoWDh6o0fz/Gwkrsb8AUnuv+VCrRjr0kxuFfnOw/gPrd bJzr3tDwaAM0TxNLG3NryiKNz/5aMjvMKpJXcAhqf77es+B+xZZqALinYug0pLok 2K7wNnc0NGliVo75k4lrE0JZeGYTA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrudefgdejtdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthejre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeetjedvteffje euvdelieeffeehuedthffgvdfhhffhkedvudevgedvjedufefgveenucfkphepjeehrddu jedvrdehrddvfedunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id DB19130600B9; Mon, 6 Jul 2020 09:45:28 -0400 (EDT) Subject: Re: PostgreSQL server does not increment a SERIAL internally To: Matthias Apitz , pgsql-general@lists.postgresql.org References: <20200706094336.GA318@sh4-5.1blu.de> From: Adrian Klaver Message-ID: <7601a2e4-c0fd-65d0-9f30-c80eca99b59c@aklaver.com> Date: Mon, 6 Jul 2020 06:45:28 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200706094336.GA318@sh4-5.1blu.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/6/20 2:43 AM, Matthias Apitz wrote: > > Hello, > > Me and my team passed a full weekend hunting a bug in our Perl written > software were rows have been inserted with the same id 'acq_haushalt.hnr' > which should not have been the case because any budget year in that > table has a single internal number 'hnr' > > The table in the 11.4 server is created as: > > create table acq_haushalt ( > hnr serial not NULL , /* internal budget year number primary key */ Is this the complete definition, I'm not seeing PRIMARY KEY? > hjahr smallint not NULL , /* budget year */ > stufe smallint not NULL , /* level 0,1,2,3 */ > kurzname char (16) , /* short name for ... */ > ... > ); > > We update the serial 'acq_haushalt_hnr_seq' with this statement after loading: > > /* table: acq_haushalt */ > DO $$ > DECLARE > max_id int; > BEGIN > if to_regclass('acq_haushalt') is not null then > SELECT INTO max_id GREATEST(COALESCE(max(hnr), 0),0) + 1 FROM acq_haushalt; The GREATEST() is redundant, the COALSESCE is going to yield either 0 or a number > 0. > RAISE NOTICE '% % %', 'acq_haushalt', 'hnr', max_id ; > EXECUTE 'ALTER SEQUENCE acq_haushalt_hnr_seq RESTART ' || max_id::text; You don't need to cast max_id. > end if; > END $$ LANGUAGE plpgsql; > So what are you trying to do with the code below, create a new row or something else? > > Usage in Perl DBI to get the next value for acq_haushalt.hnr: > > if ( &getDBDriverName eq 'Pg') { > $erg = &selectData("SELECT NEXTVAL('acq_haushalt_hnr_seq')",[]); > if ($erg->{'CountData'} == 0) { > $newhnr=1; > }else{ > $newhnr=$erg->{'Data'}->[0]->{$erg->{'Fields'}->[0]->{'Name'}}; > } > } else { .... code block for Sybase ... > > } > > But the serial was not incremented internally as we could see with > 'psql' and so more than one row was build and inserted with the same > number in $newhnr. > > What helped was using: > > $erg = &selectData("SELECT max(hnr) FROM acq_haushalt",[]); > if ($erg->{'CountData'} == 0) { > $newhnr=1; > }else{ > $newhnr=$erg->{'Data'}->[0]->{$erg->{'Fields'}->[0]->{'Name'}}+1; > } > > What we are doing wrong? > > Thanks > > matthias > -- Adrian Klaver adrian.klaver@aklaver.com