当前位置:网站首页>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 .
边栏推荐
- Graduation design game mall
- LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
- Bus message bus
- Asynchronous components and suspend (in real development)
- . Net 5 fluentftp connection FTP failure problem: this operation is only allowed using a successfully authenticated context
- LVS+Keepalived(DR模式)学习笔记
- Reflection (II)
- Algorithm --- bit count (kotlin)
- Paranoid unqualified company
- 2022年全国所有A级景区数据(13604条)
猜你喜欢
Kuboard无法发送邮件和钉钉告警问题解决
Asynchronous components and suspend (in real development)
$refs: get the element object or sub component instance in the component:
This article introduces you to the characteristics, purposes and basic function examples of static routing
组件的嵌套和拆分
transform-origin属性详解
Basic introduction of JWT
Use of completable future
IP address
Take you to brush (niuke.com) C language hundred questions (the first day)
随机推荐
Unity3d learning notes
非父子组件的通信
计算机服务中缺失MySQL服务
Paranoid unqualified company
Basic process of network transmission using tcp/ip four layer model
关于数据库数据转移的问题,求各位解答下
Maze games based on JS
MySQL的主从复制原理
Freeswitch dials extension number source code tracking
Data of all class a scenic spots in China in 2022 (13604)
父组件传递给子组件:Props
Select the product attribute pop-up box to pop up the animation effect from the bottom
toRefs API 与 toRef Api
Bus message bus
Torefs API and toref API
Détailler le bleu dans les tâches de traduction automatique
Use of completable future
This article introduces you to the characteristics, purposes and basic function examples of static routing
Tujia, muniao, meituan... Home stay summer war will start
Please tell me how to monitor multiple schemas and tables by listening to PgSQL