- Mastering PostgreSQL 9.6
- Hans Jurgen Schonig
- 279字
- 2021-07-09 19:57:11
Making use of savepoints
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=# ROLLBACK TO SAVEPOINT a;
ROLLBACK
test=# SELECT 3;
?column?
----------
3
(1 row)
test=# COMMIT;
COMMIT
After the first SELECT clause, I decided to create a SAVEPOINT to make sure that the application can always return to this point inside the transaction. As you can see, a SAVEPOINT has a name, which is referred to later.
After returning to 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 that.
If you want to remove a savepoint from inside a transaction, there is 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 that 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.
- 工業(yè)機(jī)器人虛擬仿真實(shí)例教程:KUKA.Sim Pro(全彩版)
- 精通MATLAB圖像處理
- 數(shù)據(jù)庫(kù)原理與應(yīng)用技術(shù)學(xué)習(xí)指導(dǎo)
- 電腦上網(wǎng)直通車
- 人工智能工程化:應(yīng)用落地與中臺(tái)構(gòu)建
- SAP Business Intelligence Quick Start Guide
- 氣動(dòng)系統(tǒng)裝調(diào)與PLC控制
- 悟透AutoCAD 2009案例自學(xué)手冊(cè)
- 空間機(jī)械臂建模、規(guī)劃與控制
- 人工智能:語(yǔ)言智能處理
- Hands-On Dashboard Development with QlikView
- 步步驚“芯”
- 手把手教你學(xué)Photoshop CS3
- 網(wǎng)管員世界2009超值精華本
- 基于元胞自動(dòng)機(jī)的人群疏散系統(tǒng)建模與分析