top of page

Stopping Runaway Queries Before They Start: SQL Quarantine on Exadata in Oracle Database@Azure

OmerNarman
Nov 6
8 min read

Every shared database has one: a reporting tool that lets users build their own queries. Most of them are fine. Now and then someone joins two large tables without a join condition, or filters on an unindexed expression, and a single statement runs for forty minutes, using CPU that the order-entry application needs. The DBA kills the session, and the next morning the same user presses the same button.


Resource Manager can already cancel a statement that runs too long. What it cannot do on its own is remember. SQL Quarantine, added in Oracle Database 19c, closes that gap: when Resource Manager cancels a statement for exceeding a limit, the database records the execution plan, and later executions of the same plan are refused immediately, before they use any resources. This post sets up the limit, watches a quarantine being created, adds one by hand, and shows how to release or copy quarantines.



The Use Case

The production database runs on Exadata Database Service in Oracle Database@Azure. The order-entry application connects from Azure Kubernetes Service, and a self-service reporting tool on Azure VMs connects as the account ADHOC_USER. Twice last month an ad hoc query ran for more than half an hour during business hours, and the application's response times doubled. The goal is to cancel any ad hoc statement that uses more than 60 seconds of CPU and make sure the same plan cannot run again.


Environment used in this runbook


  • Service: Exadata Database Service on Dedicated Infrastructure in Oracle Database@Azure

  • Database: Oracle Database 19c (19.24), container database with PDB SALESPDB, two-node RAC

  • Workloads: order entry as APP_USER, ad hoc reporting as ADHOC_USER

  • Limit: 60 seconds of CPU per call for ad hoc statements

  • Licence: SQL Quarantine is listed in the Licensing Information User Manual as available on Oracle Database Enterprise Edition on Engineered Systems and the Exadata cloud services, which includes Exadata Database Service in Oracle Database@Azure; Resource Manager is part of Enterprise Edition



How SQL Quarantine Works

  • Resource Manager sets the limit. A plan directive cancels a statement in a consumer group when it passes a CPU, elapsed time or I/O limit.

  • The cancelled plan is quarantined. When Resource Manager cancels a statement for that reason, the database creates a quarantine configuration for the statement's execution plan, with the limit that was exceeded.

  • Later executions are refused. When the same statement compiles to the quarantined plan and the limit still applies, it fails at once with ORA-56955 instead of running for minutes before being cancelled.

  • Only the plan is blocked. If statistics or a new index give the statement a different plan, that plan runs normally and is judged on its own.

  • DBAs can manage it. The DBMS_SQLQ package creates, changes, drops, exports and imports quarantine configurations.



Step 1: Check the Platform and the Current Plan

Confirm the release, that the database runs on Exadata, and whether any Resource Manager plan is active in the PDB.


SQL

-- Connected to SALESPDB as a DBA

SELECT version_full FROM v$instance;

COLUMN name FORMAT A24

COLUMN value FORMAT A20

SELECT name, value

FROM v$parameter

WHERE name IN ('resource_manager_plan', 'cell_offload_processing');

Expected output

VERSION_FULL
-----------------
19.24.0.0.0

NAME                     VALUE
------------------------ --------------------
cell_offload_processing  TRUE
resource_manager_plan

What to expect: A 19c release and no Resource Manager plan in the PDB yet. If the PDB already has a plan, add the consumer group and directive from Step 2 to that plan instead of creating a new one, because only one plan can be active at a time.



Step 2: Create a Consumer Group and a Cancel Limit

Create a consumer group for ad hoc users and a plan whose directive cancels any call in that group after 60 seconds of CPU. All changes go into a pending area and take effect only when it is submitted.


SQL

BEGIN

DBMS_RESOURCE_MANAGER.CREATE_PENDING_AREA;

DBMS_RESOURCE_MANAGER.CREATE_CONSUMER_GROUP(

consumer_group => 'ADHOC_QUERIES',

comment => 'Self-service reporting users');

