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

  • Mastering PostgreSQL 9.6
  • Hans Jurgen Schonig
  • 170字
  • 2021-07-09 19:57:11

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.

主站蜘蛛池模板: 临漳县| 玛多县| 中牟县| 木兰县| 赣榆县| 乌拉特前旗| 湖南省| 馆陶县| 铜梁县| 湘阴县| 舞钢市| 东台市| 南丹县| 万山特区| 永州市| 湘潭市| 隆子县| 景宁| 金沙县| 古交市| 威远县| 梓潼县| 鹰潭市| 密云县| 红桥区| 桑日县| 互助| 宜兰市| 武威市| 武平县| 饶阳县| 大田县| 九龙城区| 彭州市| 峡江县| 古蔺县| 广南县| 个旧市| 丰都县| 红安县| 凤台县|