Oracle 26ai New Feature: WAIT and NOWAIT support for DML statements

Ever had an UPDATE hanging because another session forgot to COMMIT? Oracle AI Database 26ai has a SQL enhancement: WAIT and NOWAIT are now supported directly on INSERT, UPDATE, DELETE, and MERGE. The enhancement arrived in Release Update 23.26.2.

Try it

I have a two-node Oracle RAC environment, and this example uses a test schema in the same PDB across both nodes, rac1 and rac2.

Make sure your database is running RU 23.26.2 or later, and use a test table rather than production data.

SQL> SELECT inst_id, instance_name, version_full
FROM gv$instance
ORDER BY inst_id;
INST_ID INSTANCE_NAME VERSION_FULL
---------- ---------------- -----------------
1 orcl1 23.26.3.0.0
2 orcl2 23.26.3.0.0

Create a sample table and a row:

CREATE TABLE lock_test (id NUMBER PRIMARY KEY, val NUMBER);
INSERT INTO lock_test VALUES (1, 100);
COMMIT;

Session 1 (rac1): Lock the row and leave the transaction open.

SQL> UPDATE lock_test SET val = 200 WHERE id = 1;
-- Don't commit yet.

Session 2 (rac2): Connect to the same PDB. Confirm the instance, then try the new syntax.

SQL> SELECT instance_name FROM v$instance;
INSTANCE_NAME
----------------
orcl2
SQL> UPDATE lock_test SET val = 300 WHERE id = 1 NOWAIT;
ERROR at line 1:
ORA-00054: Failed to acquire a lock (Type: "TX", Name: "Transaction",
Description: "Lock held by a transaction to allow other transactions to wait
for it") because it is currently held by another session. The resource being
locked can be identified by 262150 ("usn<<16 | slot") and 770 ("sequence")
ORA-40097: Failure to acquire row lock, currently held by transaction ID
4.6.770
SQL> UPDATE lock_test SET val = 300 WHERE id = 1 WAIT 5 SECONDS;
...
ERROR at line 1:
ORA-00054: Failed to acquire a lock (Type: "TX", Name: "Transaction",
Description: "Lock held by a transaction to allow other transactions to wait
for it") because it is currently held by another session. The resource being
locked can be identified by 262150 ("usn<<16 | slot") and 770 ("sequence")
ORA-40097: Failure to acquire row lock, currently held by transaction ID
4.6.770

The first UPDATE should fail immediately because the row is locked. The second waits up to five seconds before failing if Session 1 still holds the lock.

What’s new?

NOWAIT = don’t wait for the locked row. WAIT 5 SECONDS = wait for a bounded period. You can also use WAIT n MILLISECONDS, WAIT n MICROSECONDS, or WAIT FOREVER (the default indefinite wait behavior).

This is not RAC-specific.

Cleanup

-- Session 1:
ROLLBACK;
DROP TABLE lock_test;

Blocking session, find root blockers and kill

 

By the following script you are able to find root cause of the locks. Script do not display BACKGROUND processes that are locking others (for example, LGWR, DBWR), because killing them causes database crush.

In front of the kill immediate statement there is written additional information such as : username[machine]:sql_id[prev_sql_id]:program and then comes kill immediate statement.

with lk as (select blocking_instance||'.'||blocking_session blocker, inst_id||'.'||sid waiter
 from gv$session where blocking_instance is not null and blocking_session is not null and username is not null)
 select lpad(' ',2*(level-1))||waiter lock_tree from
 (select * from lk
 union all
 select distinct 'root', blocker from lk
 where blocker not in (select waiter from lk))
 connect by prior waiter=blocker start with blocker='root';
--Generate SQLs to kill top-level blockers
set serverout on
 declare
 sess varchar2(20);
 sessinfo varchar2(100);
 begin
 for i in
 (with lk as (select blocking_instance||'.'||blocking_session blocker, inst_id||'.'||sid waiter
 from gv$session where blocking_instance is not null and blocking_session is not null and username is not null)
 select distinct blocker from lk where blocker not in (select waiter from lk)
 )
 loop
 begin
 select regexp_substr(i.blocker,'[0-9]+$')||','||serial# ||',@' || regexp_substr(i.blocker,'[0-9]+'),
 substr(username||'['||machine||']:'||sql_id||'['||prev_sql_id||']:'||program,1,100) into sess, sessinfo
 from gv$session where inst_id = regexp_substr(i.blocker,'[0-9]+') and sid = regexp_substr(i.blocker,'[0-9]+$') and type='USER';
 dbms_output.put_line(sessinfo || ' ' || 'alter system kill session ''' || sess || ''' immediate;');
 exception when no_data_found
 then continue;
 end;
 end loop;
 end;
 /