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

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.

主站蜘蛛池模板: 彰武县| 南阳市| 乐东| 黄大仙区| 法库县| 广元市| 南川市| 西平县| 观塘区| 远安县| 无锡市| 兴安县| 安西县| 宕昌县| 孟连| 遂昌县| 乌什县| 巧家县| 通化县| 合山市| 株洲市| 佛冈县| 府谷县| 宝丰县| 澜沧| 讷河市| 高要市| 鄱阳县| 沁源县| 顺义区| 上蔡县| 永寿县| 会东县| 抚顺市| 扬中市| 鄂尔多斯市| 商城县| 昌邑市| 卫辉市| 湘潭市| 东至县|