DBMS_RESOURCE_MANAGER.CREATE_PLAN(

plan => 'SALES_DAYTIME',

comment => 'Protect order entry from runaway ad hoc queries');

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(

plan => 'SALES_DAYTIME',

group_or_subplan => 'ADHOC_QUERIES',

comment => 'Cancel ad hoc calls after 60 CPU seconds',

mgmt_p1 => 20,

switch_group => 'CANCEL_SQL',

switch_time => 60,

switch_for_call => TRUE);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(

plan => 'SALES_DAYTIME',

group_or_subplan => 'OTHER_GROUPS',

comment => 'Everything else, including order entry',

mgmt_p1 => 80);

DBMS_RESOURCE_MANAGER.VALIDATE_PENDING_AREA;

DBMS_RESOURCE_MANAGER.SUBMIT_PENDING_AREA;

END;

/

Expected output

PL/SQL procedure successfully completed.

What this does: It defines the plan SALES_DAYTIME. Statements in ADHOC_QUERIES that use more than 60 seconds of CPU in one call are switched to CANCEL_SQL, which cancels the call. switch_for_call means the limit applies to each call, so the session stays connected and its next call starts from zero. Every plan must have a directive for OTHER_GROUPS, which here also gives order entry the larger share of CPU when the system is busy.



Step 3: Map the Reporting Account and Activate the Plan

Map ADHOC_USER to the new consumer group, allow it to switch into the group, and activate the plan in the PDB.


SQL

BEGIN

DBMS_RESOURCE_MANAGER.CREATE_PENDING_AREA;

DBMS_RESOURCE_MANAGER.SET_CONSUMER_GROUP_MAPPING(

attribute => DBMS_RESOURCE_MANAGER.ORACLE_USER,

value => 'ADHOC_USER',

consumer_group => 'ADHOC_QUERIES');

DBMS_RESOURCE_MANAGER.SUBMIT_PENDING_AREA;

DBMS_RESOURCE_MANAGER_PRIVS.GRANT_SWITCH_CONSUMER_GROUP(

grantee_name => 'ADHOC_USER',

consumer_group => 'ADHOC_QUERIES',

grant_option => FALSE);

END;

/

ALTER SYSTEM SET resource_manager_plan = 'SALES_DAYTIME' SCOPE = BOTH SID = '*';

COLUMN username FORMAT A12

COLUMN resource_consumer_group FORMAT A23

SELECT username, resource_consumer_group, COUNT(*) AS sessions

FROM gv$session

WHERE username IN ('ADHOC_USER', 'APP_USER')

GROUP BY username, resource_consumer_group

ORDER BY username;

Expected output

PL/SQL procedure successfully completed.

System altered.

USERNAME     RESOURCE_CONSUMER_GROUP   SESSIONS
------------ ----------------------- ----------
ADHOC_USER   ADHOC_QUERIES                    6
APP_USER     OTHER_GROUPS                    84

What to expect: New ADHOC_USER sessions land in ADHOC_QUERIES, and the application stays in OTHER_GROUPS. Sessions that were connected before the mapping keep their old group until they reconnect, so ask the reporting team to restart the tool's connection pool. SID = '*' activates the plan on both RAC instances.



Step 4: Watch a Runaway Query Being Cancelled

When an ad hoc user runs a statement that passes the limit, Resource Manager cancels it. Here is what the user sees, followed by the DBA's view of the cancellation.


SQL

-- As ADHOC_USER, from the reporting tool: a join with a missing join condition

SELECT c.region, COUNT(*)

FROM sales.orders o, sales.customers c

WHERE o.order_date >= DATE '2026-01-01'

GROUP BY c.region;

Expected output

ERROR at line 1:
ORA-00040: active time limit exceeded - call aborted

What to expect: The call is cancelled after about 60 seconds of CPU, instead of running for half an hour. The session stays connected. At the same moment, the database creates a quarantine configuration for the statement's execution plan.



Step 5: Find the New Quarantine

Look at the quarantine configurations and at the cursor that was cancelled. The new configuration can take a few minutes to appear in the dictionary view.


SQL

COLUMN name FORMAT A36

