官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 新泰市| 酒泉市| 甘南县| 七台河市| 柳河县| 台北市| 五原县| 青州市| 河曲县| 吐鲁番市| 什邡市| 迭部县| 江城| 吉安市| 建瓯市| 偏关县| 广河县| 瑞安市| 珠海市| 乌拉特后旗| 五指山市| 深泽县| 工布江达县| 永川市| 宽甸| 通化市| 客服| 临澧县| 来安县| 和静县| 天柱县| 杭锦旗| 昆山市| 怀来县| 浠水县| 泗阳县| 肇源县| 灵武市| 彭水| 呈贡县| 兴宁市|