Remove pg_lock from postgres without any PID
查看DB鎖時發現鎖沒有pid,只有事務ID
這種情況通過kill pid的方法無法刪除鎖
-- 查詢pg_locks
postgres=# select * from pg_locks;
locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath
--------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+------------------+---------+----------
relation | 16390 | 19694 | | | | | | | | -1/24019935 | | RowExclusiveLock | t | f
transactionid | | | | | | 24019935 | | | | -1/24019935 | | ExclusiveLock | t | f
根據事務ID刪除鎖
-- 查詢事務的GID
SELECT gid FROM pg_prepared_xacts WHERE "transaction" = 165588128;
-- 回滾事務(鎖即可刪除)
ROLLBACK PREPARED '100_Z2xvYmFsVHJhbnNhY3Rpb25JZA==_M2M2YWExYjktZTRjYS00ZTZhLTg3ZjItYjlkNTMwZGM0MmU2';
這種鎖的產生原因可能是事務准備好后PID還沒產生、DB就重啟了,一般virtualtransaction開頭為-1的都是這種鎖
清除掉全部異常未完成的事務鎖
ROLLBACK PREPARED (SELECT gid FROM pg_prepared_xacts);
