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 1iN0QD-0001Sa-0w for pgsql-general@arkaria.postgresql.org; Tue, 22 Oct 2019 20:06: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 1iN0QA-0003Yj-OA for pgsql-general@arkaria.postgresql.org; Tue, 22 Oct 2019 20:06: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 1iN0QA-0003Yb-CS for pgsql-general@lists.postgresql.org; Tue, 22 Oct 2019 20:06:22 +0000 Received: from mail-yw1-xc41.google.com ([2607:f8b0:4864:20::c41]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iN0Q7-000068-5w for pgsql-general@lists.postgresql.org; Tue, 22 Oct 2019 20:06:22 +0000 Received: by mail-yw1-xc41.google.com with SMTP id d5so1076687ywk.9 for ; Tue, 22 Oct 2019 13:06:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=X+z141qXY9QfwuqUIm8zEhEax4pdnUDJ/6lYDs+GWs4=; b=eE3Qbz2QW9mRwdhpMI9HbPQQNrpoG7Ua8npVV3uzdyibN6CGzJymhoEY5CsdylZ3/R 6sYpDR3wvvd1PuOd8fYtoZ4nQJfEFfevQnzmW8JfSwqcuDYit68nPsEkc6fJAaTWmife 1YhuuD9HiDMpNhiRyZIF3g15ZNeKPI8vqPmk92zpwYKiElWG8n1MBTxHcvbJ5ZKqyMtp tH0atvzFxBUyQf/TV9M7T1E/OkST7zWNJCn3cZNl744Y5fhThXRICOWinQPEPOArIoDc Z3Qvjzwb+EIpPW+eMkuBHQLJ8NjrtNc3pH20j9bVDATovWWUHgmPsnoPX11F14E0GT85 EiqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=X+z141qXY9QfwuqUIm8zEhEax4pdnUDJ/6lYDs+GWs4=; b=ePfdgELrhrCyfLjnCOZ5FyOPf6wENVqFm0o0+PPhoqUTzSHC2vNk0OliSnAtUcwr6z BopV6UqRtZ9BBkQY8r/7v68VWrFjrKg4GrGAtFm2NZZG9aruFu5JxU6HGWzjBMvVvd87 a35jbv+G8GYofNDIrK+7VoCqDxpv051GdUKUlPyFp3qiW8fmnmgkQ3vTyFUghsXxaT5Q cbdTyzOjb8bkH5tBowVFK1YxlJ3dmjbaVq2DqEjh0oVHgwl6IXd6C6BsvWgPgzyOH6Ju ZsOo930quG6SQgkdyo0+mLmcl/y/sReQl4beKhnqIIqV3L/sfYhn97tOU22GbgVX0bcA Ngug== X-Gm-Message-State: APjAAAWbw3BuPH52r54Us6YgHfPTYchwAoPbJrnkdOa0Fftyp0prcm71 vcr3IuupHPVMUSFvXTv2l3xo7yAbVtjxNA== X-Google-Smtp-Source: APXvYqxXsXZKc1QYqC0m+6hx7fFc0TBnOrl+2TNh1+c9TofUzYhRHCYOeSiYATMtOui3qQOcc7Po3Q== X-Received: by 2002:a81:df0e:: with SMTP id c14mr148107ywn.379.1571774775777; Tue, 22 Oct 2019 13:06:15 -0700 (PDT) Received: from [192.168.1.10] (ip70-186-126-197.no.no.cox.net. [70.186.126.197]) by smtp.googlemail.com with ESMTPSA id z139sm2681790ywz.32.2019.10.22.13.06.14 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Oct 2019 13:06:15 -0700 (PDT) Subject: Re: Primary key definition? To: pgsql-general@lists.postgresql.org References: <20191022194710.GA15509@panix.com> From: Ron Message-ID: Date: Tue, 22 Oct 2019 15:06:14 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20191022194710.GA15509@panix.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/22/19 2: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? When I restored an 8.4 database to 9.6 using pg_dump/pg_restore, all of the sequences were restored using the sequence values at the time the pg_dump ran. -- Angular momentum makes the world go 'round.