- Mastering PostgreSQL 9.6
- Hans Jurgen Schonig
- 170字
- 2021-07-09 19:57:11
Handling errors inside a transaction
It is not always the case that transactions are correct from beginning to end. However, in PostgreSQL, only error-free transactions can be committed. Here is what happens:
test=# BEGIN;
BEGIN
test=# SELECT 1;
?column?
----------
1
(1 row)
test=# SELECT 1 / 0;
ERROR: division by zero
test=# SELECT 1;
ERROR: current transaction is aborted, commands ignored until end of transaction block
test=# COMMIT;
ROLLBACK
Note that the division by zero did not work out.
In any proper database, an instruction like this will instantly error-out and make the statement fail.
It is important to point out that PostgreSQL will error-out, unlike MySQL, which does not seem to have a problem with a mathematically wrong result.
After an error has occurred, no more instructions will be accepted even if those instructions are semantically and syntactically correct. It is still possible to issue a COMMIT. However, PostgreSQL will roll back the transaction because it is the only thing at this point that can still be done.
推薦閱讀
- Internet接入·網(wǎng)絡(luò)安全
- LabVIEW虛擬儀器從入門到測控應(yīng)用130例
- 協(xié)作機(jī)器人技術(shù)及應(yīng)用
- ServiceNow Cookbook
- 計(jì)算機(jī)網(wǎng)絡(luò)應(yīng)用基礎(chǔ)
- Photoshop CS3圖層、通道、蒙版深度剖析寶典
- 網(wǎng)絡(luò)管理工具實(shí)用詳解
- 自動(dòng)化生產(chǎn)線安裝與調(diào)試(三菱FX系列)(第二版)
- 重估:人工智能與賦能社會(huì)
- 步步驚“芯”
- MongoDB 4 Quick Start Guide
- 菜鳥起飛五筆打字高手
- Mastering SQL Server 2014 Data Mining
- 服務(wù)科學(xué)概論
- Arduino創(chuàng)意機(jī)器人入門:基于Mixly