为什么此 DELETE 查询会冻结数据库?
Why does this DELETE query freezes the DB?
有点奇怪,我不明白。当 运行 一个 select 像这样:
select count(*) "factures_quantite_achats_prms"
WHERE "factures_quantite_achats_prms"."quantite_achats_prm_id" IN (3099747, 3099746, 2979429, 2979430)
我得到正确的结果:4 行。
但是当 运行 这(相同但删除)时:
DELETE FROM "factures_quantite_achats_prms"
WHERE "factures_quantite_achats_prms"."quantite_achats_prm_id" IN (3099747, 3099746, 2979429, 2979430)
查询冻结。
删除的解释如下:
Delete on factures_quantite_achats_prms (cost=0.43..23.50 rows=4 width=6)
-> Index Scan using factures_quantite_achats_prms__quantite_achats_prm_id__idx on factures_quantite_achats_prms (cost=0.43..23.50 rows=4 width=6)
Index Cond: (quantite_achats_prm_id = ANY ('{3099747,3099746,2979429,2979430}'::integer[]))
此外,在查找锁定的 table 时,结果如下:
pid | usename | blocked_by | blocked_query
-------+---------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
28097 | Project | {14713} | DELETE FROM "factures_quantite_achats_prms" WHERE "factures_quantite_achats_prms"."facture_id" = AND "factures_quantite_achats_prms"."quantite_achats_prm_id" IN (3099747, 3099746, 2979429, 2979430)
所以结论是 DELETE 查询锁定了 table 但为什么呢?
DELETE 语句不会冻结数据库并且不会以独占模式锁定 table:它主要以独占模式锁定或尝试锁定所有选定的行。
如果 DELETE 查询正在等待另一个事务,则另一个事务可能已经锁定了一些行。
有点奇怪,我不明白。当 运行 一个 select 像这样:
select count(*) "factures_quantite_achats_prms"
WHERE "factures_quantite_achats_prms"."quantite_achats_prm_id" IN (3099747, 3099746, 2979429, 2979430)
我得到正确的结果:4 行。
但是当 运行 这(相同但删除)时:
DELETE FROM "factures_quantite_achats_prms"
WHERE "factures_quantite_achats_prms"."quantite_achats_prm_id" IN (3099747, 3099746, 2979429, 2979430)
查询冻结。
删除的解释如下:
Delete on factures_quantite_achats_prms (cost=0.43..23.50 rows=4 width=6)
-> Index Scan using factures_quantite_achats_prms__quantite_achats_prm_id__idx on factures_quantite_achats_prms (cost=0.43..23.50 rows=4 width=6)
Index Cond: (quantite_achats_prm_id = ANY ('{3099747,3099746,2979429,2979430}'::integer[]))
此外,在查找锁定的 table 时,结果如下:
pid | usename | blocked_by | blocked_query
-------+---------+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
28097 | Project | {14713} | DELETE FROM "factures_quantite_achats_prms" WHERE "factures_quantite_achats_prms"."facture_id" = AND "factures_quantite_achats_prms"."quantite_achats_prm_id" IN (3099747, 3099746, 2979429, 2979430)
所以结论是 DELETE 查询锁定了 table 但为什么呢?
DELETE 语句不会冻结数据库并且不会以独占模式锁定 table:它主要以独占模式锁定或尝试锁定所有选定的行。
如果 DELETE 查询正在等待另一个事务,则另一个事务可能已经锁定了一些行。