gpt4 book ai didi

postgresql - autocommit 在 postgresql 和 psycopg2 中意味着什么?

转载 作者:行者123 更新时间:2023-11-29 11:34:01 29 4
gpt4 key购买 nike

自动提交在 PostgreSQL 和 Psycopg2 中的意思相同吗?

来自 PostgreSQL 手册

By default (without BEGIN ), PostgreSQL executes transactions in “autocommit” mode, that is, each statement is executed in its own transaction and a commit is implicitly performed at the end of the statement (if execution was successful, otherwise a rollback is done).

是不是说autocommit会为每个命令创建一个事务?

来自 Psycopg2 driver's manual

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.

and

psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT

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 中的自动提交会为每个命令创建一个事务?

“执行命令时不启动事务并且不需要 commit() 或 rollback()”是否意味着 Psycopg2 中的自动提交将阻止为每个命令创建事务?

“某些 PostgreSQL 命令,如 CREATE DATABASE 或 VACUUM 不能运行到事务中:要运行此类命令,启用自动提交模式”是否意味着 Psycopg2 中的自动提交将阻止仅为某些命令(CREATE DATABASE 或 VACUUM)创建的事务)?

谢谢。

最佳答案

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

PostgreSQL 本身知道自动提交模式,这意味着如果您没有显式启动事务,每个语句将在其自己的事务中运行。

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

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

关于postgresql - autocommit 在 postgresql 和 psycopg2 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51880309/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com