changing start value of sequence after it was created...

From: "Alan T(dot) Miller" <amiller(at)hollywood101(dot)com>
To: <pgsql-novice(at)postgresql(dot)org>
Subject: changing start value of sequence after it was created...
Date: 2004-01-17 00:48:04
Message-ID: 07a701c3dc93$9149c520$640a0a0a@webdev
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I am converting an application from another database to postgresql. I am
creating my new table using the 'Serial' type for the primary key. When I
use the copy command to import all the old records from the old database,
the sequence never gets incremeneted, even though I am populating the id
field that has a sequence tied to it.

When I go to insert another record trying to take advantage of the sequence
once all the records have been imported, postgresql still thinks the next
value for the sequence is #1, where it would normally start, even though
there may be 5000 records in the table already. The insert will fail because
it is assigning the new record a number that already exists from the import.

I cannot set the sequence using setval(), beforehand because the number of
records I am importing is not always known at the time and I have this
problem across my conversion application with dozens of tables.

To solve my problem, is there a way to ask postgresql for the name of the
sequence of a given table? I was thinking that after my initial import of
data using the copy command, I could use the setval command on the sequence,
to reset the sequence so it will work for inserts of future records.

Thanks in advance,
Alan

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message joseph speigle 2004-01-17 05:37:11 Re: changing start value of sequence after it was created...
Previous Message Josh Berkus 2004-01-16 19:11:14 Re: failover