云数据库 GAUSSDB-事务管理:示例
时间:2024-11-02 18:45:46
示例
- 示例1:支持在PL/SQL的存储过程内使用COMMIT/ROLLBACK,后续示例依赖此用例。
gaussdb=# DROP TABLE IF EXISTS EXAMPLE1; NOTICE: table "example1" does not exist, skipping DROP TABLE gaussdb=# CREATE TABLE EXAMPLE1(COL1 INT); CREATE TABLE gaussdb=# CREATE OR REPLACE PROCEDURE TRANSACTION_EXAMPLE() AS BEGIN FOR i IN 0..20 LOOP INSERT INTO EXAMPLE1(COL1) VALUES (i); IF i % 2 = 0 THEN COMMIT; ELSE ROLLBACK; END IF; END LOOP; END; / CREATE PROCEDURE gaussdb=# CALL TRANSACTION_EXAMPLE(); transaction_example --------------------- (1 row)
- 示例2:
支持含有EXCEPTION的存储过程使用COMMIT/ROLLBACK。
支持在存储过程的EXCEPTION语句内使用COMMIT/ROLLBACK。
支持DDL在COMMIT/ROLLBACK后的提交/回滚。
gaussdb=# CREATE OR REPLACE PROCEDURE TEST_COMMIT_INSERT_EXCEPTION_ROLLBACK() AS BEGIN DROP TABLE IF EXISTS TEST_COMMIT; CREATE TABLE TEST_COMMIT(A INT, B INT); INSERT INTO TEST_COMMIT SELECT 1, 1; COMMIT; CREATE TABLE TEST_ROLLBACK(A INT, B INT); RAISE EXCEPTION 'RAISE EXCEPTION AFTER COMMIT'; EXCEPTION WHEN OTHERS THEN INSERT INTO TEST_COMMIT SELECT 2, 2; ROLLBACK; END; / CREATE PROCEDURE gaussdb=# CALL TEST_COMMIT_INSERT_EXCEPTION_ROLLBACK(); NOTICE: table "test_commit" does not exist, skipping CONTEXT: SQL statement "DROP TABLE IF EXISTS TEST_COMMIT" PL/pgSQL function test_commit_insert_exception_rollback() line 3 at SQL statement test_commit_insert_exception_rollback --------------------------------------- (1 row)
- 示例3:支持在事务块里调用含有COMMIT/ROLLBACK的存储过程,即通过BEGIN/START/END等开启控制的外部事务。
gaussdb=# BEGIN; -- TEST_COMMIT_INSERT_EXCEPTION_ROLLBACK定义见示例2 CALL TEST_COMMIT_INSERT_EXCEPTION_ROLLBACK(); END; test_commit_insert_exception_rollback --------------------------------------- (1 row) COMMIT
- 示例4:支持多数PL/SQL的上下文和语句内调用COMMIT/ROLLBACK,包括常用的IF/FOR/CURSOR LOOP/WHILE。
gaussdb=# CREATE OR REPLACE PROCEDURE TEST_COMMIT2() IS BEGIN DROP TABLE IF EXISTS TEST_COMMIT; CREATE TABLE TEST_COMMIT(A INT); FOR I IN REVERSE 3..0 LOOP INSERT INTO TEST_COMMIT SELECT I; COMMIT; END LOOP; FOR I IN REVERSE 2..4 LOOP UPDATE TEST_COMMIT SET A=I; COMMIT; END LOOP; EXCEPTION WHEN OTHERS THEN INSERT INTO TEST_COMMIT SELECT 4; COMMIT; END; / CREATE PROCEDURE gaussdb=# CALL TEST_COMMIT2(); test_commit2 -------------- (1 row)
- 示例5:支持存储过程返回值与简单表达式计算。
gaussdb=# CREATE OR REPLACE PROCEDURE exec_func3(RET_NUM OUT INT) AS BEGIN RET_NUM := 1+1; COMMIT; END; / CREATE PROCEDURE gaussdb=# CALL exec_func3(''); ret_num --------- 2 (1 row) gaussdb=# CREATE OR REPLACE PROCEDURE exec_func4(ADD_NUM IN INT) AS SUM_NUM INT; BEGIN SUM_NUM := ADD_NUM + exec_func3(); COMMIT; END; / CREATE PROCEDURE gaussdb=# CALL exec_func4(1); exec_func4 ------------ (1 row)
- 示例6:支持存储过程内GUC参数的回滚提交。
gaussdb=# SET explain_perf_mode='normal'; SET gaussdb=# SHOW explain_perf_mode; explain_perf_mode ------------------- normal (1 row) gaussdb=# SHOW enable_force_vector_engine; enable_force_vector_engine ---------------------------- off (1 row) gaussdb=# CREATE OR REPLACE PROCEDURE GUC_ROLLBACK() AS BEGIN SET enable_force_vector_engine = on; COMMIT; SET explain_perf_mode TO pretty; ROLLBACK; END; / CREATE PROCEDURE gaussdb=# CALL GUC_ROLLBACK(); guc_rollback -------------- (1 row) gaussdb=# SHOW explain_perf_mode; explain_perf_mode ------------------- normal (1 row) gaussdb=# SHOW enable_force_vector_engine; enable_force_vector_engine ---------------------------- on (1 row) gaussdb=# SET enable_force_vector_engine = off; SET
- 示例7:不允许Trigger的存储过程包含commit/rollback语句,或调用带有commit/rollback语句的存储过程。
gaussdb=# CREATE OR REPLACE FUNCTION FUNCTION_TRI_EXAMPLE2() RETURN TRIGGER AS EXP INT; BEGIN FOR i IN 0..20 LOOP INSERT INTO EXAMPLE1(col1) VALUES (i); IF i % 2 = 0 THEN COMMIT; ELSE ROLLBACK; END IF; END LOOP; SELECT COUNT(*) FROM EXAMPLE1 INTO EXP; END; / CREATE FUNCTION gaussdb=# CREATE TRIGGER TRIGGER_EXAMPLE AFTER DELETE ON EXAMPLE1 FOR EACH ROW EXECUTE PROCEDURE FUNCTION_TRI_EXAMPLE2(); CREATE TRIGGER gaussdb=# DELETE FROM EXAMPLE1; ERROR: Can not commit/rollback if it's atomic is true: can not use commit rollback in Complex SQL CONTEXT: PL/pgSQL function function_tri_example2() line 7 at COMMIT
- 示例8:不支持带有IMMUTABLE以及SHIPPABLE的存储过程调用commit/rollback,或调用带有commit/rollback语句的存储过程。
gaussdb=# CREATE OR REPLACE PROCEDURE TRANSACTION_EXAMPLE1() IMMUTABLE AS EXP INT; BEGIN FOR i IN 0..20 LOOP SELECT COUNT(*) FROM EXAMPLE1 INTO EXP; IF i % 2 = 0 THEN COMMIT; ELSE ROLLBACK; END IF; END LOOP; END; / CREATE PROCEDURE gaussdb=#CALL TRANSACTION_EXAMPLE1(); ERROR: Can not commit/rollback if it's atomic is true: commit/rollback/savepoint is not allowed in a non-volatile function CONTEXT: PL/pgSQL function transaction_example1() line 7 at COMMIT
- 示例9:不支持存储过程中任何变量的提交,包括存储过程内声明的变量或者传入的参数。
gaussdb=# CREATE OR REPLACE PROCEDURE TRANSACTION_EXAMPLE2(EXP_OUT OUT INT) AS EXP INT:=-1; BEGIN EXP_OUT := 0; EXP := 0; COMMIT; DBE_OUTPUT.PRINT_LINE('EXP IS:'||EXP); DBE_OUTPUT.PRINT_LINE('EXP_OUT IS:'||EXP_OUT); EXP := 1; EXP_OUT := 1; ROLLBACK; DBE_OUTPUT.PRINT_LINE('EXP IS:'||EXP); DBE_OUTPUT.PRINT_LINE('EXP_OUT IS:'||EXP_OUT); END; / CREATE PROCEDURE gaussdb=# CALL TRANSACTION_EXAMPLE2(1); EXP IS:0 EXP_OUT IS:0 EXP IS:1 EXP_OUT IS:1 exp_out --------- 1 (1 row)
- 示例10:不支持出现在SQL中的调用(除了Select Procedure)。
gaussdb=# CREATE OR REPLACE FUNCTION TRANSACTION_EXAMPLE3() RETURN INT IS BEGIN FOR i IN 0..20 LOOP INSERT INTO EXAMPLE1 (col1) VALUES (i); IF i % 2 = 0 THEN EXECUTE IMMEDIATE 'COMMIT'; ELSE EXECUTE IMMEDIATE 'ROLLBACK'; END IF; END LOOP; RETURN 1; END; / CREATE PROCEDURE gaussdb=# SELECT * FROM example1 WHERE col1=TRANSACTION_EXAMPLE3(); ERROR: cannot call transaction statements in EXECUTE IMMEDIATE statement. CONTEXT: PL/pgSQL function transaction_example3() line 6 at EXECUTE statement
- 示例11:存储过程头带有GUC参数设置的不允许调用commit/rollback语句。
gaussdbo=# CREATE OR REPLACE PROCEDURE TRANSACTION_EXAMPLE4() SET ARRAY_NULLS TO "ON" AS BEGIN FOR i IN 0..20 LOOP INSERT INTO EXAMPLE1 (col1) VALUES (i); IF i % 2 = 0 THEN COMMIT; ELSE ROLLBACK; END IF; END LOOP; END; / CREATE PROCEDURE gaussdb=# CALL TRANSACTION_EXAMPLE4(); ERROR: Can not commit/rollback if it's atomic is true: transaction statement in store procedure with GUC setting in option clause is not supported CONTEXT: PL/pgSQL function transaction_example4() line 6 at COMMIT
- 示例12:游标open的对象不允许为带有commit/rollback语句的存储过程。
gaussdb=# CREATE OR REPLACE PROCEDURE TRANSACTION_EXAMPLE5(INTIN IN INT, INTOUT OUT INT) AS BEGIN INTOUT := INTIN + 1; COMMIT; END; / CREATE PROCEDURE gaussdb=# CREATE OR REPLACE PROCEDURE TRANSACTION_EXAMPLE6() AS CURSOR CURSOR1(EXPIN INT) IS SELECT TRANSACTION_EXAMPLE5(EXPIN); INTEXP INT; BEGIN FOR i IN 0..20 LOOP OPEN CURSOR1(i); FETCH CURSOR1 INTO INTEXP; INSERT INTO EXAMPLE1(COL1) VALUES (INTEXP); IF i % 2 = 0 THEN COMMIT; ELSE ROLLBACK; END IF; CLOSE CURSOR1; END LOOP; END; / CREATE PROCEDURE gaussdb=# CALL TRANSACTION_EXAMPLE6(); ERROR: Can not commit/rollback if it's atomic is true: transaction statement in store procedure used as cursor is not supported CONTEXT: PL/pgSQL function transaction_example5(integer) line 4 at COMMIT referenced column: transaction_example5 PL/pgSQL function transaction_example6() line 8 at FETCH
- 示例13:不支持CURSOR/EXECUTE语句,以及各类表达式内调用COMMIT/ROLLBACK。
gaussdb=# CREATE OR REPLACE PROCEDURE exec_func1() AS BEGIN CREATE TABLE TEST_exec(A INT); COMMIT; END; / CREATE PROCEDURE gaussdb=# CREATE OR REPLACE PROCEDURE exec_func2() AS BEGIN EXECUTE exec_func1(); COMMIT; END; / CREATE PROCEDURE gaussdb=# CALL exec_func2(); ERROR: Can not commit/rollback if it's atomic is true: transaction statement in store procedure used as a expression is not supported CONTEXT: PL/pgSQL function exec_func1() line 4 at COMMIT PL/pgSQL function exec_func2() line 3 at EXECUTE statement
support.huaweicloud.com/centralized-devg-v8-gaussdb/gaussdb-42-0816.html
看了此文的人还看了
CDN加速
GaussDB
文字转换成语音
免费的服务器
如何创建网站
域名网站购买
私有云桌面
云主机哪个好
域名怎么备案
手机云电脑
SSL证书申请
云点播服务器
免费OCR是什么
电脑云桌面
域名备案怎么弄
语音转文字
文字图片识别
云桌面是什么
网址安全检测
网站建设搭建
国外CDN加速
SSL免费证书申请
短信批量发送
图片OCR识别
云数据库MySQL
个人域名购买
录音转文字
扫描图片识别文字
OCR图片识别
行驶证识别
虚拟电话号码
电话呼叫中心软件
怎么制作一个网站
Email注册网站
华为VNC
图像文字识别
企业网站制作
个人网站搭建
华为云计算
免费租用云托管
云桌面云服务器
ocr文字识别免费版
HTTPS证书申请
图片文字识别转换
国外域名注册商
使用免费虚拟主机
云电脑主机多少钱
鲲鹏云手机
短信验证码平台
OCR图片文字识别
SSL证书是什么
申请企业邮箱步骤
免费的企业用邮箱
云免流搭建教程
域名价格
推荐文章
- GaussDB考试_GaussDB数据库考试_高斯数据库考试_华为云
- GaussDB视频教程_gaussdb查看表结构语句_高斯数据库视频教程_华为云
- 分布式云原生集合示例_华为云分布式云原生_华为云UCS集合示例
- GaussDB认证_GaussDB考试认证_GaussDB认证指南-华为云
- GaussDB学习_gaussdb教程_高斯数据库学习_华为云
- GaussDB数据库考试_GaussDB认证_高斯数据库考试_华为云
- GaussDB发展_gaussdb数据库_高斯数据库发展_华为云
- GaussDB下载免费版_GaussDB怎么样_高斯数据库下载
- GaussDB培训_GaussDB教程_高斯数据库培训-华为云
- GaussDB用法_GaussDB数据库使用方法_高斯数据库如何使用_华为云