Postgresql 只有一个整数组合的Postgres索引

Postgresql 只有一个整数组合的Postgres索引,postgresql,indexing,Postgresql,Indexing,我正在开发一个系统,该系统必须引用由另一个应用程序创建的数据。另一个应用程序数据库有一个表,其中包含: | contactid | revision | lineno | other data... | | 12345 | 00 | 01 | other data... | | 12345 | 00 | 02 | other data... | | 12345 | 01 | 01 | other data...

我正在开发一个系统,该系统必须引用由另一个应用程序创建的数据。另一个应用程序数据库有一个表,其中包含:

| contactid | revision | lineno | other data... | | 12345 | 00 | 01 | other data... | | 12345 | 00 | 02 | other data... | | 12345 | 01 | 01 | other data... | | 12345 | 01 | 02 | other data... | | 67890 | 00 | 01 | other data... | | 67890 | 01 | 01 | other data... | |联系人ID |版本|行号|其他数据| |12345 | 00 | 01 |其他数据| |12345 | 00 | 02 |其他数据| |12345 | 01 | 01 |其他数据| |12345 | 01 | 02 |其他数据| |67890 | 00 | 01 |其他数据| |67890 | 01 | 01 |其他数据| 关键在于收缩、修订、行号。在我的系统中,一次只能有一个合同处于活动状态,因此如果在我的表中有

| 12345 | 00 | 01 | other data... | | 12345 | 00 | 02 | other data... | |12345 | 00 | 01 |其他数据| |12345 | 00 | 02 |其他数据| 我不能在表格中用不同的修改来压缩相同的内容。我可以使用什么样的索引来强制执行这种任性的唯一性。

您需要一个


引入另一个表是否可以接受?因此,您有了看起来像第一个表的
T1
,您正在构建与
T1
结构相同的
T2
,但您正在添加一个额外的唯一性约束?为什么不创建一个唯一的复合索引(contactid、revision、lineno)?类似于
在表上创建唯一索引tableidx(contactid、revision、lineno)@muistooshort-这是正确的。@REDMONKEY当然可以!这就是复合索引的全部要点。您不应该在任何单个列上创建唯一索引,而应该在它们的组合上创建唯一索引。所以你可以有(1234,1,1)和(1234,2,1)。但在这种情况下,你不能有多个(1234,1,1)。太棒了,这就是我需要的。非常感谢。另外,这在DbFiddle上不起作用,因为它需要激活btree_gist。
create table my_table(
    contactid int, 
    revision text, 
    lineno text, 
    other_data text,
    exclude using gist (contactid with =, revision with <>)
);
create extension if not exists btree_gist;