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

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.

主站蜘蛛池模板: 石门县| 镇雄县| 乌兰察布市| 太和县| 广德县| 休宁县| 宁河县| 巴林右旗| 乌鲁木齐市| 卢龙县| 鲜城| 崇左市| 武邑县| 开鲁县| 瑞丽市| 通辽市| 福安市| 沙湾县| 沧源| 高密市| 皮山县| 全椒县| 临湘市| 旅游| 乌拉特中旗| 安塞县| 健康| 包头市| 合阳县| 禄劝| 德令哈市| 彰化市| 塔河县| 宁化县| 镇坪县| 靖远县| 邢台市| 台东县| 江城| 通江县| 白银市|