From fdee2570120cde997bfca2bfbb595e520d49a628 Mon Sep 17 00:00:00 2001 From: spaceoddity91719 Date: Thu, 28 Apr 2022 15:03:01 +0800 Subject: [PATCH 1/3] =?UTF-8?q?update:=E5=BC=80=E5=8F=91=E8=80=85=E6=8C=87?= =?UTF-8?q?=E5=8D=97-=E8=87=AA=E6=B2=BB=E4=BA=8B=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ction-supporting-autonomous-transaction.md | 94 ++++------- .../autonomous-transaction/4-restrictions.md | 151 ++++++++++-------- ...block-supporting-autonomous-transaction.md | 8 +- 3 files changed, 122 insertions(+), 131 deletions(-) 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 862fc7a3..7feb3cb0 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,45 @@ --- -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 - PRAGMA AUTONOMOUS_TRANSACTION; +--建表 +create table t2(a int, b int); +insert into t2 values(1,2); +select * from t2; + +--创建包含自治事务的存储过程 +CREATE OR REPLACE PROCEDURE autonomous_4(a int, b int) AS +DECLARE + num3 int := a; + num4 int := b; + PRAGMA AUTONOMOUS_TRANSACTION; +BEGIN + insert into t2 values(num3, num4); + +END; +/ +--创建调用自治事务存储过程的普通存储过程 +CREATE OR REPLACE PROCEDURE autonomous_5(a int, b int) AS +DECLARE 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(666, 666); + autonomous_4(a,b); + rollback; 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 -(1 row) +/ +--调用普通存储过程 +select autonomous_5(11,22); +--查看表结果 +select * from t2 order by a; ``` 上述例子,最后在回滚的事务块中执行包含自治事务的函数,直接说明了自治事务的特性,即主事务的回滚,不会影响自治事务已经提交的内容。 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 a9412bc9..dcc987c8 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,41 @@ 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; + $$ + ; + ``` + + 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 fe19bd78..e5b15108 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 @@ -13,11 +13,11 @@ date: 2021-10-15 create table t1(a int ,b text); START TRANSACTION; -DECLARE - PRAGMA AUTONOMOUS_TRANSACTION; +DECLARE + PRAGMA AUTONOMOUS_TRANSACTION; BEGIN - dbe_output.print_line('just use call.'); - insert into t1 values(1,'you are so cute,will commit!'); + + insert into t1 values(1,'you are so cute,will commit!'); END; / insert into t1 values(1,'you will rollback!'); -- Gitee From d615556b5c100a42768e7eca7a634ee3298f64f6 Mon Sep 17 00:00:00 2001 From: spaceoddity91719 Date: Thu, 28 Apr 2022 15:09:00 +0800 Subject: [PATCH 2/3] fix markdownlint --- ...nction-supporting-autonomous-transaction.md | 18 +++++++++--------- .../autonomous-transaction/4-restrictions.md | 4 +--- ...-block-supporting-autonomous-transaction.md | 6 +++--- 3 files changed, 13 insertions(+), 15 deletions(-) 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 7feb3cb0..b2e82cc1 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 @@ -18,22 +18,22 @@ select * from t2; --创建包含自治事务的存储过程 CREATE OR REPLACE PROCEDURE autonomous_4(a int, b int) AS DECLARE - num3 int := a; - num4 int := b; - PRAGMA AUTONOMOUS_TRANSACTION; + num3 int := a; + num4 int := b; + PRAGMA AUTONOMOUS_TRANSACTION; BEGIN - insert into t2 values(num3, num4); - + insert into t2 values(num3, num4); + END; / --创建调用自治事务存储过程的普通存储过程 CREATE OR REPLACE PROCEDURE autonomous_5(a int, b int) AS DECLARE BEGIN - - insert into t2 values(666, 666); - autonomous_4(a,b); - rollback; + + insert into t2 values(666, 666); + autonomous_4(a,b); + rollback; END; / --调用普通存储过程 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 dcc987c8..527bdd83 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 @@ -147,6 +147,4 @@ date: 2021-05-10 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 e5b15108..e10880db 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 @@ -14,10 +14,10 @@ create table t1(a int ,b text); START TRANSACTION; DECLARE - PRAGMA AUTONOMOUS_TRANSACTION; + PRAGMA AUTONOMOUS_TRANSACTION; BEGIN - - insert into t1 values(1,'you are so cute,will commit!'); + + insert into t1 values(1,'you are so cute,will commit!'); END; / insert into t1 values(1,'you will rollback!'); -- Gitee From 0acc3e5975c15f7901a7bcfc54bff4f2372bc122 Mon Sep 17 00:00:00 2001 From: spaceoddity91719 Date: Thu, 28 Apr 2022 17:33:12 +0800 Subject: [PATCH 3/3] fix review comment --- ...ction-supporting-autonomous-transaction.md | 21 ++++++++++++++----- ...block-supporting-autonomous-transaction.md | 15 +++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) 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 b2e82cc1..e4041072 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 @@ -11,9 +11,13 @@ date: 2021-05-10 ```sql --建表 -create table t2(a int, b int); -insert into t2 values(1,2); -select * from t2; +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 @@ -37,9 +41,16 @@ BEGIN END; / --调用普通存储过程 -select autonomous_5(11,22); +MogDB=# select autonomous_5(11,22); +autonomous_5 +(1 row) --查看表结果 -select * from t2 order by a; +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/anonymous-block-supporting-autonomous-transaction.md b/product/zh/docs-mogdb/v3.0/developer-guide/autonomous-transaction/anonymous-block-supporting-autonomous-transaction.md index e10880db..6dc98603 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,7 +10,8 @@ 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 @@ -20,10 +21,16 @@ BEGIN 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) ``` 上述例子,最后在回滚的事务块前执行包含自治事务的匿名块,也能直接说明了自治事务的特性,即主事务的回滚,不会影响自治事务已经提交的内容。 -- Gitee