Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is8LL-0006KV-No for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 16:50:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1is8LK-0008Tx-Hq for pgsql-general@arkaria.postgresql.org; Thu, 16 Jan 2020 16:50:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is8LK-0008Pl-42 for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 16:50:02 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1is8LE-0001c4-Qf for pgsql-general@lists.postgresql.org; Thu, 16 Jan 2020 16:50:01 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 1B85322278; Thu, 16 Jan 2020 11:49:56 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 16 Jan 2020 11:49:56 -0500 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=fm2; bh=X z0WQOKxpsyMNV9r3205J/zU6gRIu4/q1a0Yh5VpkeQ=; b=QBwns1WWK4x9mQBR7 NV8vLRoxkLOV+5Jh+7DPoYBeinukTYbN5dt6iJ3a65GiM7NO68xxOiq+3Sf8FQj9 qXzxSpeot5e3tIjVrL6ANcZiBGkKA/WfSYZxzZ62fftK4XaJoi5RBOEOQCK9t8MJ YmmzC8TR9bjOjXYFyHP3yOsnZxfOBxxhay5QrPwBuz0D0UzQcxy0SQdXnR0nj8cu jEy5H/MIboS18O54714zQ8VEMELbjXcmeUHbDzHY/rGnKEraqKYfWxQiAKwYhK4f V8sIrXXigPIgFMGnVYAOiIeTR25P/VqvG0OUZ2ERPSKbD+45sj9+38A5H++499S9 ekitQ== 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=fm1; bh=Xz0WQOKxpsyMNV9r3205J/zU6gRIu4/q1a0Yh5Vpk eQ=; b=fvKpQdnR9x1Wyj2/iyltc89V3YgcytwDhPiDO3UoQDcY2GHnznjPw1fFd po8p1Jlnn1uk7tBCo1JO0DDol07+GxP/Gnt70zhwUeu2WJW+CpudHA+USCWMTGbp tY17eZhLcW+f0MNnSBkOzopCL59mDG71GE3Rd4Rk8+i5YhXDj295rDjyWf14ohCW fvPOJsk5dwHH8wBt8xDfBNuOA783jVljBYqdevgmDzyoov8gcsThIFY18xq+ydSg /cKjnAqhPI2BQb8F3KXylP8ekvUJTsPDSIXiH8fDyvx9Bm+83W2hbRw4TbLXGgMU RZzK8lDkQGkOpRmfBuU2U7hrXNv5w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrtdehgdeludcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghdpthhosghithdrshhofhht figrrhgvnecukfhppeejuddrvdduvddruddvledrudelfeenucfrrghrrghmpehmrghilh hfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhu shhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 58F7680060; Thu, 16 Jan 2020 11:49:55 -0500 (EST) Subject: Re: Re-2: Problem with SqlState=23505 when inserting rows To: Werner Kuhnle , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Thu, 16 Jan 2020 08:49:54 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.1 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 1/16/20 12:14 AM, Werner Kuhnle wrote: > Thanks Michael und Adrian for your answers. > I think that > " > My guess is that the SERIAL was defined for the column, then the data > was added with id values, but the counter for the sequence behind the > SERIAL was not updated to a value greater the the last id added. > " > is a correct description of what happens. > For avoiding the need of an additional command for updating the sequence > before every insert statement: You don't have to update the sequence before every INSERT. For each SERIAL column do this one time: 1) Find current max(id) for the column. 2) Update the the sequence: select setval('seq_name', max_id); From then on the sequence will automatically increment the id with values that are greater then those in the table and you will not get the unique violation errors. The caveat is that if you manually supply an id value then all bets are off. For more information on sequences see: https://www.postgresql.org/docs/12/sql-createsequence.html For SERIAL see: https://www.postgresql.org/docs/12/datatype-numeric.html#DATATYPE-SERIAL If you want more control over what is accepted for the value then you can use an IDENTITY column: https://www.postgresql.org/docs/12/sql-createtable.html GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ] As to below, no. > Is there a way to specify the desired behaviour > (that PG always provides conflict-free id values, eg. max(id)+1 when > id values are not given explicitly in the INSERT statement) > already whend defining(!) the table in the CREATE TABLE statement ? > > Original Message processed by david® > > *Re: Problem with SqlState=23505 when inserting rows*15. Januar > 2020, 17:51 Uhr > *Von* Michael Lewis > *An* Werner Kuhnle > *Cc* PostgreSQL General > > > > > On Wed, Jan 15, 2020 at 9:41 AM Werner Kuhnle > wrote: > > I've tried to using the newer definition: > id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY > but that does not solve the problem. > > Is there a way to define id columns to that when the database > provides values, > it recognizes already existing values avoiding conflicts. > > > You'll need to run something like the below to set the next value to > the max current value. You'll just have to figure out the name of > the sequence that is automatically created whether you use the > pseudo type serial, or the newer IDENTITY option. Both are > implemented with a sequence. > > --set sequence to max ID on a table > select setval( 'table_name_id_seq', ( select max(id) + 1 from > table_name ) ); > -- Adrian Klaver adrian.klaver@aklaver.com