COLUMN plan_hash_value FORMAT 9999999999

COLUMN cpu_time FORMAT A8

COLUMN origin FORMAT A16

SELECT name, plan_hash_value, cpu_time, origin, enabled

FROM dba_sql_quarantine

ORDER BY created;

Expected output

NAME                                 PLAN_HASH_VALUE CPU_TIME ORIGIN           ENA
------------------------------------ --------------- -------- ---------------- ---
SQL_QUARANTINE_8kd2rr0wjf6nxa4c1e9b       2817461903 60       RESOURCE-MANAGER YES

What to expect: One configuration created by Resource Manager for plan 2817461903, with the CPU limit of 60 seconds that the statement exceeded. ENABLED is YES, so the plan is now blocked. The name is generated by the database; use it in the DBMS_SQLQ calls that follow.



Step 6: Confirm the Next Execution Is Refused

When the user runs the same statement again, it fails at once instead of running for another minute. V$SQL counts how many executions the quarantine has stopped.


SQL

-- As ADHOC_USER: the same statement again

SELECT c.region, COUNT(*)

FROM sales.orders o, sales.customers c

WHERE o.order_date >= DATE '2026-01-01'

GROUP BY c.region;

Expected output

ERROR at line 1:
ORA-56955: quarantined plan used

Then check the cursor as the DBA:


SQL

COLUMN sql_id FORMAT A13

COLUMN sql_quarantine FORMAT A36

SELECT sql_id, plan_hash_value, executions, avoided_executions, sql_quarantine

FROM gv$sql

WHERE sql_quarantine IS NOT NULL;

Expected output

SQL_ID        PLAN_HASH_VALUE EXECUTIONS AVOIDED_EXECUTIONS SQL_QUARANTINE
------------- --------------- ---------- ------------------ ------------------------------------
7f3k9vq2mxt1d      2817461903          1                  3 SQL_QUARANTINE_8kd2rr0wjf6nxa4c1e9b

What to expect: The error comes back within a second, and AVOIDED_EXECUTIONS shows three attempts that never used CPU. Pass the error text to the reporting team: ORA-56955 means the query must be rewritten, and is not a fault of the database.


Tip: Ask the reporting tool's administrators to show ORA-00040 and ORA-56955 to users with a friendly message, such as "This report was stopped because it would run too long; please add a filter". Users then fix the query instead of opening a ticket.


Step 7: Quarantine a Known Bad Statement by Hand

You do not have to wait for Resource Manager. If AWR or a user complaint has already identified a statement whose plan is known to be bad, quarantine that plan directly and set the threshold yourself.


SQL

SET SERVEROUTPUT ON

DECLARE

l_name VARCHAR2(128);

BEGIN

l_name := DBMS_SQLQ.CREATE_QUARANTINE_BY_SQL_ID(

sql_id => '4vb8m1nq0hc2w',

plan_hash_value => 1946033287);

DBMS_SQLQ.ALTER_QUARANTINE(

quarantine_name => l_name,

parameter_name => 'ELAPSED_TIME',

parameter_value => '120');

DBMS_OUTPUT.PUT_LINE('Created ' || l_name);

END;

/

Expected output

Created SQL_QUARANTINE_2pq6zw5c9mfh3b7d0k1x

PL/SQL procedure successfully completed.

What this does: It quarantines one plan of statement 4vb8m1nq0hc2w with an elapsed time threshold of 120 seconds. Without a plan_hash_value, all plans of the statement would be quarantined, which is rarely what you want. Other thresholds that ALTER_QUARANTINE accepts are CPU_TIME, IO_MEGABYTES, IO_REQUESTS and IO_LOGICAL, and the ENABLED and AUTOPURGE flags.



Step 8: Release a Quarantine After the Query Is Fixed

Once the report has been rewritten, or a new index gives it a better plan, release the old quarantine. Disable it first if you want to keep the record, or drop it altogether.


SQL

-- Keep the record but stop blocking the plan

BEGIN

