当前位置:网站首页>Procedure in PostgreSQL supports transaction syntax (instance & Analysis)
Procedure in PostgreSQL supports transaction syntax (instance & Analysis)
2022-07-07 07:12:00 【mingjie73】
relevant
《Postgresql Source code (60) Transaction system summary 》
https://www.postgresql.org/docs/current/plpgsql-transactions.html
example 1:PROCEDURE Submission can be used internally 、 Rollback statement
drop table test1;
create table test1 (a int);
CREATE or replace PROCEDURE transaction_test1()
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO test1 (a) VALUES (2);
COMMIT;
INSERT INTO test1 (a) VALUES (3);
ROLLBACK;
END;
$$;
CALL transaction_test1();
select * from test1;
a
---
2
commit What does the statement do ?
Execute the upper level transaction state flow function :
- perform CommitTransactionCommand
- perform StartTransactionCommand
static int
exec_stmt_commit(PLpgSQL_execstate *estate, PLpgSQL_stmt_commit *stmt)
{
if (stmt->chain)
SPI_commit_and_chain();
else
{
SPI_commit();
SPI_start_transaction();
}
...
return PLPGSQL_RC_OK;
}
rollback What does the statement do ?
Execute the upper level transaction state flow function :
- perform AbortCurrentTransaction
- perform StartTransactionCommand
static int
exec_stmt_rollback(PLpgSQL_execstate *estate, PLpgSQL_stmt_rollback *stmt)
{
if (stmt->chain)
SPI_rollback_and_chain();
else
{
SPI_rollback();
SPI_start_transaction();
}
...
return PLPGSQL_RC_OK;
}
example 2:PROCEDURE If an internal error is reported, the executed statement will be rolled back automatically
drop table test1;
create table test1 (a int);
CREATE or replace PROCEDURE transaction_test1()
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO test1 (a) VALUES (2);
INSERT INTO test1 (a) VALUES (3);
RAISE division_by_zero;
END;
$$;
CALL transaction_test1();
ERROR: division_by_zero
CONTEXT: PL/pgSQL function transaction_test1() line 5 at RAISE
select * from test1;
a
---
(0 rows)
How transactions are rolled back ?
// Trigger ereport ERROR
RAISE division_by_zero;
// jump To :
PostgresMain
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
AbortCurrentTransaction()
go AbortCurrentTransaction Trigger rollback action
example 3:PROCEDURE Internal error reporting will not roll over the submitted statements
drop table test1;
create table test1 (a int);
CREATE or replace PROCEDURE transaction_test1()
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO test1 (a) VALUES (2);
COMMIT;
INSERT INTO test1 (a) VALUES (3);
RAISE division_by_zero;
END;
$$;
CALL transaction_test1();
select * from test1;
a
---
2
(1 row)
Reference examples 1 Analysis results of ,commit After the execution, a new transaction will start , The subsequent saving does not affect the previously committed transactions .
example 4:PROCEDURE contain EXCEPTION The statement block of does not support COMMIT
drop table test1;
create table test1 (a int);
CREATE or replace PROCEDURE transaction_test1()
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO test1 (a) VALUES (2);
COMMIT;
INSERT INTO test1 (a) VALUES (3);
RAISE division_by_zero;
EXCEPTION
WHEN division_by_zero THEN
RAISE NOTICE 'caught division_by_zero';
END;
$$;
CALL transaction_test1();
ERROR: cannot commit while a subtransaction is active
CONTEXT: PL/pgSQL function transaction_test1() line 4 at COMMIT
select * from test1;
a
---
(0 rows)
If you go EXCEPTION Statement block words , Will take the whole block Wrapped in a sub transaction , Execution is not supported in sub transactions commit.
exec_stmt_block
...
if (block->exceptions)
// Started a sub transaction
BeginInternalSubTransaction
PG_TRY()
exec_stmts
PG_CATCH()
// If there is any abnormality , End the whole sub transaction
RollbackAndReleaseCurrentSubTransaction
example 5:function It is atomic and does not support partial submission
drop table test1;
create table test1 (a int);
CREATE or replace function transaction_test1()
returns void
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO test1 (a) VALUES (2);
COMMIT;
INSERT INTO test1 (a) VALUES (3);
ROLLBACK;
END;
$$;
select transaction_test1();
ERROR: invalid transaction termination
CONTEXT: PL/pgSQL function transaction_test1() line 4 at COMMIT
reason :
Before executing the function , initialization SPI System
If the incoming fcinfo->context It's a call context Just configure nonatomic
plpgsql_call_handler
nonatomic = fcinfo->context
&& IsA(fcinfo->context, CallContext)
&& !castNode(CallContext, fcinfo->context)->atomic;
SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0))
_SPI_current->atomic = (options & SPI_OPT_NONATOMIC ? false : true);
If it is call procedure sentence
_SPI_current->atomic = false;
So in the implementation exec_stmt_commit when , No mistake. .
exec_stmt_commit
SPI_commit
_SPI_commit
if (_SPI_current->atomic)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
errmsg("invalid transaction termination")));
If it is function It will report an error and quit .
边栏推荐
- Precise space-time travel flow regulation system - ultra-high precision positioning system based on UWB
- Multithreading and high concurrency (9) -- other synchronization components of AQS (semaphore, reentrantreadwritelock, exchanger)
- .net core 访问不常见的静态文件类型(MIME 类型)
- 请教一下,监听pgsql ,怎样可以监听多个schema和table
- MySQL的主从复制原理
- DHCP路由器工作原理
- Config distributed configuration center
- Reflection (II)
- Answer to the second stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
- JDBC database connection pool usage problem
猜你喜欢

DHCP路由器工作原理

关于二进制无法精确表示小数

JDBC database connection pool usage problem

Non empty verification of collection in SQL

LC 面试题 02.07. 链表相交 & LC142. 环形链表II

.net 5 FluentFTP连接FTP失败问题:This operation is only allowed using a successfully authenticated context

计算机服务中缺失MySQL服务

Kuboard无法发送邮件和钉钉告警问题解决

Communication between non parent and child components

Four goals for the construction of intelligent safety risk management and control platform for hazardous chemical enterprises in Chemical Industry Park
随机推荐
Jetpack compose is much more than a UI framework~
2018 Jiangsu Vocational College skills competition vocational group "information security management and evaluation" competition assignment
freeswitch拨打分机号源代码跟踪
Use of completable future
The latest trends of data asset management and data security at home and abroad
2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书
Anr principle and Practice
Graduation design game mall
Please answer the questions about database data transfer
[Luogu p1971] rabbit and egg game (bipartite game)
transform-origin属性详解
leetcode 509. Fibonacci number
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
main函数在import语句中的特殊行为
LVS+Keepalived(DR模式)学习笔记
LC 面试题 02.07. 链表相交 & LC142. 环形链表II
计算机服务中缺失MySQL服务
How to model and simulate the target robot [mathematical / control significance]
请教一下,监听pgsql ,怎样可以监听多个schema和table
弹性布局(二)