当前位置:网站首页>PgSQL reports an error: current transaction is aborted, commands ignored until end of transaction block

PgSQL reports an error: current transaction is aborted, commands ignored until end of transaction block

2022-06-11 05:51:00 Lao Liu selling vegetables at the corner of the street

We are PG database , When auto commit of transactions is turned off , Will often encounter such problems

ERROR: current transaction is aborted, commands ignored until end of transaction block

Why did it cause such a problem , as a result of

Postgres In the database , In the same transaction, if there is an error in a database operation , Then the database after this transaction will make mistakes .

Let's take a very simple example

test=# select * from test1;
ERROR:  relation "test1" does not exist
LINE 1: select * from test1;
Time: 0.376 ms

This is the time , Because this operation is wrong , Then all operations in the following session will report

ERROR: current transaction is aborted, commands ignored until end of transaction block

This is the time , We have to solve this problem , Only use rollback, Or is it commit To solve the

That's the point ,PG Not humane enough

If it is python operation pgsql, The solution is as follows :

try:
    function()
except Exception as e:
      print('------------>',e)
      cursor.execute("ROLLBACK")   # If the data is wrong , Perform a rollback operation 

cursor.execute("ROLLBACK")

First try Execute the code you need , then except Execute the above rollback operation to continue execution

原网站

版权声明
本文为[Lao Liu selling vegetables at the corner of the street]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020533537551.html