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 1iN0CV-0000qj-Pk for pgsql-general@arkaria.postgresql.org; Tue, 22 Oct 2019 19:52:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iN0CU-0007R5-JN for pgsql-general@arkaria.postgresql.org; Tue, 22 Oct 2019 19:52:14 +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 1iN0CU-0007Qy-A3 for pgsql-general@lists.postgresql.org; Tue, 22 Oct 2019 19:52:14 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iN0CR-0008QD-HU for pgsql-general@postgresql.org; Tue, 22 Oct 2019 19:52:13 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id DAC6321B2B; Tue, 22 Oct 2019 15:52:09 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 22 Oct 2019 15:52:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=K 13tfFZQ7PL76eyW1XrmOUHIgKJ+QQnn9lne8gmNhIk=; b=PKXkQ7lAClchdfXxo j5VImfCrupI+EuU0Hy3NUzD0TtYJq2KSxEei//nHIaIsmfjnn4AjPW6+L6gpCVw8 cY/mbCz8MGEmhdcC/VAQrU+9uKKSIoBl9bX+PhBswIE8AEN5hk5wuajzv/KiXIbX d0sMjWzJ0GmEtxGV2vvSGA+/38+lJrfh85WJoUzRuJt8vnDMVMe6+2Zn9zAvuk3A vGkNsyMWVohGIxdE7rg/ExQd5AOreC8Ylz5ZX6uBzxhNsxm67gLsioWys3nrFxxi AKJiNgWc3/MictHxrEYckgCO5spyZtHHRI/QvwFJ4WingBsmlv01eKBny83RUByS +UGYA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=K13tfFZQ7PL76eyW1XrmOUHIgKJ+QQnn9lne8gmNh Ik=; b=ubYLCH7fhboH4M5YEY8pLVYX0K2tFzWq8iKlbmSNQGXFrKKYyoJGd8ItX tDaleTDj7gNtKup7hb1Iyf8JcxF9HAA9CvzXJbcM7433r2zNM70HdaFqil/X/1hS 96uNVMm0J0J9tK0/C36Fm+n95iMOmOuJIfKouWmqQ6vGDr4uMMkI7/BXPGl4cJtd LvaIJgS5OBM+YiQgNxAjn2qhIb6V4Ube63A7XA0KzpvToa4ffEnOy9X4Xw7zJZk6 KLFXrKjEwm6+aXHDKME5+CyfqBjAQS6AXvn3KZv3w1G9r3D+V8gUCxY+kkmdiYr+ 1n6ahDbc9M4c3M9pM6FHBl90UfGXQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrkeejgddugedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtje ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvdduvddrudegiedrvd duleenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghk lhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedu X-ME-Proxy: Received: from [192.168.1.3] (unknown [71.212.146.219]) by mail.messagingengine.com (Postfix) with ESMTPA id 02FF18005A; Tue, 22 Oct 2019 15:52:08 -0400 (EDT) Subject: Re: Primary key definition? To: stan , pgsql-general@postgresql.org Cc: crodmail@gmail.com, docrtp@gmail.com References: <20191022194710.GA15509@panix.com> From: Adrian Klaver Message-ID: <6696346a-252e-2aaa-e81f-06e9c35e7fcc@aklaver.com> Date: Tue, 22 Oct 2019 12:52:08 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <20191022194710.GA15509@panix.com> 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 10/22/19 12:47 PM, stan wrote: > I started doing the following to define my primary keys long ago and in a > universe far away: > > CREATE TABLE employee ( > employee_key integer DEFAULT nextval('employee_key_serial') > PRIMARY KEY , > > WEE ran into a scenario, after a total db restore on a project where we got > errors inserting new data because the keys were duplicated. Looking at a > pg_dump, it appears to me that I now understand why. Although the values f > the keys, and the data structures that reference them look like they will > get restored correctly, it appears to me that the sequences get recreated with > an initial value of 1, which means that on the next insert we will get 1 for > a key, which likely is already used. Looks like this is a different way of > defining this: > > > CREATE TABLE books ( > id SERIAL PRIMARY KEY, > > Which has the advantage of not having to manually create the sequences. Will > this also enforce that the "internally created sequence" will be initialized > to a value above the maximum key in use on a pg_restore? See my reply to your off-list post for why this is not correct. My guess is you got the duplicate key errors because the new data you inserted after the restore was using PK values that overlapped the old data. That is probably down to some script not starting at a value > max(PK). > > > -- Adrian Klaver adrian.klaver@aklaver.com