您可以添加基于回报的唯一索引,EG:
create unique index so45 on example ((ARRAY[greatest(a,c),least(a,c)]));
示例:
t=# insert into example select 1,2,3;
INSERT 0 1
t=# insert into example select 1,2,1;
INSERT 0 1
t=# insert into example select 3,2,1;
ERROR: duplicate key value violates unique constraint "so45"
DETAIL: Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.
在这种情况下,您不需要 PK 来限制唯一性:
t=# alter table example drop CONSTRAINT example_pkey ;
ALTER TABLE
t=# insert into example select 1,3,3;
ERROR: duplicate key value violates unique constraint "so45"
DETAIL: Key ((ARRAY[GREATEST(a, c), LEAST(a, c)]))=({3,1}) already exists.