pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedPrimary key definition?
4+ messages / 4 participants
[nested] [flat]
* Primary key definition?
@ 2019-10-22 19:47 stan <stanb@panix.com>
0 siblings, 2 replies; 4+ messages in thread
From: stan @ 2019-10-22 19:47 UTC (permalink / raw)
To: pgsql-general; +Cc: crodmail@gmail.com; docrtp@gmail.com
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?
--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Primary key definition?
@ 2019-10-22 19:52 Adrian Klaver <adrian.klaver@aklaver.com>
parent: stan <stanb@panix.com>
1 sibling, 0 replies; 4+ messages in thread
From: Adrian Klaver @ 2019-10-22 19:52 UTC (permalink / raw)
To: stan <stanb@panix.com>; pgsql-general; +Cc: crodmail@gmail.com; docrtp@gmail.com
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Primary key definition?
@ 2019-10-22 20:06 Ron <ronljohnsonjr@gmail.com>
parent: stan <stanb@panix.com>
1 sibling, 1 reply; 4+ messages in thread
From: Ron @ 2019-10-22 20:06 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Primary key definition?
@ 2019-10-22 21:16 Michael Lewis <mlewis@entrata.com>
parent: Ron <ronljohnsonjr@gmail.com>
0 siblings, 0 replies; 4+ messages in thread
From: Michael Lewis @ 2019-10-22 21:16 UTC (permalink / raw)
To: Ron <ronljohnsonjr@gmail.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
>
> > 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?
>
I think you will still run into the same issue if your sequence is not
getting the proper value as max(id) or max(id) +1, not sure which is
actually needed. You may get some benefits from using IDENTITY rather than
the pseudo-type of serial, as described in this blog post-
https://www.2ndquadrant.com/en/blog/postgresql-10-identity-columns/
Still, depending on how you are doing the data restore, you may need
something like this to ensure the sequence is updated.
select setval( 'table_id_seq', ( select max(id) + 1 from table ) );
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2019-10-22 21:16 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-10-22 19:47 Primary key definition? stan <stanb@panix.com>
2019-10-22 19:52 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-10-22 20:06 ` Ron <ronljohnsonjr@gmail.com>
2019-10-22 21:16 ` Michael Lewis <mlewis@entrata.com>
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