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

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 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 a COMMIT. However, PostgreSQL will roll back the transaction because it is the only correct thing to be done at this point.

主站蜘蛛池模板: 中西区| 炎陵县| 阳原县| 永顺县| 额敏县| 顺平县| 忻州市| 长兴县| 桦南县| 翼城县| 依兰县| 莱芜市| 六安市| 开化县| 呼玛县| 中阳县| 稷山县| 奇台县| 志丹县| 平舆县| 绥中县| 巴林右旗| 红桥区| 镇赉县| 城市| 宾阳县| 屯留县| 镇康县| 普兰店市| 清苑县| 县级市| 托里县| 鄱阳县| 江永县| 安岳县| 偏关县| 武陟县| 尼玛县| 株洲县| 卓尼县| 平顺县|