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

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.

主站蜘蛛池模板: 铁岭县| 广宁县| 南岸区| 南漳县| 皋兰县| 从江县| 承德市| 清水河县| 平和县| 高雄市| 阿鲁科尔沁旗| 修文县| 凯里市| 翁源县| 杭锦后旗| 高密市| 集安市| 灯塔市| 阿合奇县| 吴桥县| 黔南| 泾川县| 永州市| 土默特右旗| 长白| 德阳市| 固安县| 沂南县| 五峰| 麻栗坡县| 抚州市| 萨迦县| 临海市| 育儿| 监利县| 资讯 | 遵化市| 盘山县| 城步| 南通市| 米易县|