postgresql - sqlalchemy/postgresql : get database 'as-of' timestamp of a query

标签 postgresql sqlalchemy

我正在使用 SQLAlchemy 和 Postgresql,我正在做一个复杂的查询(可能没有行),我需要我从服务器获得的结果的“截至时间戳”(即当前时间戳此查询由数据库执行)。

我怎样才能做到这一点?

最佳答案

如果您在事务中执行查询,则 localtimestamp 将引用事务开始时间。

例如:

with engine.begin() as t:
    print(t.execute("SELECT localtimestamp").fetchone()[0])
    time.sleep(2)  
    print(t.execute("SELECT localtimestamp").fetchone()[0])

打印:

2017-10-19 09:52:44.646723
2017-10-19 09:52:44.646723

即本地时间戳在交易期间保持不变。

关于postgresql - sqlalchemy/postgresql : get database 'as-of' timestamp of a query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46824350/

相关文章:

postgresql - postgresql 中的 data_type 和 UDT_name

stored-procedures - SQLAlchemy 从 postgresql 存储过程获取输出参数

python - 如何在 SqlAlchemy 中转义表名

php - 拉维尔 5.6 : Create schema in database

同一连接上的 PostgreSQL 多个事务

Postgresql Centos 问题

Python3 typehints - 传入类型的元组后,指定返回值是这些类型的实例的元组

python - 如何使 declarative_base 派生类符合接口(interface)?

python - SQLAlchemy:多个表的外键

PostgreSQL 与时间戳之谜