Friday, April 20, 2018

Remove database locks that have been idle / inactive for at least 5 minutes

A. Oracle Database

//Find the all the row exclusive locks belong to dbuserID and have been inactive for more than
//5 minutes
SQL> select v1.sid, v1.serial#
           from v$session v1, v$locked_object v2
           where upper(v1.status)='INACTIVE' and v1.LAST_CALL_ET > 300 and                                                        upper(v2.ORACLE_USERNAME)='dbuserID' and v2.LOCKED_MODE=3
                      and v1.sid = v2.session_id;

//Remove the locks
SQL> ALTER SYSTEM KILL SESSION 'sid, serial#';


B. Postgresql

SQL> SELECT s.pid
           FROM pg_stat_activity s
           JOIN pg_locks p1 ON p1.pid = s.pid 
           WHERE (now() - s.query_start) > interval '5 minutes'
                    and upper(s.usename)='dbuserID' and upper(s.state) like 'IDLE%'
                    and p1.mode='RowExclusiveLock' and p1.granted is true;

SQL> select pg_terminate_backend(pid);

-----------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment