下面例子印证了这种操作方式,得出的结论是,PG中的唯一索引是可以增加where条件过滤的:
postgres=# create table t1 (id int);
CREATE TABLE
postgres=# create unique index idx_id_unq on t1(id) where id >10;
CREATE INDEX
postgres=# \d+ t1
id | integer | | plain | |
postgres=# insert into t1 values(13)
postgres-# ;
INSERT 0 1
postgres=# insert into t1 values(13); ERROR: duplicate key value violates unique constraint "idx_id_unq"
DETAIL: Key (id)=(13) already exists.