我正在使用 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/