首页 > 解决方案 > 如何在 Postgres 9.4 中忽略没有唯一约束的重复项?

问题描述

我目前在我们的旧数据库(postgres 9.4)表中面临一个问题,其中包含一些重复的行。我想确保不再生成重复的行。

但我也想保留已经生成的重复行。因此,我无法对这些列(多列)应用唯一约束。

我创建了一个触发器,它将检查该行是否已存在并相应地引发异常。但是在处理并发事务时它也会失败。

例子 :

TAB1

col1   |  col2  |  col3  |
------------------------------------
1      |  A     |  B     |   -- 
2      |  A     |  B     |   -- already present duplicates for column col2 and col3(allowed)
3      |  C     |  D     |

INSERT INTO TAB1 VALUES(4 , 'A' , 'B') ; -- This insert statement will not be allowed.

注意:由于数据库版本较旧,我无法在冲突中使用。

标签: sqlpostgresqlduplicatespostgresql-9.4

解决方案


大概,您不希望新行重复历史行。如果是这样,您可以这样做,但它需要修改表并添加新列。

alter table t add duplicate_seq int default 1;

然后更新此列以识别现有重复项:

update t
    set duplicate_seq = seqnum
    from (select t.*, row_number() over (partition by col order by col) as seqnum
          from t
         ) tt
    where t.<primary key> = tt.<primary key>;

现在,创建一个唯一索引或约束:

alter table t add constraint unq_t_col_seq on t(col, duplicate_seq);

插入行时,不要为duplicate_seq. 默认值为1. 这将与任何现有值冲突 - 或与最近输入的重复值冲突。历史重复将被允许。


推荐阅读