Sub topic 12
Changing, restarting and dropping identity
Assume you know basic ALTER TABLE, primary keys, and transactions. An identity column has two related parts:
- The column stores the ID.
- An attached sequence supplies the next generated value.
You can change how PostgreSQL accepts generated values, change the sequence's next value, or remove the identity behavior. These operations do not rewrite existing IDs.
Changing the generation mode
GENERATED ALWAYS gives PostgreSQL control over the value. GENERATED BY DEFAULT uses the sequence only when the INSERT does not provide an ID.
Change an existing identity column with SET GENERATED:
DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer text NOT NULL
);
INSERT INTO orders (customer) VALUES ('Ada');
ALTER TABLE orders
ALTER COLUMN id SET GENERATED BY DEFAULT;
INSERT INTO orders (id, customer)
VALUES (900, 'Grace');
SELECT * FROM orders ORDER BY id;Changing to BY DEFAULT does not change existing IDs or reset the sequence. It changes which explicit inserts PostgreSQL accepts.
Change back when you want normal inserts to reject accidental IDs:
ALTER TABLE orders
ALTER COLUMN id SET GENERATED ALWAYS;Restarting the sequence
Use RESTART WITH when the next generated value must start at a known number:
ALTER TABLE orders
ALTER COLUMN id RESTART WITH 5000;
INSERT INTO orders (customer)
VALUES ('Lin') RETURNING id;The returned ID is 5000. A later generated row receives 5001, unless another sequence option changes the increment.
A restart does not update existing rows. Therefore, choose a value that cannot conflict with an existing primary key. Restarting at 1 on a table that already contains low IDs can cause a duplicate-key error.
You can also use RESTART without a number. It returns the sequence to the identity definition's original starting value.
| Operation | Changes existing IDs | Changes future automatic IDs | Keeps identity behavior |
|---|---|---|---|
| `SET GENERATED` | No | Only acceptance rules | Yes |
| `RESTART WITH n` | No | Yes | Yes |
| `DROP IDENTITY` | No | Stops generation | No |
Dropping identity
DROP IDENTITY removes the identity property and its automatic generation rule. It does not remove the column, its stored values, or its primary key constraint.
ALTER TABLE orders
ALTER COLUMN id DROP IDENTITY;
-- An ID is now required explicitly.
INSERT INTO orders (id, customer)
VALUES (6000, 'Katherine');After dropping identity, an insert that omits id fails because the primary-key column is still non-nullable and no default generates a value. If you need automatic IDs again, add an identity property with ADD GENERATED, after checking that its sequence will not produce an existing ID.
These schema changes can acquire locks and can conflict with concurrent writes. Apply them during a controlled migration, and check the current maximum ID before restarting or adding generation.
Exercise
A bulk load inserted rows with IDs through 4999. You want the next automatic ID to be 5000. Which statement should you run?
Show answer
Run ALTER TABLE orders ALTER COLUMN id RESTART WITH 5000; then verify that 5000 is not already present.