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 1jsNf9-0007O5-Jn for pgsql-general@arkaria.postgresql.org; Mon, 06 Jul 2020 09:43:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jsNf8-0007qp-IH for pgsql-general@arkaria.postgresql.org; Mon, 06 Jul 2020 09:43:46 +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 1jsNf8-0007qh-A9 for pgsql-general@lists.postgresql.org; Mon, 06 Jul 2020 09:43:46 +0000 Received: from sh4-5.1blu.de ([178.254.11.41]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jsNf5-00049m-Mg for pgsql-general@lists.postgresql.org; Mon, 06 Jul 2020 09:43:45 +0000 Received: from ftp51246-2575596 by sh4-5.1blu.de with local (Exim 4.90_1) (envelope-from ) id 1jsNey-0001Nf-UB for pgsql-general@lists.postgresql.org; Mon, 06 Jul 2020 11:43:36 +0200 Date: Mon, 6 Jul 2020 11:43:36 +0200 From: Matthias Apitz To: pgsql-general@lists.postgresql.org Subject: PostgreSQL server does not increment a SERIAL internally Message-ID: <20200706094336.GA318@sh4-5.1blu.de> Reply-To: Matthias Apitz MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Operating-System: FreeBSD 12.0-CURRENT r314251 (amd64) X-message-flag: Mails containing HTML will not be read! Please send only plain text. User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 */ 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; RAISE NOTICE '% % %', 'acq_haushalt', 'hnr', max_id ; EXECUTE 'ALTER SEQUENCE acq_haushalt_hnr_seq RESTART ' || max_id::text; end if; END $$ LANGUAGE plpgsql; 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 -- Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045 Public GnuPG key: http://www.unixarea.de/key.pub May, 9: Спаси́бо освободители! Thank you very much, Russian liberators!