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

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.

主站蜘蛛池模板: 平远县| 禹州市| 石渠县| 泰来县| 莎车县| 闻喜县| 郑州市| 茂名市| 鄢陵县| 孙吴县| 化德县| 平罗县| 义乌市| 铜梁县| 南平市| 甘南县| 贵溪市| 子洲县| 淮安市| 北碚区| 余江县| 阿克苏市| 梁山县| 科技| 清远市| 南澳县| 同仁县| 临洮县| 宜宾市| 武胜县| 满洲里市| 开鲁县| 闵行区| 武汉市| 合阳县| 松桃| 阿拉善右旗| 亳州市| 嵊泗县| 镶黄旗| 陕西省|