当我执行普通时返回选择正确的结果,但是当我执行此选择以进行数据库正常运行时,它始终返回相同的第一个结果。我确实检查了Postgres日志,并且我看到select已执行。
#!/usr/bin/python3 import psycopg2 from time import sleep conn = psycopg2.connect("dbname='MyDB' user='root' host='127.0.0.1' password='********'") cur = conn.cursor() def test(): e = 0 while e != 100: cur.execute("SELECT date_trunc('second', current_timestamp - pg_postmaster_start_time()) as uptime;") uptv = cur.fetchone() print(uptv) e += 1 sleep(0.1) test()
答案 0 :(得分:3)
Per the documentation, current_timestamp
returns the timestamp at the start of the transaction。 SQL标准需要此行为。
=query('KI Site Visit'!A1:Y38,"Select A,max(D), H, Q where D is not null group by A, H, Q label max(D) 'KI Most Recent Visit Date'",1)
开始一个事务。它不会自动提交。所以,除非你psycopg2
,否则第一个查询和后来的迭代都会运行相同的xact。
你应该:
conn.commit()
(或者如果您愿意,conn.commit()
如果它是只读的并且不做任何更改);或conn.rollback()
代替clock_timestamp()
,因为前者在整个交易过程中都会发生变化。最好避免让事务继续运行,因为它们可以占用服务器所需的资源。