Re: Migration problem - serial fields

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Rick Anderson <rick(at)planetdigital(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Migration problem - serial fields
Date: 2002-03-07 15:38:20
Message-ID: 20020307073449.E76547-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 4 Mar 2002, Rick Anderson wrote:

> The snag I'm encountering is with Identity fields in SQL7. I discovered the
> equivalent in PG -- sequences/serial field. I would like to use serial if
> possible, but here's the problem: getting the existing records over while
> *keeping the existing values* for the Identity/sequence ID fields. They
> actually transfer over fine, but the next insert into the PG table
> generates a duplicate ID error. It seems obvious that my transfer did not
> update the sequence used by the serial field. However, I'm just not sure of
> the best course of action.

Yes, the sequence is only updated if a value is not given (it's
effectively just a default so nextval doesn't get called otherwise)

> I've thought about the following as solutions:
>
> 1. Do the transfer, put values into the serial field, then find the highest
> value and manually set the sequence somehow to start from highest+1 (seed
> value). I don't know if this means moving away from serial field to a

This is probably the easiest. setval('<seq name>', highest value) should
be okay. IIRC this is what pg_dump does.

> "nextval of sequence" approach. I understand there is a problem with orphan
> sequences with serial fields if you drop tables, so maybe I shouldn't use
> serials anyway.

It's just a thing to remember when you drop the table. You run into
similar problems with standalone sequences anyway.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-03-07 15:46:57 Re: Defined C function gives nondeterministic results
Previous Message Tom Lane 2002-03-07 15:32:34 Re: "select myfunc(fields) from my table" inside plpgslq proc