大型 VARCHAR 的唯一约束 - PostgreSQL

cze*_*rny 8 postgresql index varchar postgresql-9.3 unique-constraint

我有一个列定义如下:

data_url character varying(32768) NOT NULL
Run Code Online (Sandbox Code Playgroud)

和该列的 UNIQUE 约束:

CONSTRAINT unique_data_url UNIQUE (data_url)
Run Code Online (Sandbox Code Playgroud)

当一个大对象被插入到表中时,会出现以下错误信息:

ERROR:  index row requires 32584 bytes, maximum size is 8191
Run Code Online (Sandbox Code Playgroud)

如何设置 PostgreSQL 以便能够索引大于 8191 个字符的对象?空间和速度都不是问题。它是一个很少更改的表,最多有数百行。

环境:PostgreSQL 9.3.6,Fedora 20 x64

cze*_*rny 14

正如@Josh Kupershmidt@JoeNahmias所建议的,解决方案是在长值的 md5 哈希上使用 UNIQUE。但是 PostgreSQL 9.3 不支持UNIQUE 约束中的表达式,因此必须使用支持表达式的index

create unique index unique_data_url_index on mytable (md5(data_url));
Run Code Online (Sandbox Code Playgroud)