This is a short blog post about a tiny little feature that you are probably unaware of but can make your life easier.
When you work with databases, you sometimes may find yourself in a situation where you got a long-running query because you forgot a
WHERE clause or you didn’t realize how many rows the table had, and you wished you could stop the query but not kill your entire database session in the process.
Say you have a transaction running, and now you want to verify your changes before committing but, oh no, you messed up the
WHERE clause and the query goes off for a while. What are you going to do? Kill the session but have to redo the entire transaction again, or wait it out but it could be quite a while. Well, if you are on Oracle Database 18c or later, you will find yourself in luck. Say hello to
ALTER SYSTEM CANCEL SQL!
Since Oracle Database 18c, you now got a new
ALTER SYSTEM CANCEL SQL clause that allows another (privileged) session to cancel the currently running SQL statement without killing the session in the process (like
ALTER SYSTEM KILL SESSION will do).
The syntax of
CANCEL SQL is rather easy and similar to the
KILL SESSION syntax, as you may expect:
ALTER SYSTEM CANCEL SQL 'SID, SERIAL, @INST_ID, SQL_ID';
The documentation describes these parameters as follows:
The following clauses are required in an
ALTER SYSTEM CANCEL SQL statement:
SID– Session ID
SERIAL– Session serial number
The following clauses are optional in an
ALTER SYSTEM CANCEL SQL statement:
INST_ID– Instance ID
SQL_ID– SQL ID of the SQL statement
SERIAL are both the same
SERIAL# from the session that you cancel the SQL for.
INST_ID identifies the database instance you want to cancel the SQL on. This is interesting when running the Oracle Database in a RAC cluster and say the SQL runs on a different node or the SQL in question is a parallel query across nodes.
SQL_ID parameter is the
SQL_ID of the SQL statement you want to cancel and provided as a failsafe mechanism. As the documentation says, “If
SQL_ID is not specified, the currently running SQL statement in the specified session is terminated.” So when would you want to specify the
SQL_ID then? Basically, whenever you want to make sure that you do not cancel the next SQL by accident. It could be that by the time you issue the
ALTER SYSTEM CANCEL SQL statement, the long-running statement just finished. If that SQL was executed by you in a tool or command line, that’s probably not a big deal as there is no next SQL statement unless you type it in. However, if the statement was executed, say as part of a script, then the script will have moved on to the following SQL statement, or potentially quite a few SQL statements by the time you hit Enter. However likely that is or not, if you specify the
ALTER SYSTEM CANCEL SQL command will only cancel the SQL statement with the specified id. If that SQL statement is no longer running, the command will do nothing.
Canceling a SQL statement
To cancel the SQL statement, just type in the command das specified above.
Session 1 (with long-running SQL statement):
SQL> INSERT INTO test SELECT * FROM very_big_table;
SQL> ALTER SYSTEM CANCEL SQL '181, 12538'; System altered.
SQL> INSERT INTO test SELECT * FROM very_big_table; INSERT INTO test SELECT * FROM very_big_table; * ERROR at line 1: ORA-01013: User requested cancel of current operation.
For more information, check out the documentation section on canceling SQL statements.
2 thoughts on “How to cancel long-running queries in Oracle Database”
Pull power cable.
Push power cable back in place.
Reminds me of that story where the server always went down at 2pm on a Saturday because the cleaners came and pulled the cable to vacuum. 😀
LikeLiked by 1 person