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.92) (envelope-from ) id 1jAg3c-00019m-8N for pgsql-general@arkaria.postgresql.org; Sat, 07 Mar 2020 20:28:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jAg3a-0006br-UE for pgsql-general@arkaria.postgresql.org; Sat, 07 Mar 2020 20:28:22 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1jAg3Z-0006bi-0o for pgsql-general@lists.postgresql.org; Sat, 07 Mar 2020 20:28:22 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jAg3P-00013C-My for pgsql-general@lists.postgresql.org; Sat, 07 Mar 2020 20:28:20 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 08FFB5C8; Sat, 7 Mar 2020 15:28:06 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Sat, 07 Mar 2020 15:28:07 -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=o 2BRnoFgM2Ke2BJfaUv75Znm/qNBkYtUjXrtpv728VM=; b=wEmwIyoDLLSak0HOC e0I9uNmSCdzV9DNpXZXr/OrpqczqbsBuWulH+JboJpzFbCHKmC83qqZ2CljoTTx8 p79QBlpivVb9dPIPjTa9GkMILTmoF3L334OTr3aIjKBH9wE6WM2pQScNMKQ3oh78 XyRZKJ/9UQt7/C+QxaPSPk1JiP2wMc1Mg76ON8RpbRGdtaDzuOOuPDwMstkPmYVS eRfCHhfiOudQJTNFe6pO6tSFLJ4vOHoY1yKZxhiKvepzPW7IZ0zfzg24XBUEok9a f6gvAWohyV30vi9r5kO694xTEt0l2Y9/rDihyAOoyJn5gDAPp9T/xWk6PCbSVUST nJ+Lw== 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=fm2; bh=o2BRnoFgM2Ke2BJfaUv75Znm/qNBkYtUjXrtpv728 VM=; b=fdznZnqCqXMbJsDqJC43J+ZMhKjL/dQ4PMGYB0X5HBl+PDZ9TjPcza4lD X4oNuFNrRCGHcBDuZv2jYs9dEOVHw0SuZqUJPSg/ioDYFIRaEjrO71bGQxYepo69 p6mpE2dXFv4zdHPWZ6eqk4e4T36RG1oqmOA9/Cc5Q91qQqQMCD4z3Vy/X4H8xRqL rrNVOUYShgZxI3W1+FB3Pw13CzU3PmMvVSuLEWERBGStcC1KYLf0BiEJJ7Q3q77M pSZC5qjYMVuchevaMYIYUJzqgu57BnMLF0MGvfFkJX/f2qy2v1kh/tyHaGRLlyro RLzh62+vbbsnFpwb0ID8j7k4Zi9+Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedruddugedgudegudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddv uddvrdduvdelrdduleefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.10] (unknown [71.212.129.193]) by mail.messagingengine.com (Postfix) with ESMTPA id 1DBA430614B1; Sat, 7 Mar 2020 15:28:06 -0500 (EST) Subject: Re: duplicate key value violates unique constraint To: Ashkar Dev , "pgsql-general@lists.postgresql.org" References: From: Adrian Klaver Message-ID: Date: Sat, 7 Mar 2020 12:28:05 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.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 3/7/20 11:29 AM, Ashkar Dev wrote: > Hi all, > > how to fix a problem, suppose there is a table with id and username > > if I set the id to bigint so the limit is 9223372036854775807 > if I insert for example 3 rows > id    username > --    -------------- > 1     abc > 2     def > 3     ghi > > if I delete all rows and insert one another it is like > > id    username > --    -------------- > 4     jkl So I am assuming id is of type bigserial or something that has a sequence behind it? > > > So it doesn't start again from non-available id 1, so what is needed to > do to make the new inserts go into non-available id numbers? If you are sequences then they do not go backwards: https://www.postgresql.org/docs/12/sql-createsequence.html "Because nextval and setval calls are never rolled back, sequence objects cannot be used if “gapless” assignment of sequence numbers is needed. It is possible to build gapless assignment by using exclusive locking of a table containing a counter; but this solution is much more expensive than sequence objects, especially if many transactions need sequence numbers concurrently." If you want that to happen you will have to roll your own implementation. -- Adrian Klaver adrian.klaver@aklaver.com