更新时间:2024-04-26 GMT+08:00
分享

匿名块支持自治事务

自治事务可以在匿名块中定义,标识符为PRAGMA AUTONOMOUS_TRANSACTION,其余语法与创建匿名块语法相同,示例如下。

gaussdb=# create table t1(a int ,b text);
NOTICE:  The 'DISTRIBUTE BY' clause is not specified. Using 'a' as the distribution column by default.
HINT:  Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column.
CREATE TABLE

gaussdb=# START TRANSACTION;
START TRANSACTION
gaussdb=# DECLARE 
	PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
	dbe_output.print_line('just use call.');
	insert into t1 values(1,'you are so cute,will commit!');
END;
/
just use call.
ANONYMOUS BLOCK EXECUTE
gaussdb=# insert into t1 values(1,'you will rollback!');
INSERT 0 1
gaussdb=# rollback;
ROLLBACK

gaussdb=# select * from t1;
 a |              b
---+------------------------------
 1 | you are so cute,will commit!
(1 row)

上述例子,最后在回滚的事务块前执行包含自治事务的匿名块,也能直接说明了自治事务的特性,即主事务的回滚,不会影响自治事务已经提交的内容。

相关文档