简体   繁体   English

PostgreSQL:删除所有锁

[英]postgresql: delete all locks

question: there is a table with over 9000 rows. 问题:有一个包含9000行以上的表。 It must be cleaned but without any locks (table in active using). 它必须被清洁但没有任何锁(处于活动使用状态的表)。 I tried to use pg_advisory_unlock_all, but no result. 我尝试使用pg_advisory_unlock_all,但没有结果。

select pg_advisory_unlock_all();
start transaction();
delete from table where id='1';
DELETE 1

start transaction();
delete from table where id='1';
(waiting for finish first transaction)

There is no way to delete data from a table without locking the rows you want to delete. 如果不锁定要删除的行,则无法从表中删除数据。

That shouldn't be a problem as long as concurrent access doesn't try to modify these rows or insert new ones with id = '1' , because writers never block readers and vice versa in PostgreSQL. 只要并发访问不尝试修改这些行或插入id = '1'新行,这就不成问题,因为在PostgreSQL中,写程序从不阻塞读者,反之亦然。

If concurrent transactions keep modifying the rows you want to delete, that's a little funny (why would you want to delete data you need?). 如果并发事务不断修改您要删除的行,那会有点有趣(为什么要删除所需的数据?)。 You'd have to wait for the exclusive locks, and you might well run into deadlocks. 您必须等待排他锁,否则很可能会遇到死锁。 In that case, it might be best to lock the whole table with the LOCK statement before you start. 在这种情况下,最好在开始之前使用LOCK语句锁定整个表。 Deleting from a table that small should then only take a very short time. 从表中删除这么小的表只需要很短的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM