Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql remove stale lock

Tags:

After a system crash my Postgresql database does have a lock on a row.

The pg_locks table contains a lot of rows without a pid. i.e.

select locktype,database,relation,virtualtransaction, pid,mode,granted from pg_locks p1;

locktype    | database | relation | virtualtransaction |  pid  |       mode       | granted 
---------------+----------+----------+--------------------+-------+------------------+---------
 relation      |    16408 |    31459 | -1/40059           |       | AccessShareLock  | t
 relation      |    16408 |    31459 | -1/40059           |       | RowExclusiveLock | t
 relation      |    16408 |    31022 | -1/40060           |       | AccessShareLock  | t
 transactionid |          |          | -1/40060           |       | ExclusiveLock    | t
 relation      |    16408 |    31485 | -1/40060           |       | AccessShareLock  | t

How do I get the transaction 40060 killed and the locks removed?

like image 388
Nils Bokermann Avatar asked Feb 09 '17 10:02

Nils Bokermann


1 Answers

Ok, solution found by myself:

  1. Find the gid to the transaction (i.e. 40060 in the case above) by select * from pg_prepared_xacts where transaction = 40060;
  2. Find an awful long gid.
  3. ROLLBACK PREPARED gid;

This will clear the locks.

like image 80
Nils Bokermann Avatar answered Oct 03 '22 15:10

Nils Bokermann