简体   繁体   中英

Oracle WITH and MATERIALIZE hint acts as autonomous transaction for functions

In Oracle 12c if I call a function in a query that uses MATERIALIZE hint in a WITH..AS section, the function call acts like an autonomous transaction:

DROP TABLE my_table;

CREATE TABLE my_table (
    my_column NUMBER
);

-- Returns number of records in table 
CREATE OR REPLACE FUNCTION my_function 
RETURN INTEGER
IS
    i INTEGER;
BEGIN
    SELECT COUNT(1) INTO i FROM my_table;
    RETURN i;
END;
/

-- Inserts one record to table 
INSERT INTO my_table (my_column) VALUES (9);

-- Returns number of records in table. This works correctly, returns 1
SELECT COUNT(1) AS "use simple select" FROM my_table;

-- Returns number of records in table. This works correctly, returns 1
WITH x AS (
    SELECT /*+ MATERIALIZE */ COUNT(1) AS "use WITH, MATERIALIZE" FROM my_table
)
SELECT * FROM x;

-- Returns number of records in table. This works correctly, returns 1
SELECT my_function AS "use FUNCTION" FROM dual;

-- Returns number of records in table. This works INCORRECTLY, returns 0. 
-- Function is called in autonomous transaction?
WITH x AS (
    SELECT /*+ MATERIALIZE */ my_function "use WITH,MATERIALIZE,FUNCTION" FROM dual
)
SELECT * FROM x;

ROLLBACK;

Does anyone know what is the reason for this? Is it an Oracle bug or it is intended to work like this? (Why?) Why it works like this only when WITH is combined with MATERIALIZED hint and FUNCTION call?

This looks like bug 15889476, "Wrong results with cursor-duration temp table and function running on an active transaction"; and 13253977 "Wrong results or error with cursor-duration temp table and PLSQL function running on an active transaction".

I can reproduce on 11.2.0.3 but not 11.2.0.4; and from Husqvik's comment it doesn't seem to reproduce on 12.1.0.2. That aligns with the affected version and fix-first-included-in information in the bug documents.

See MOS documents 15889476.8 and 13253977.8 for more information. You may need to contact Oracle Support to confirm this is the issue you are seeing, but it looks pretty similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM