diff --git a/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/2-function-supporting-autonomous-transaction.md b/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/2-function-supporting-autonomous-transaction.md index 862fc7a3742c91e26172a63a3c2d4c2640b8fe6d..e40410722775e06c21d6aa88e2c198b53339f980 100644 --- a/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/2-function-supporting-autonomous-transaction.md +++ b/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/2-function-supporting-autonomous-transaction.md @@ -1,75 +1,56 @@ --- -title: 函数支持自治事务 -summary: 函数支持自治事务 +title: 用户自定义函数支持自治事务 +summary: 用户自定义函数支持自治事务 author: Zhang Cuiping date: 2021-05-10 --- -# 函数支持自治事务 +# 用户自定义函数支持自治事务 -自治事务可以在函数中定义,标识符为PRAGMA AUTONOMOUS_TRANSACTION,执行的函数块中使用包含start transaction和commit/rollback的sql,其余语法与CREATE FUNCTION创建函数语法类似,一个简单的用例如下: +自治事务可以在函数中定义,标识符为PRAGMA AUTONOMOUS_TRANSACTION,执行的函数块中使用包含start transaction和commit/rollback的sql,其余语法与[CREATE FUNCTION](48-CREATE-FUNCTION)创建函数语法类似,一个简单的用例如下: ```sql ---创建表。 -CREATE TABLE test1 (a int, b text); ---创建包含自治事务的函数。 -CREATE OR REPLACE FUNCTION autonomous_easy_2(i int) RETURNS integer -LANGUAGE plpgsql -AS $$ -DECLARE +--建表 +MogDB=# create table t2(a int, b int); +MogDB=# insert into t2 values(1,2); +MogDB=# select * from t2; +a | b +---+--- +1 | 2 +(1 row) + +--创建包含自治事务的存储过程 +CREATE OR REPLACE PROCEDURE autonomous_4(a int, b int) AS +DECLARE + num3 int := a; + num4 int := b; PRAGMA AUTONOMOUS_TRANSACTION; BEGIN - START TRANSACTION; - INSERT INTO test1 VALUES (2, 'a'); - IF i % 2 = 0 THEN - COMMIT; - ELSE - ROLLBACK; - END IF; - RETURN i % 2 = 0; + insert into t2 values(num3, num4); + END; -$$; ---执行命令。 -select autonomous_easy_2(1); ---执行结果。 - autonomous_easy_2 -------------------- - 0 -(1 row) ---执行命令,查询表数据。 -select * from test1; ---执行结果。 - a | b ----+--- -(0 rows) ---执行命令。 -select autonomous_easy_2(2); ---执行结果。 - autonomous_easy_2 -------------------- - 1 -(1 row) ---执行命令,查询表数据。 -select * from test1; ---执行结果 - a | b ----+--- - 2 | a -(1 row) ---清空表数据。 -truncate table test1; ---在回滚的事务块中执行包含自治事务的函数。 -begin; -insert into test1 values(1,'b'); -select autonomous_easy_2(2); -rollback; ---检查表数据。 -select * from test1; ---执行结果如下。 - a | b ----+--- - 2 | a +/ +--创建调用自治事务存储过程的普通存储过程 +CREATE OR REPLACE PROCEDURE autonomous_5(a int, b int) AS +DECLARE +BEGIN + + insert into t2 values(666, 666); + autonomous_4(a,b); + rollback; +END; +/ +--调用普通存储过程 +MogDB=# select autonomous_5(11,22); +autonomous_5 (1 row) +--查看表结果 +MogDB=# select * from t2 order by a; +a | b +----+---- +1 | 2 +11 | 22 +(2 rows) ``` 上述例子,最后在回滚的事务块中执行包含自治事务的函数,直接说明了自治事务的特性,即主事务的回滚,不会影响自治事务已经提交的内容。 diff --git a/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/4-restrictions.md b/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/4-restrictions.md index a9412bc96fd83e9eab98f6a9bde196cdad48509b..527bdd8359968d3e9031440b96e60db035e78034 100644 --- a/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/4-restrictions.md +++ b/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/4-restrictions.md @@ -7,17 +7,21 @@ date: 2021-05-10 # 规格约束 -> ![img](https://cdn-mogdb.enmotech.com/docs-media/icon/icon-caution.gif) **注意:** 自治事务执行时,将会在后台启动自治事务session,我们可以通过max_concurrent_autonomous_transactions设置自治事务执行的最大并行数量,该参数取值范围为0~1024,默认值为10。当max_concurrent_autonomous_transactions参数设置为0时,自治事务将无法执行。 +> ![img](https://cdn-mogdb.enmotech.com/docs-media/icon/icon-caution.gif) **注意:** +> +> - 自治事务执行时,将会在后台启动自治事务session,我们可以通过max_concurrent_autonomous_transactions设置自治事务执行的最大并行数量,该参数取值范围为0~1024,默认值为10。 +> - 当max_concurrent_autonomous_transactions参数设置为0时,自治事务将无法执行。 +> - 自治事务新启session后,将使用默认session参数,不共享主session下对象(包括session级别变量,本地临时变量,全局临时表的数据等)。 - 触发器函数不支持自治事务。 ```sql CREATE TABLE test_trigger_des_tbl(id1 INT, id2 INT, id3 INT); - + CREATE OR REPLACE FUNCTION tri_insert_func() RETURNS TRIGGER AS $$ DECLARE - PRAGMA AUTONOMOUS_TRANSACTION; + PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3); RETURN NEW; @@ -25,28 +29,7 @@ date: 2021-05-10 $$ LANGUAGE PLPGSQL; ``` -- 自治事务不支持非顶层匿名块调用(仅支持顶层自治事务,包括存储过程、函数、匿名块)。 - - ```sql - create table t1(a int ,b text); - - DECLARE - --PRAGMA AUTONOMOUS_TRANSACTION; - BEGIN - DECLARE - PRAGMA AUTONOMOUS_TRANSACTION; - BEGIN - dbe_output.print_line('just use call.'); - insert into t1 values(1,'can you rollback!'); - END; - insert into t1 values(2,'I will rollback!'); - rollback; - END; - / - - select * from t1; - - ``` +- 自治事务不支持非顶层匿名块调用(仅支持顶层自治事务,包括存储过程、函数、匿名块)。 - 自治事务不支持ref_cursor参数传递。 @@ -56,71 +39,71 @@ date: 2021-05-10 insert into sections values(1); insert into sections values(1); insert into sections values(1); - + CREATE OR REPLACE function proc_sys_ref() return SYS_REFCURSOR IS declare - PRAGMA AUTONOMOUS_TRANSACTION; + PRAGMA AUTONOMOUS_TRANSACTION; C1 SYS_REFCURSOR; BEGIN - OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID; - return C1; + OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID; + return C1; END; / - - CREATE OR REPLACE PROCEDURE proc_sys_call() AS + + CREATE OR REPLACE PROCEDURE proc_sys_call() AS DECLARE - C1 SYS_REFCURSOR; - TEMP NUMBER(4); + C1 SYS_REFCURSOR; + TEMP NUMBER(4); BEGIN - c1 = proc_sys_ref(); - if c1%isopen then - raise notice '%','ok'; - end if; - - LOOP - FETCH C1 INTO TEMP; - raise notice '%',C1%ROWCOUNT; - EXIT WHEN C1%NOTFOUND; - END LOOP; + c1 = proc_sys_ref(); + if c1%isopen then + raise notice '%','ok'; + end if; + + LOOP + FETCH C1 INTO TEMP; + raise notice '%',C1%ROWCOUNT; + EXIT WHEN C1%NOTFOUND; + END LOOP; END; / - + select proc_sys_call(); - + CREATE OR REPLACE function proc_sys_ref(OUT C2 SYS_REFCURSOR, OUT a int) return SYS_REFCURSOR IS declare - PRAGMA AUTONOMOUS_TRANSACTION; + PRAGMA AUTONOMOUS_TRANSACTION; C1 SYS_REFCURSOR; BEGIN - OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID; - return C1; + OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID; + return C1; END; / - - CREATE OR REPLACE PROCEDURE proc_sys_call() AS + + CREATE OR REPLACE PROCEDURE proc_sys_call() AS DECLARE - C1 SYS_REFCURSOR; - TEMP NUMBER(4); - a int; + C1 SYS_REFCURSOR; + TEMP NUMBER(4); + a int; BEGIN - OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID; - c1 = proc_sys_ref(C1,a); - if c1%isopen then - raise notice '%','ok'; - end if; - - LOOP - FETCH C1 INTO TEMP; - raise notice '%',C1%ROWCOUNT; - EXIT WHEN C1%NOTFOUND; - END LOOP; + OPEN C1 FOR SELECT section_ID FROM sections ORDER BY section_ID; + c1 = proc_sys_ref(C1,a); + if c1%isopen then + raise notice '%','ok'; + end if; + + LOOP + FETCH C1 INTO TEMP; + raise notice '%',C1%ROWCOUNT; + EXIT WHEN C1%NOTFOUND; + END LOOP; END; / - + select proc_sys_call(); ``` @@ -129,3 +112,39 @@ date: 2021-05-10 - 不支持修改自治事务的隔离级别。 - 不支持自治事务返回集合类型(setof)。 + + ```sql + create table test_in (id int,a date); + create table test_main (id int,a date); + insert into test_main values (1111,'2021-01-01'),(2222,'2021-02-02'); + truncate test_in,test_main; + CREATE OR REPLACE FUNCTION autonomous_f_022(num1 int) RETURNS SETOF test_in + LANGUAGE plpgsql AS $$ + DECLARE + count int :=3; + test_row test_in%ROWTYPE; + PRAGMA AUTONOMOUS_TRANSACTION; + BEGIN + while true + loop + if count=3 then + null; + else + if count=2 then + insert into test_main values (count,'2021-03-03'); + goto pos1; + end if; + end if; + count=count-1; + end loop; + insert into test_main values (1000,'2021-04-04'); + <> + for test_row in select * from test_main + loop + return next test_row; + end loop; + return; + END; + $$ + ; + ``` \ No newline at end of file diff --git a/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/anonymous-block-supporting-autonomous-transaction.md b/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/anonymous-block-supporting-autonomous-transaction.md index fe19bd7820684e67d9b64f91b1c590e7fefd1917..6dc98603e38a9facdf4c63c8831657a3d0aaa763 100644 --- a/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/anonymous-block-supporting-autonomous-transaction.md +++ b/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/anonymous-block-supporting-autonomous-transaction.md @@ -10,20 +10,27 @@ date: 2021-10-15 自治事务可以在匿名块中定义,标识符为PRAGMA AUTONOMOUS_TRANSACTION,其余语法与创建匿名块语法相同,示例如下。 ```sql -create table t1(a int ,b text); +MogDB=# create table t1(a int ,b text); +CREATE TABLE START TRANSACTION; -DECLARE +DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN - dbe_output.print_line('just use call.'); + insert into t1 values(1,'you are so cute,will commit!'); END; / -insert into t1 values(1,'you will rollback!'); -rollback; +MogDB=# insert into t1 values(1,'you will rollback!'); +INSERT 0 1 +MogDB=# rollback; +ROLLBACK -select * from t1; +MogDB=# select * from t1; +a | b +---+------------------------------ +1 | you are so cute,will commit! +(1 row) ``` 上述例子,最后在回滚的事务块前执行包含自治事务的匿名块,也能直接说明了自治事务的特性,即主事务的回滚,不会影响自治事务已经提交的内容。