Skip to content
Advertisement

How to ignore duplicates without unique constraint in Postgres 9.4?

I am currently facing an issue in our old database(postgres 9.4) table which contains some duplicate rows. I want to ensure that no more duplicate rows should be generated.

But I also want to keep the duplicate rows that already has been generated. Due to which I could not apply unique constraint on those columns(multiple column).

I have created a trigger which would check the row if already exists and raise exception accordingly. But it is also failing when concurrent transactions are in processing.

Example :

Note: I cannot use on conflict due to older version of database.

Advertisement

Answer

Presumably, you don’t want new rows to duplicate historical rows. If so, you can do this but it requires modifying the table and adding a new column.

Then update this column to identify existing duplicates:

Now, create a unique index or constraint:

When you insert rows, do not provide a value for duplicate_seq. The default is 1. That will conflict with any existing values — or with duplicates entered more recently. Historical duplicates will be allowed.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement