How to cancel long-running queries in Oracle Database

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

The SID and SERIAL are both the same SID and SERIAL# from the session that you cancel the SQL for.
The 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.
The 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 SQL_ID, 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;

Session 2:

SQL> ALTER SYSTEM CANCEL SQL '181, 12538';

System altered.

Session 1:

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.

Everything you always wanted to know about Oracle Database NLS_LANG and character sets

I have recently come across the Oracle Database Globalization Support again and the question of what character set is used when, how and where. A lot of this can be or is controlled by the NLS_LANG OS environment variable, buried deep down in the Database Globalization Support Guide. But I just found this “hidden gem” on the Oracle web page that, for some reason, isn’t very well indexed by the search engines: https://www.oracle.com/database/technologies/faq-nls-lang.html

I also found that the page could do with an overhaul and a few more details, so I’ve taken the liberty to provide an updated version of it over here: Oracle Database NLS_LANG FAQ

Hopefully, this will help other users of Oracle Database to understand this environment variable better and maybe help to improve the overall search engine ranking for that page. If you have any questions regarding what character set is used or should be used and how to set what, that FAQ page is your friend.

SOLVED: Oracle XE: ORA-00821: Specified value of sga_target is too small

tl;dr

  • Open ${ORACLE_HOME}/assistants/dbca/templates/XE_Database.dbc with a text editor
  • Find <InitParamAttributes><InitParams>
  • Add the following XML tag under <InitParams>: <initParam name="cpu_count" value="2"/>
Continue reading “SOLVED: Oracle XE: ORA-00821: Specified value of sga_target is too small”

Introducing gvenzl/oracle-xe: Oracle Database XE Docker images

One of the things that kept me busy lately was experimenting with how much an Oracle XE database setup could be streamlined inside a Docker image for things like CI/CD consumption. Pretty much ever since I put together the first official build scripts for Oracle Database, people have asked for faster image pull and startup times to speed up their continuous integration tests. A lot of things have changed since then, and I’m happy that my engineering colleagues at Oracle have taken on the maintenance and further enhancements of Oracle’s official Docker build files and images, and integrated them into the internal processes.

Continue reading “Introducing gvenzl/oracle-xe: Oracle Database XE Docker images”

How to install Oracle Database 18c XE on Oracle Linux 8

It has been more than two and a half years since Oracle 18c XE has been released for Linux. Since then things have changed and one of these changes was the release of Oracle Linux 8 about nine months later. Unfortunately, installing Oracle 18c XE on Oracle Linux 8 is no longer quite as straightforward as it was with Linux 7 – a simple yum command. That is because the oracle-database-preinstall-18c is not provided for Oracle Linux 8. However, installing Oracle 18c XE on Oracle Linux 8 is still possible, it just requires a few more keystrokes.

tl;dr

Execute all commands as root user:

  1. curl -OL https://download.oracle.com/otn-pub/otn_software/db-express/oracle-database-xe-18c-1.0-1.x86_64.rpm
  2. dnf install -y /bin/bash /bin/sh /etc/redhat-release bc bind-utils binutils ethtool glibc glibc-devel initscripts ksh libaio libaio-devel libgcc libstdc++ libstdc++-devel make module-init-tools net-tools nfs-utils openssh-clients pam procps psmisc smartmontools sysstat unzip util-linux-ng xorg-x11-utils xorg-x11-xauth libnsl
  3. rpm -i --nodeps oracle-database-xe-18c-1.0-1.x86_64.rpm
Continue reading “How to install Oracle Database 18c XE on Oracle Linux 8”