As part of an effort to create a primary key for a large table in production, I've created a nullable id
column, an auto-incrementing sequence and connected the two:
-- Add nullable column id
ALTER TABLE my_table ADD COLUMN IF NOT EXISTS id BIGINT;
-- Create a sequence
CREATE SEQUENCE my_table_id_seq;
-- Associate the sequence with the table
ALTER SEQUENCE my_table_id_seq OWNED BY my_table.id;
-- Use the sequence for new rows in this table
ALTER TABLE my_table ALTER COLUMN id SET DEFAULT nextval('my_table_id_seq');
After this was executed all new rows indeed getting incrementing values on the id
column. Now I need to back-fill all previously existing rows with values from the sequence.
I've had some concerns:
id
is NULL
I need to use the next value from the sequence as well as increment the index, in order to avoid using the same value twice.id
value?What would be the best way to use the sequence to back fill all rows where id IS NULL
while staying safe vs. concurrent inserts?
You can update the current records, using nextval()
UPDATE my_table
SET id = nextval('my_table_id_seq')
WHERE id IS NULL;
Maintenance of the index is done by the database and you don't have issues with concurrency. Just don't expect any order in your numbers, these are just numbers without any additional meaning.