This article explains a case in which we resolved an incident on an Oracle RAC database. The database runs on three read-write nodes for high availability and serves two applications that deliver two different services. A large number of end users rely on it: some receive services through these applications, others provide them. A database outage therefore brings every application built on it to a complete halt: the interfaces become so slow that users can neither provide nor receive services. Even a few minutes of disruption can create significant backlogs and considerable frustration among users.
Context: how the incident began
The problem began when a query ran with an execution plan different from the usual one. This locked the table, and other queries that needed to access it were queued. As a result, a very large number of connections were left hanging on the database. Sessions piled up until they reached the maximum the database supports (3,872 sessions), bringing the entire application to a complete halt.
The table shows the situation at the time of the incident. The MACHINE column lists the machines the connections came from. The COUNT(*) column shows how many connections each machine had opened. Machine names have been masked to protect the customer's privacy.
Analyzing the incident
Given how severe the situation was, we immediately started looking for the query. It was not easy to spot, because it did not use bind variables. As a result, the database showed many queries with different SQL IDs, even though they all came from the same application interface. A closer analysis showed that all these queries accessed the same table, which allowed us to find the one that had caused the problem. This query read a 13 GB table without using any index. Some of the SQL IDs (65) had picked up a bad execution plan that made the queries run very slowly. This led to a lock on the table, a buildup of sessions, and ultimately the application lockup.
The solution
Before we could fix the problem, we had to find the bad execution plan. We identified it with the following query:
Next, we used another query to find the SQL IDs (65) that used this execution plan:
Using the results of the previous queries, we then retrieved the full plan with a third query:
When this query runs, the prompt asks for the values of the SQL_ID and PLAN_HASH_VALUE variables:
Enter value for sql_id: 8aaaaaaa8aaaa
Enter value for plan_hash_value: <bad_plan>
The image shows the bad execution plan. Here too, table names have been masked to protect the customer's privacy.
As the Rows column shows, the query read a very large number of rows on every execution, which is what made it so slow.
To fix the problem, we therefore had to force a change of execution plan. The queries above allowed us to find the correct execution plan, and the following command forced the database to use it:
n PLS_INTEGER;
BEGIN
n := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE(sql_id => '8aaaaaaa8aaaa', plan_hash_value => <good_plan>, fixed => 'YES', enabled => 'YES');
END;
/
The next image shows the correct execution plan (*table names have been masked to protect the customer's privacy).
With this plan, the query now reads far fewer rows.
Finally, to prevent the problem from recurring, we introduced bind variables into the query. Bind variables reduce the number of distinct queries, which lowers memory usage and execution times. They also give DBAs more information and more options to act.
Steps taken to resolve the incident in the Oracle RAC database
Analyzing the situation to determine how to solve the problem
Identifying the query that caused the incident
Identifying the bad execution plan
Identifying the correct execution plan
Forcing the execution plan
Introducing bind variables into the query