DBMS_SQLQ.ALTER_QUARANTINE(

quarantine_name => 'SQL_QUARANTINE_8kd2rr0wjf6nxa4c1e9b',

parameter_name => 'ENABLED',

parameter_value => 'NO');

END;

/

-- Remove a configuration that is no longer needed

EXEC DBMS_SQLQ.DROP_QUARANTINE('SQL_QUARANTINE_2pq6zw5c9mfh3b7d0k1x');

SELECT name, enabled FROM dba_sql_quarantine;

Expected output

PL/SQL procedure successfully completed.

PL/SQL procedure successfully completed.

NAME                                 ENA
------------------------------------ ---
SQL_QUARANTINE_8kd2rr0wjf6nxa4c1e9b  NO

What this does: The first plan may run again but stays on record; the second configuration is gone. With AUTOPURGE left at its default of YES, the database also purges configurations that have not been used for a long time, so old quarantines do not pile up.



Step 9: Copy Quarantines to Another Database

Quarantines found in production are useful in the test copy too, so that testers meet the same errors as users. DBMS_SQLQ packs configurations into a staging table that you can move with Data Pump and unpack on the other side.


SQL

-- On production

BEGIN

DBMS_SQLQ.CREATE_STGTAB_QUARANTINE(

staging_table_name => 'SQLQ_STAGE',

staging_table_owner => 'DBA_TOOLS');

END;

/

SET SERVEROUTPUT ON

DECLARE

n NUMBER;

BEGIN

n := DBMS_SQLQ.PACK_STGTAB_QUARANTINE(

staging_table_name => 'SQLQ_STAGE',

staging_table_owner => 'DBA_TOOLS',

name => '%');

DBMS_OUTPUT.PUT_LINE(n || ' quarantine configurations packed');

END;

/

Expected output

PL/SQL procedure successfully completed.

1 quarantine configurations packed

PL/SQL procedure successfully completed.

What to expect: The staging table now holds the configurations. Export DBA_TOOLS.SQLQ_STAGE with Data Pump, import it into the test database, and call DBMS_SQLQ.UNPACK_STGTAB_QUARANTINE there with the same table name and owner. The test database must also run on Exadata for the quarantines to have any effect.



Step 10: Review Quarantines Every Week

A quarantine is a symptom: someone ran a query that needs fixing. Review the list every week with the reporting team, and use it to decide which queries need a rewrite, an index or a materialized view.


SQL

COLUMN name FORMAT A36

COLUMN last_executed FORMAT A16

SELECT name, origin, enabled,

TO_CHAR(last_executed, 'YYYY-MM-DD HH24:MI') AS last_executed

FROM dba_sql_quarantine

ORDER BY last_executed DESC NULLS LAST;

Expected output

NAME                                 ORIGIN           ENA LAST_EXECUTED
------------------------------------ ---------------- --- ----------------
SQL_QUARANTINE_5tz1xk8n3rw0q9e2m4hc  RESOURCE-MANAGER YES 2026-11-06 14:22
SQL_QUARANTINE_8kd2rr0wjf6nxa4c1e9b  RESOURCE-MANAGER NO  2026-11-04 10:07

What to expect: A short list, with the last time each quarantined plan was attempted. An enabled quarantine that keeps being hit means a user still runs the bad report; one that is never hit can be dropped. If the list grows every week, the 60-second limit may be too strict for the reports the business needs, and is worth discussing with the reporting team.



Summary

SQL Quarantine gives Resource Manager a memory. Create a consumer group for ad hoc users and a plan directive that cancels calls after a CPU limit, map the reporting account to the group and activate the plan on all RAC instances. When a runaway query is cancelled with ORA-00040, its plan is quarantined, and the next attempts fail at once with ORA-56955 without using the Exadata cluster's CPU. Quarantine known bad plans by hand with DBMS_SQLQ, disable or drop configurations once the query is fixed, copy them to test databases with a staging table, and review the list weekly. The feature is available on Exadata, including Exadata Database Service in Oracle Database@Azure, so check the platform before you rely on it elsewhere.

Recent Posts

See All

Comments


bottom of page