Re: [GENERAL] Adding identity column to a non-empty table - Mailing list pgsql-general

From Igal @ Lucee.org
Subject Re: [GENERAL] Adding identity column to a non-empty table
Date
Msg-id 7435ef5f-f662-15f0-6ebc-b4b1b7cf1156@lucee.org
Whole thread Raw
In response to [GENERAL] Adding identity column to a non-empty table  ("Igal @ Lucee.org" <igal@lucee.org>)
Responses Re: [GENERAL] Adding identity column to a non-empty table  (Melvin Davidson <melvin6925@gmail.com>)
List pgsql-general
On 10/15/2017 4:01 PM, Igal @ Lucee.org wrote:
>
> Hello,
>
> I'm trying to add an identity column to a table that has records 
> (previously had a bigserial column which I removed):
>
>   ALTER TABLE event_log
> ADD COLUMN r_id BIGINT GENERATED BY DEFAULT AS IDENTITY;
>
> But I'm getting an error `column r_id contains null values`.
>
> How can I add the column and populate it for the existing rows?
>

There is probably a better solution, but the one I came up with is to 
add the column as BIGSERIAL and DROP the SEQUENCE CASCADE, SELECT the 
max(rid) + 1, and then convert the column to IDENTITY:
  ALTER TABLE transient.event_log ADD COLUMN r_id BIGSERIAL;
  -- find the sequence name and then  DROP sequence <sequence-name> CASCADE;
  -- find min value by executing select max(r_id) + 1  ALTER table transient.event_log      ALTER COLUMN r_id         
ADDGENERATED BY DEFAULT AS IDENTITY (MINVALUE <min-value>);
 

If anyone has a better suggestion then please let me know.

Thanks,


Igal



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

pgsql-general by date:

Previous
From: "Igal @ Lucee.org"
Date:
Subject: [GENERAL] Adding identity column to a non-empty table
Next
From: Melvin Davidson
Date:
Subject: Re: [GENERAL] Adding identity column to a non-empty table