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

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.

主站蜘蛛池模板: 五原县| 霍州市| 万荣县| 苏尼特左旗| 化德县| 连江县| 沽源县| 博客| 温州市| 乐至县| 嘉黎县| 鄂伦春自治旗| 江都市| 兖州市| 临泉县| 灵丘县| 雷州市| 广宗县| 长治市| 榕江县| 宁夏| 许昌县| 四子王旗| 肥城市| 呼伦贝尔市| 普兰县| 疏附县| 信阳市| 宝清县| 宝鸡市| 宁明县| 衡阳市| 朝阳市| 灵宝市| 通州市| 小金县| 鄂州市| 改则县| 广州市| 马龙县| 司法|