- Mastering PostgreSQL 12
- Hans Jürgen Sch?nig
- 183字
- 2021-08-20 10:00:21
Handling errors inside a transaction
It is not always the case that transactions are correct from beginning to end. Things might just go wrong for whatever reason. However, in PostgreSQL, only error-free transactions can be committed. The following listing shows a failing transaction, which errors out due to a division-by-zero error:
test=# BEGIN; BEGIN
test=# SELECT 1; ?column?
----------
1
(1 row)
test=# SELECT 1 / 0;
psql: ERROR: division by zero
test=# SELECT 1; psql: ERROR: current transaction is aborted, commands ignored until end of transaction block
test=# COMMIT; ROLLBACK
Note that division by zero did not work out.
In any proper database, an instruction similar to this will instantly error out and make the statement fail.
It is important to point out that PostgreSQL will error out, unlike MySQL, which is far less strict. 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 COMMIT. However, PostgreSQL will roll back the transaction because it is the only correct thing to be done at that point.
推薦閱讀
- 大數(shù)據(jù)戰(zhàn)爭(zhēng):人工智能時(shí)代不能不說(shuō)的事
- 大學(xué)計(jì)算機(jī)基礎(chǔ):基礎(chǔ)理論篇
- SCRATCH與機(jī)器人
- 輕松學(xué)Java Web開(kāi)發(fā)
- 網(wǎng)絡(luò)綜合布線技術(shù)
- Pig Design Patterns
- Photoshop CS3圖層、通道、蒙版深度剖析寶典
- Excel 2010函數(shù)與公式速查手冊(cè)
- Pentaho Analytics for MongoDB
- Linux Shell Scripting Cookbook(Third Edition)
- 玩機(jī)器人 學(xué)單片機(jī)
- Learning Cassandra for Administrators
- Cloudera Hadoop大數(shù)據(jù)平臺(tái)實(shí)戰(zhàn)指南
- EDA技術(shù)及其創(chuàng)新實(shí)踐(Verilog HDL版)
- Keras Reinforcement Learning Projects