From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ray O'Donnell <ray@rodonnell.ie>
To: PostgreSQL <pgsql-general@postgresql.org>
Subject: Re: Resetting identity columns
Date: Mon, 22 Apr 2019 08:54:22 -0700
Message-ID: <617a279e-17c9-c4cd-ec17-1ad8a11498ba@aklaver.com> (raw)
In-Reply-To: <2b2a0f4f-c422-2f5f-1355-e37e446f104e@aklaver.com>
References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie>
<2b2a0f4f-c422-2f5f-1355-e37e446f104e@aklaver.com>
On 4/22/19 8:45 AM, Adrian Klaver wrote:
> On 4/22/19 8:30 AM, Ray O'Donnell wrote:
>> Hi all,
>>
>> I'm probably doing something silly.... I'm migrating data from one
>> database table to another, where the old table used a SERIAL primary
>> key and the new one uses GENERATED BY DEFAULT AS IDENTITY. Having
>> loaded the data into the new table, I need to reset the underlying
>> sequence so that it picks up from the highest existing value.
>>
>> I'm using PostgreSQL 11.2 on Debian 9.
>>
>> I've tried:
>>
>> =# alter table orders alter column order_id restart with (
>> select max(order_id) + 1 from orders);
>
> https://www.postgresql.org/docs/11/sql-altertable.html
>
> "ALTER [ COLUMN ] column_name { SET GENERATED { ALWAYS | BY DEFAULT } |
> SET sequence_option | RESTART [ [ WITH ] restart ] } [...]"
>
> See if the above form will work in your Do block below.
Aargh, time to clean the glasses. You where using the above. Sorry for
the noise.
>
>>
>> ERROR: syntax error at or near "("
>> LINE 1: ...r table orders alter column order_id restart with (select
>> ma...
>>
>>
>> I also tried it with a DO block:
>>
>> =# do language plpgsql $$
>> $# declare m_max_id bigint;
>> $# begin
>> $# select max(order_id) + 1 from orders into m_max_id;
>> $# alter table orders alter column order_id restart with m_max_id;
>> $# end;
>> $# $$;
>>
>> ERROR: syntax error at or near "m_max_id"
>> LINE 5: ...er table orders alter column order_id restart with m_max_id;
>>
>>
>> What am I missing?
>>
>> I should add that this is part of a larger migration script; otherwise
>> I could just do it by hand the command line.
>>
>> Thanks in advance,
>>
>> Ray.
>>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, ray@rodonnell.ie
Subject: Re: Resetting identity columns
In-Reply-To: <617a279e-17c9-c4cd-ec17-1ad8a11498ba@aklaver.com>
References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie> <2b2a0f4f-c422-2f5f-1355-e37e446f104e@aklaver.com> <617a279e-17c9-c4cd-ec17-1ad8a11498ba@aklaver.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