创建唯一索引似乎失败了,但无论如何都创建了?

Creating unique index seems to fail, but is created anyway?

正在创建唯一(多列)索引,但它失败了,因为 table 中已经存在非唯一行。然而,索引似乎无论如何都被创建了...

这是我正在使用的SQL:

CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS company_by_code ON public.company USING BTREE("owner","org","code")

这导致 Postgresql 的以下响应:

ERROR:  could not create unique index "company_by_code"
DETAIL:  Key (owner, org, code)=(ABC, DEF, XYZ) is duplicated.
SQL state: 23505

然而,即使 CREATE 语句似乎出错了,INDEX 似乎还是被创建了。

这是可以预料的吗?

这是documented in the manual

If a problem arises while scanning the table, such as a deadlock or a uniqueness violation in a unique index, the CREATE INDEX command will fail but leave behind an “invalid” index. This index will be ignored for querying purposes because it might be incomplete; however it will still consume update overhead. The psql \d command will report such an index as INVALID

如果您不想要这种行为,请不要使用 CONCURRENTLY