> ## Content Index
> Fetch the complete content index at: https://til.codes/llms.txt
> Use this file to discover other available public pages before exploring further.

# Don't forget to update the sequence in PostgreSQL after a COPY command
- URL: https://til.codes/dont-forget-to-update-the-sequence-in-postgresql-after-a-copy-command/
- Published: 2017-11-16T09:31:00.000Z
- Updated: 2017-11-19T19:53:40.000Z
- Author: Manu Ajith
- Tags: postgres, postgresql, database, #Import 2024-07-13 21:01

You might have noticed that after bulk inserting records using the `COPY` statement in PostgreSQL the sequence IDs are not getting updated for any further inserts later on, and it would throw duplicate sequence ID errors.

So you would be wondering what makes this `COPY` statement different that it does not update the sequences.

Well the copy statement is not entirely the culprit here.

###### Internal Details/Reason

When you perform a normal `INSERT`, you often don't specify the value for the SEQUENCE-backed primary key explicitly. But if for some reason you did, you would run in to the same problems as you have with `COPY`. So as you see, `COPY` statement is not the real culprit here.

The sequence only increments when a value is evaluated by the database itself during the INSERT statements( it internally uses the nextval function underneath). If you provide values for your ID, the sequence is not used, thus it doesn't get incremented.

###### Fix

So now you know why it does not increment the sequence, but how do you fix the sequence after a bulk insert?

The answer is simple: you need to call the `nextval` function to reset the sequence generator.

```
select setval('seqname',select max(id) from tablename));

```

so lets say you have a `users` table with `users_id_seq` as the sequence name then:

```
select setval('users_id_seq',select max(id) from users));

```

###### Further reading:

PostgreSQL provides a lot more configuration options for your sequence generator. You can read them over here [Postgres docs](https://www.postgresql.org/docs/9.1/static/sql-createsequence.html?ref=til.codes)

Thanks for reading, and hope it helps someone.!