#!/usr/bin/env python
# Why is this program idle in transaction while sleeping?
import pgdb, time
def psql(db, sql, commit=True):
	try:
		c = db.cursor()
		c.execute(sql)
		rows = c.fetchall()
		if commit: db.commit()
		return rows
	except Exception, e:
		db.rollback()
		raise e
db = pgdb.connect(host='XXX', database='XXX', user='XXX', password='XXX')
print(psql(db, 'select now()'))
time.sleep(20) # Now, in psql, run: select * from pg_stat_activity
print(psql(db, 'select now()'))
