pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Primary key definition?
Date: Tue, 22 Oct 2019 15:06:14 -0500
Message-ID: <a48140dc-ff42-e43b-0cc2-dd8125065d9d@gmail.com> (raw)
In-Reply-To: <20191022194710.GA15509@panix.com>
References: <20191022194710.GA15509@panix.com>

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.





view thread (4+ messages)  latest in thread

Message-ID: <a48140dc-ff42-e43b-0cc2-dd8125065d9d@gmail.com>
Permalink:  ../a48140dc-ff42-e43b-0cc2-dd8125065d9d@gmail.com/
Also on:    postgresql.org/message-id/a48140dc-ff42-e43b-0cc2-dd8125065d9d@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com
  Subject: Re: Primary key definition?
  In-Reply-To: <a48140dc-ff42-e43b-0cc2-dd8125065d9d@gmail.com>
  References: <20191022194710.GA15509@panix.com> <a48140dc-ff42-e43b-0cc2-dd8125065d9d@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox