自动提交在postgresql和psycopg2中是什么意思? [英] What does autocommit mean in postgresql and psycopg2?

查看:398
本文介绍了自动提交在postgresql和psycopg2中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PostgreSQL和Psycopg2中自动提交的含义是否相同?

Does autocommit mean the same in PostgreSQL and Psycopg2?

从PostgreSQL手册

From PostgreSQL manual


默认情况下(不使用BEGIN),
PostgreSQL在自动提交模式下执行事务,即每个语句在其自己的
事务中执行
a


$ b $在语句末尾隐式执行commit(如果执行成功,则
否则将回滚)。 b

这是否意味着自动提交将为每个命令创建一个事务?

Does it mean that autocommit will create a transaction for each command?

来自 Psycopg2驱动程序手册


可以设置连接在自动提交模式下:这种
方式所有执行的命令将立即提交,并且不可能
进行回滚。一些命令(例如CREATE DATABASE,VACUUM…)
需要在任何事务之外运行:为了能够从$ Psycopg运行
这些命令,连接必须处于自动提交
模式:您可以使用autocommit属性。

It is possible to set the connection in autocommit mode: this way all the commands executed will be immediately committed and no rollback is possible. A few commands (e.g. CREATE DATABASE, VACUUM…) require to be run outside any transaction: in order to be able to run these commands from Psycopg, the connection must be in autocommit mode: you can use the autocommit property.


psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT

在执行命令时不启动任何事务某些PostgreSQL命令(例如
CREATE DATABASE或VACUUM)无法运行到事务中:运行这样的
命令使用:

No transaction is started when commands are executed and no commit() or rollback() is required. Some PostgreSQL command such as CREATE DATABASE or VACUUM can’t run into a transaction: to run such command use:

>>> conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)


所有执行的命令都会立即提交意味着Psycopg2中的自动提交会为每个命令创建一个事务?

Does "all the commands executed will be immediately committed" mean that autocommit in Psycopg2 creates a transaction for each command?

是否在执行命令且没有commit()或rollback()的情况下不会启动任何事务是必需的是否意味着Psycopg2中的自动提交将阻止为每个命令创建事务?

Does "No transaction is started when commands are executed and no commit() or rollback() is required" mean that autocommit in Psycopg2 will prevent a transaction created for each command?

某些PostgreSQL命令(例如CREATE DATABASE或VACUUM)无法运行到事务中吗? :要运行这样的命令,启用自动提交模式,意味着Psycopg2中的自动提交将阻止仅为某些命令(CREATE DATABASE或VACUUM)创建的事务?

Does "Some PostgreSQL command such as CREATE DATABASE or VACUUM can’t run into a transaction: to run such command, enable autocommit mode" mean that autocommit in Psycopg2 will prevent a transaction created only for some commands (CREATE DATABASE or VACUUM)?

谢谢。

推荐答案

每个PostgreSQL语句都在事务中运行。

Every PostgreSQL statement is running in a transaction.

PostgreSQL本身 only 知道自动提交模式,这意味着每个语句将以自己的事务处理方式运行

PostgreSQL itself only knows autocommit mode, which means that each statement will run in its own transaction if you don't start a transaction explicitly.

诸如 VACUUM 之类的语句不能与其他事务在同一事务中运行

Statements like VACUUM cannot run in the same transaction with other statements.

如果您未在psycopg2中使用自动提交,则驱动程序必须通过在运行第一条语句时显式启动事务来模拟非自动提交模式。

If you are not using autocommit in psycopg2, the driver has to simulate non-autocommit mode by explicitly starting a transaction when the first statement is run.

这篇关于自动提交在postgresql和psycopg2中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