我通过
pgbouncer
连接到 psycopg==3.1.19
并尝试执行 SHOW STATS
(pgbouncer 本身会回答,它不会发送到 postgresql)
我从
ProtocolViolation: extended query protocol not supported by admin console
本身得到了 pgbouncer
。
这是我使用的代码
import psycopg
from psycopg.rows import dict_row
conn = psycopg.connect("host=127.0.0.1 port=6432 user=myuser password=mypass dbname=pgbouncer ", row_factory=dict_row, autocommit=False)
conn.prepare_threshold = None
conn.prepared_max = 0
cur = conn.cursor()
cur.execute("SHOW STATS") # ProtocolViolation: extended query protocol not supported by admin console
我查看了 psycopg 3 文档,并且 connect() 似乎没有任何关于“扩展查询协议”的内容。
那么有什么方法可以在 psycopg 3 中禁用这个扩展查询协议,或者我唯一的选择是降级到 psycopg 2?
正如OP中提到的,降级到psycopg2将允许对pgbouncer执行
SHOW STATS
(使用pgbouncer
数据库):
pip install psycopg2
import psycopg2
import psycopg2.extras
conn = psycopg2.connect("host=127.0.0.1 port=6432 user=myuser password=mypass dbname=pgbouncer ") # SHOW STATS only available if you connect to the special database called "pgbouncer"
conn.set_session(autocommit=True) # BEGIN not supported for database "pgbouncer"
cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) # to get column names
cur.execute("SHOW STATS")
print(cur.fetchone())
这将打印统计记录:
RealDictRow([('database', 'pgbouncer'),
('total_xact_count', Decimal('4')),
('total_query_count', Decimal('4')),
('total_received', Decimal('0')),
('total_sent', Decimal('0')),
('total_xact_time', Decimal('0')),
('total_query_time', Decimal('0')),
('total_wait_time', Decimal('0')),
('avg_xact_count', Decimal('0')),
('avg_query_count', Decimal('0')),
('avg_recv', Decimal('0')),
('avg_sent', Decimal('0')),
('avg_xact_time', Decimal('0')),
('avg_query_time', Decimal('0')),
('avg_wait_time', Decimal('0'))])
但这仍然是并没有真正禁用psycopg3中的扩展查询支持,它只是使用psycopg2来代替。