为什么针对唯一索引有非唯一条目? (PostgreSQL 9.0)
我的 postgresql 9.0 数据库中有一个基于函数的唯一索引。我尝试的任何手动测试都尚未使其失败,但当我查询数据库时,我在数据库中看到了一些重复项。
看看这个:
Index: "users_screen_name_idx" UNIQUE, btree (lower(screen_name::text))
# select lower(screen_name), count(1) from users group by lower(screen_name) having count(1) > 1;
lower | count
---------------+-------
xxx xxx 3735 | 2
xxx xxx 37383 | 2
... (36 more) ...
| 17254
(39 rows)
你知道这是怎么发生的吗?我知道 NULL 不是唯一的,这不是问题,而是其他 38 行。
I have a unique index in my postgresql 9.0 db, based on a function. I have yet to make it fail with any manual tests I try, but I am seeing some duplicates in the db when I query it.
Check this out:
Index: "users_screen_name_idx" UNIQUE, btree (lower(screen_name::text))
# select lower(screen_name), count(1) from users group by lower(screen_name) having count(1) > 1;
lower | count
---------------+-------
xxx xxx 3735 | 2
xxx xxx 37383 | 2
... (36 more) ...
| 17254
(39 rows)
Any ideas how this is happening? I know NULL's aren't unique, that's not the issue, it's the other 38 rows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您手动尝试使数据库系统失败,则可能会遇到索引损坏的情况。尝试重建索引 (
REINDEX
)。如果由于重复值而失败,那就这样了。If you have been manually trying to make the database system fail, it's possible that you have run into index corruption. Try rebuilding the index (
REINDEX
). If that fails because of duplicate values, then that's it.您可以在 PostgreSQL 中禁用触发器。这是一个非常危险的选项,但可用于实际将数据添加到违反唯一或外键约束的表中。
You can disable triggers in PostgreSQL. This is a very dangerous option, but can be used to actually add data into tables that violates unique or foreign key contraints.
如果您使用表继承,则可能会导致这种情况,因为不会继承唯一约束。事实上,这可能是此类事件的最常见原因,而不是数据损坏。
如果删除并重新创建索引会失败吗?
如果没有,则您的副本在其他地方。
If you are using table inheritance, that can cause this since unique constraints are not inherited. In fact this is probably the most common cause of this sort of thing and it isn't data corruption.
If you drop and recreate the index does it fail?
If not, your duplication is elsewhere.