是否可以发出“VACUUM ANALYZE ”来自 psycopg2 或 sqlalchemy for PostgreSQL?

发布于 2024-09-27 14:14:24 字数 85 浏览 5 评论 0 原文

嗯,这个问题几乎概括了这一点。我的数据库活动更新密集,我想以编程方式发出 Vacuum 分析。但是我收到一条错误,指出查询无法在事务中执行。还有其他方法吗?

Well, the question pretty much summarises it. My db activity is very update intensive, and I want to programmatically issue a Vacuum Analyze. However I get an error that says that the query cannot be executed within a transaction. Is there some other way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木緿 2024-10-04 14:14:24

这是 Python DB-API 中的一个缺陷:它为您启动一个事务。它不应该这样做;是否以及何时开始事务应该由程序员决定。像这样的低级核心 API 不应该照顾开发人员并在我们背后执行诸如启动事务之类的事情。我们是大男孩了——我们可以自己开始交易,谢谢。

使用 psycopg2,您可以通过 API 扩展禁用这种不幸的行为:运行 connection.autocommit = True。不幸的是,没有标准的 API 可以实现这一点,因此您必须依赖非标准扩展来发出必须在事务外部执行的命令。

没有一种语言是没有缺点的,Python 就是其中之一。我以前也被这个咬过。

This is a flaw in the Python DB-API: it starts a transaction for you. It shouldn't do that; whether and when to start a transaction should be up to the programmer. Low-level, core APIs like this shouldn't babysit the developer and do things like starting transactions behind our backs. We're big boys--we can start transactions ourself, thanks.

With psycopg2, you can disable this unfortunate behavior with an API extension: run connection.autocommit = True. There's no standard API for this, unfortunately, so you have to depend on nonstandard extensions to issue commands that must be executed outside of a transaction.

No language is without its warts, and this is one of Python's. I've been bitten by this before too.

围归者 2024-10-04 14:14:24

您可以使用 SQLAlchemy 的 autocommit 模式="noreferrer">raw_connection(这将为您提供“原始”psycopg2 连接):

import sqlalchemy
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


engine = sqlalchemy.create_engine(url)
connection = engine.raw_connection()
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = connection.cursor()
cursor.execute("VACUUM ANALYSE table_name")

You can turn on Postgres autocommit mode using SQLAlchemy's raw_connection (which will give you a "raw" psycopg2 connection):

import sqlalchemy
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


engine = sqlalchemy.create_engine(url)
connection = engine.raw_connection()
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = connection.cursor()
cursor.execute("VACUUM ANALYSE table_name")
等风来 2024-10-04 14:14:24

不确定旧版本的 SQLAlchemy,但使用最新版本(1.4.x 或更高版本),您可以创建自动提交会话,而无需处理原始连接或依赖于数据库特定的黑客:

import sqlalchemy
from sqlalchemy.orm import Session

engine = sqlalchemy.create_engine('postgresql://localhost:5432')
autocommit_engine = engine.execution_options(isolation_level="AUTOCOMMIT")
with Session(autocommit_engine) as session:
    session.execute(f'VACUUM ANALYZE public.my_table;')

自动提交引擎可以从任何 Engine 派生对象。旧的 Engine 实例仍然有效。

Not sure about older versions of SQLAlchemy, but with a recent version (1.4.x or higher) you can create an autocommit session, without handling raw connections or relying on database specific hacks:

import sqlalchemy
from sqlalchemy.orm import Session

engine = sqlalchemy.create_engine('postgresql://localhost:5432')
autocommit_engine = engine.execution_options(isolation_level="AUTOCOMMIT")
with Session(autocommit_engine) as session:
    session.execute(f'VACUUM ANALYZE public.my_table;')

The autocommit engine can be derived from any Engine object. The old Engine instance remains functional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文