- Mastering PostgreSQL 10
- Hans Jürgen Sch?nig
- 298字
- 2021-06-30 19:03:49
Making use of SAVEPOINT
In professional applications, it can be pretty hard to write reasonably long transactions without ever encountering a single error. To solve the problem, users can utilize something called SAVEPOINT. As the name indicates, it is a safe place inside a transaction that the application can return to in the event things go terribly wrong. Here is an example:
test=# BEGIN; BEGIN
test=# SELECT 1; ?column? ---------- 1 (1 row)
test=# SAVEPOINT a; SAVEPOINT
test=# SELECT 2 / 0; ERROR: division by zero
test=# SELECT 2; ERROR: current transaction is aborted, commands ignored until end of transaction block
test=# ROLLBACK TO SAVEPOINT a; ROLLBACK
test=# SELECT 3; ?column? ---------- 3
(1 row)
test=# COMMIT; COMMIT
After the first SELECT clause, I decided to create SAVEPOINT to make sure that the application can always return to this point inside the transaction. As you can see, SAVEPOINT has a name, which is referred to later.
After returning the savepoint called a, the transaction can proceed normally. The code has jumped back before the error, so everything is fine.
The number of savepoints inside a transaction is practically unlimited. We have seen customers with over 250,000 savepoints in a single operation. PostgreSQL can easily handle this.
If you want to remove a SAVEPOINT from inside a transaction, there is a RELEASE SAVEPOINT:
test=# \h RELEASE SAVEPOINT
Command: RELEASE SAVEPOINT
Description: Destroy a previously defined SAVEPOINT
Syntax:
RELEASE [ SAVEPOINT ] savepoint_name
Many people ask, what will happen if you try to reach a savepoint after a transaction has ended? The answer is, the life of a savepoint ends as soon as the transaction ends. In other words, there is no way to return to a certain point in time after the transactions have been completed.
- 集成架構中型系統
- Microsoft Dynamics CRM Customization Essentials
- 網頁編程技術
- Visual Basic從初學到精通
- WordPress Theme Development Beginner's Guide(Third Edition)
- The Python Workshop
- Grome Terrain Modeling with Ogre3D,UDK,and Unity3D
- 人工智能:語言智能處理
- 機器人人工智能
- 手機游戲策劃設計
- 生物3D打印:從醫療輔具制造到細胞打印
- 基于RPA技術財務機器人的應用與研究
- PowerPoint 2003中文演示文稿5日通
- 軟件質量管理實踐
- 實戰突擊