[英]Sequential Scan and Index Scan for primary key return different rows
我的表users
數據有問題。 該表的主鍵定義為:
"primary_c26a3d1a9d1c7aa4bb0a8d6b752c01a7" PRIMARY KEY, btree (id)
當我使用WITH
子句強制對表進行順序掃描時,我發現重復的ID:
=> WITH temp_table AS (select * from "users") SELECT id from temp_table group by id having count(id) > 1;
-[ RECORD 1 ]
id | 8225700
-[ RECORD 2 ]
id | 8225682
...
這是怎么發生的? 如果我通過索引搜索這些重復項,我沒有同樣的問題:
=> select count(*) from users where id = 8225700;
-[ RECORD 1 ]
count | 1
我正在使用PostgreSQL 9.1。
VACUUM沒有幫助我。 我試圖通過ctid刪除重復項:
// good and bad rows
> with usrs as (select ctid, * from users) select ctid, id from usrs where id = 8225700;
ctid | id
-------------+---------
(195669,33) | 8225700
(195708,34) | 8225700
// good row
select id, ctid from users where id = 8225700;
-[ RECORD 1 ]-----
id | 8225700
ctid | (195708,34)
// deleting bad row
DELETE FROM users WHERE ctid = '(195669,33)';
ERROR: update or delete on table "users" violates foreign key constraint "foreign_1589fcbc580d08caf03e0fbaaca7d6dd" on table "account"
詳細信息:仍然從account
表中引用密鑰(id)=(8225700)。
但真正的行有引用,我無法刪除它。
如何刪除這些損壞的行?
// deleting bad row DELETE FROM users WHERE ctid = '(195669,33)'; ERROR: update or delete on table "users" violates foreign key constraint "foreign_1589fcbc580d08caf03e0fbaaca7d6dd" on table "account" In detail: Key (id)=(8225700) is still referenced from table "account".
它說得非常清楚:該行由account
表引用。
您需要找到該引用並進行修復。
UPDATE account SET fkey_field = ??? WHERE ... ;
細節取決於account
表的結構和內容。
如果您需要更多幫助,請從psql粘貼\\d account
和\\d users
完整輸出。
聽起來像是一個糟糕的索引。 嘗試重建索引。
reindex table users
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.