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

  • 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.

主站蜘蛛池模板: 阜城县| 都昌县| 县级市| 体育| 牡丹江市| 津市市| 昌黎县| 海宁市| 大庆市| 怀来县| 柳林县| 东台市| 邹城市| 江永县| 洪雅县| 丰都县| 吉安市| 商都县| 井研县| 同仁县| 滨州市| 理塘县| 江都市| 鹤山市| 乌海市| 方城县| 和田市| 乌拉特中旗| 崇文区| 漳浦县| 宜良县| 新闻| 嘉祥县| 华蓥市| 通化县| 乌兰县| 城口县| 临猗县| 广德县| 即墨市| 定襄县|