ORA-04031 After a Release: Finding the Hard Parse Storm That Eats the Shared Pool on Exadata in Oracle Database@Azure
ORA-04031 is one of those errors that looks like a memory problem and is usually an application problem. The shared pool runs out of a contiguous chunk of the size a session asks for, the session gets the error, and someone proposes doubling the SGA. More memory delays the next error by a few hours. The real cause is often a flood of SQL statements that are all different, because the application builds them with literal values instead of bind variables, so each one is parsed, stored and then thrown away.
This post works through such a case on Exadata Database Service in Oracle Database@Azure. It confirms the error, shows how the memory manager reacted, measures the hard parse rate, finds the statements that differ only in their literals, and keeps the database stable until the application is fixed.
The Use Case
A new release of the order service went live in Azure Kubernetes Service in the morning. By early afternoon, the alert log on both RAC instances showed ORA-04031 errors, CPU on the database servers was much higher than usual, and some order screens failed with errors. The release notes mention a new product search, and nothing about database changes.
Environment used in this runbook
Service: Exadata Database Service on Dedicated Infrastructure in Oracle Database@Azure
Database: Oracle Database 19c (19.24), PDB SALESPDB, two-node RAC
Memory: SGA_TARGET of 96 GB per instance, automatic shared memory management
Application: order service in Azure Kubernetes Service, connecting as ORDER_SVC
Licence: the V$ views used here are part of Enterprise Edition; AWR requires the Diagnostics Pack, included in licence-included Exadata Database Service
Why Literal SQL Hurts
Every distinct text is a new statement. A query with WHERE product_id = 1001 and the same query with 1002 are two different statements to the database.
Each one is hard parsed. The optimizer builds a plan, and the shared pool stores the cursor, even though it will probably never be used again.
Parsing costs CPU and latches. Thousands of hard parses a second keep the CPUs busy and serialise sessions on shared pool structures.
Memory fragments. Many small cursors come and go, the free memory breaks into small pieces, and a request for a larger chunk fails with ORA-04031.
Step 1: Confirm the Error and Its Size
Read the ORA-04031 messages in the alert log. The message tells you which pool ran short and how large the failed request was.
Shell |
# On a database server, as the oracle user adrci exec="set home diag/rdbms/sales_az/SALES1; show alert -p \"message_text like '%ORA-04031%'\" -term" | tail -4 |
Expected output |
2026-12-02 13:41:07.218000 +00:00
ORA-04031: unable to allocate 4160 bytes of shared memory ("shared pool","unknown object","sga heap(3,0)","modification ")
2026-12-02 13:52:44.903000 +00:00
ORA-04031: unable to allocate 32800 bytes of shared memory ("shared pool","SELECT p.product_id, p.name...","sga heap(2,0)","KGLH0^4e8a1c2d")What to expect: Errors in the shared pool, for small and medium requests, the second one while loading a SELECT on products. Failing requests of a few kilobytes in a shared pool of many gigabytes point to fragmentation and churn rather than a pool that is simply too small.
Step 2: See How the Memory Manager Reacted
With automatic shared memory management, the database moves memory between the buffer cache and the shared pool as demand changes. The resize history shows whether the shared pool has been growing all day.
SQL |
-- Connected to CDB$ROOT as SYSDBA COLUMN component FORMAT A20 COLUMN oper_type FORMAT A8
SELECT TO_CHAR(start_time, 'HH24:MI') AS time, component, oper_type, ROUND(initial_size/1024/1024/1024, 1) AS from_gb, ROUND(final_size/1024/1024/1024, 1) AS to_gb FROM v$sga_resize_ops WHERE start_time > TIMESTAMP '2026-12-02 08:00:00' AND component IN ('shared pool', 'DEFAULT buffer cache') ORDER BY start_time FETCH FIRST 6 ROWS ONLY; |
Expected output |
TIME COMPONENT OPER_TYP FROM_GB TO_GB
----- -------------------- -------- ---------- ----------
09:14 shared pool GROW 10.0 11.0
09:14 DEFAULT buffer cache SHRINK 78.0 77.0
10:02 shared pool GROW 11.0 13.0
10:02 DEFAULT buffer cache SHRINK 77.0 75.0
11:37 shared pool GROW 13.0 16.0
11:37 DEFAULT buffer cache SHRINK 75.0 72.0What to expect: The shared pool has grown from 10 to 16 GB since the release, taking memory from the buffer cache each time. The memory manager is trying to help, but the growth does not stop the errors, because the problem is not the size of the pool but what fills it.
Step 3: Measure the Parse Rates
Compare hard parses with all parses and with executions. A healthy OLTP system hard parses rarely; most executions reuse a cursor that is already in the shared pool.
SQL |
COLUMN metric_name FORMAT A28
SELECT inst_id, metric_name, ROUND(value) AS per_second FROM gv$sysmetric WHERE metric_name IN ('Hard Parse Count Per Sec', 'Total Parse Count Per Sec', 'Executions Per Sec') AND group_id = 2 ORDER BY inst_id, metric_name; |
Expected output |
INST_ID METRIC_NAME PER_SECOND
---------- ---------------------------- ----------
1 Executions Per Sec 16204
1 Hard Parse Count Per Sec 3871
1 Total Parse Count Per Sec 4952
2 Executions Per Sec 15622
2 Hard Parse Count Per Sec 3644
2 Total Parse Count Per Sec 4710What to expect: Almost 4,000 hard parses a second per instance, about one for every four executions. Before the release, AWR shows fewer than 50 a second. Something is generating new SQL texts at a very high rate.
Step 4: Find Statements That Differ Only in Literals
FORCE_MATCHING_SIGNATURE is the same for statements that would be identical if their literals were replaced by binds. Grouping by it finds the families of literal SQL and shows how many copies of each are in the shared pool.
SQL |
COLUMN sample_text FORMAT A48
SELECT force_matching_signature AS fms, COUNT(*) AS copies, ROUND(SUM(sharable_mem)/1024/1024) AS mem_mb, MIN(SUBSTR(sql_text, 1, 48)) AS sample_text FROM v$sql WHERE force_matching_signature <> 0 AND parsing_schema_name = 'ORDER_SVC' GROUP BY force_matching_signature HAVING COUNT(*) > 100 ORDER BY copies DESC FETCH FIRST 3 ROWS ONLY; |
Expected output |
FMS COPIES MEM_MB SAMPLE_TEXT
-------------------- ---------- ---------- ------------------------------------------------
8237461950271834566 48211 2876 SELECT p.product_id, p.name, p.price FROM sales.
12904367185522104871 1204 61 SELECT o.order_id, o.status FROM sales.orders o
3318402876451904213 388 19 UPDATE sales.carts SET updated_at = TO_DATE('202What to expect: One family of more than 48,000 copies of the same product search, using almost 3 GB of shared pool, plus two smaller families. Each copy differs only in the product name or ID written into the text. This is the new product search from the release, building its SQL by string concatenation.
Tip: Keep the FMS value of the worst family. It identifies the statement independently of its literals, so you can track it before and after the fix, and give it to the developers together with one sample text.
Step 5: Look at What Fills the Shared Pool
Check the largest consumers inside the shared pool. Cursor heaps and SQL areas growing at the expense of free memory confirm the picture.
SQL |
COLUMN name FORMAT A26
SELECT name, ROUND(bytes/1024/1024) AS mb FROM v$sgastat WHERE pool = 'shared pool' ORDER BY bytes DESC FETCH FIRST 5 ROWS ONLY; |
Expected output |
NAME MB
-------------------------- ----------
SQLA 5612
KGLH0 3984
free memory 812
KGLHD 701
db_block_hash_buckets 512What to expect: SQLA (SQL areas) and KGLH0 (library cache heaps) dominate, and less than 1 GB is free in a 16 GB pool. That free memory is scattered in small pieces, which is why requests of a few kilobytes fail. The memory is held by cursors that will never be executed again.
Step 6: Relieve the Pressure for the Problem Module Only
While the developers change the code to use bind variables, let the database replace the literals for the affected sessions. CURSOR_SHARING = FORCE does that, but setting it for the whole database can change plans for other applications, so set it only for the order service.
SQL |
-- Connected to SALESPDB as a DBA CREATE OR REPLACE TRIGGER sys.order_svc_cursor_sharing AFTER LOGON ON DATABASE WHEN (USER = 'ORDER_SVC') BEGIN EXECUTE IMMEDIATE 'ALTER SESSION SET cursor_sharing = FORCE'; END; /
SELECT username, COUNT(*) AS sessions FROM gv$session WHERE username = 'ORDER_SVC' GROUP BY username; |
Expected output |
Trigger created.
USERNAME SESSIONS
---------- ----------
ORDER_SVC 842What this does: Every new session of ORDER_SVC rewrites literals into system-generated binds, so the 48,000 variants of the product search collapse into one shared cursor. Existing sessions are not affected until they reconnect, so ask the application team for a rolling restart of the order-service pods. Treat the trigger as temporary: it hides the defect, and a literal that matters to the optimizer, such as a status flag with skewed data, may get a worse plan.
Step 7: Set a Floor for the Shared Pool
The memory manager will keep moving memory back and forth while the storm lasts. Give the shared pool a minimum size, so it cannot be shrunk below a level that has been enough in the past, and so the buffer cache is not drained either.
SQL |
-- Connected to CDB$ROOT as SYSDBA ALTER SYSTEM SET shared_pool_size = 12G SCOPE = BOTH SID = '*';
COLUMN name FORMAT A20 COLUMN value FORMAT A12
SELECT inst_id, name, display_value AS value FROM gv$parameter WHERE name IN ('shared_pool_size', 'sga_target') ORDER BY inst_id, name; |
Expected output |
System altered.
INST_ID NAME VALUE
---------- -------------------- ------------
1 sga_target 96G
1 shared_pool_size 12G
2 sga_target 96G
2 shared_pool_size 12GWhat this does: With SGA_TARGET set, SHARED_POOL_SIZE acts as a minimum: the memory manager may grow the shared pool above 12 GB but never shrink it below. This avoids the opposite problem after the fix, when the pool would shrink quickly and the next busy hour would have to grow it again.
Step 8: Flush Once, Then Watch the Parse Rate
Once the pods have restarted with the new session setting, flush the shared pool on each instance during a quiet moment to clear the thousands of dead cursors, then watch the parse rates return to normal.
SQL |
-- Connected to CDB$ROOT as SYSDBA, once per instance ALTER SYSTEM FLUSH SHARED_POOL;
SELECT inst_id, metric_name, ROUND(value) AS per_second FROM gv$sysmetric WHERE metric_name IN ('Hard Parse Count Per Sec', 'Executions Per Sec') AND group_id = 2 ORDER BY inst_id, metric_name; |
Expected output |
System altered.
INST_ID METRIC_NAME PER_SECOND
---------- ---------------------------- ----------
1 Executions Per Sec 16518
1 Hard Parse Count Per Sec 38
2 Executions Per Sec 15903
2 Hard Parse Count Per Sec 41What to expect: Hard parses down from almost 4,000 to about 40 a second for the same number of executions, and the CPU on the database servers back to its usual level. Flushing the shared pool causes a short burst of hard parses for every application, so do it once, at a quiet time, and never as a routine fix.
Step 9: Follow Up on the Real Fix
Keep checking the literal SQL families until the corrected release removes them, and then remove the logon trigger so the database returns to its normal settings.
SQL |
SELECT force_matching_signature AS fms, COUNT(*) AS copies FROM v$sql WHERE force_matching_signature = 8237461950271834566 GROUP BY force_matching_signature;
-- After the corrected release with bind variables: -- DROP TRIGGER sys.order_svc_cursor_sharing; |
Expected output |
FMS COPIES
-------------------- ----------
8237461950271834566 1What to expect: A single cursor for the product search, shared by all executions. When the application release with proper bind variables is live, drop the trigger, check once more that the family stays at one cursor, and add the hard parse rate to the monitoring so the next such release is caught in minutes, not hours.
Summary
ORA-04031 after a release usually means that the application stopped sharing SQL. Confirm the error and its size in the alert log, read the SGA resize operations, and compare the hard parse rate with executions. Group V$SQL by FORCE_MATCHING_SIGNATURE to find the literal statement families, and check V$SGASTAT to see the shared pool filled with dead cursors. Relieve the pressure with CURSOR_SHARING = FORCE for the affected user only, set a minimum shared pool size, flush once after the sessions reconnect, and remove the workaround when the application uses bind variables again.
Comments