gpt4 book ai didi

postgresql - 通过 pgbouncer 查找查询源

转载 作者:行者123 更新时间:2023-11-29 13:22:13 24 4
gpt4 key购买 nike

我正在尝试尽可能多地了解特定类型的更新查询来自哪个主机。

我的数据库是 PostgreSQL 9.0.23,本地运行 pgbouncer 用于连接池。池化类型为交易池。

数据库中保存来自 pg_stat_activity 的信息的触发器告诉我有关 pgbouncer 连接的信息,但无法帮助我找出背后的人。

有问题的数据库是负载下的生产数据库。因此删除 pgbouncer 不是一个选项。让 pgbouncer 写一个冗长的日志可能也是一个坏主意。

有什么合理的方法可以找出查询最终来自哪个主机?

最佳答案

我用来从 pgbouncer 跟踪客户端的方法是加入 pg_stat_statements 和 pgbouncer show clients 命令。这是一个例子:

t=# create extension "postgres_fdw";
CREATE EXTENSION
t=# CREATE SERVER pgbouncer FOREIGN DATA WRAPPER postgres_fdw OPTIONS ( host '10.10.10.10', port '6432', dbname 'pgbouncer');
CREATE SERVER

(无论如何,您可以只使用 dblink 或只使用 postgres_fdw - 我使用 mix 来支持产品上的过时用法...如果您没有任何注释,请忽略它...)

t=# create extension "dblink";
CREATE EXTENSION
t=# create view bnc_client AS SELECT _.type,
_."user",
_.database,
_.state,
_.addr,
_.port,
_.local_addr,
_.local_port,
_.connect_time,
_.request_time,
_.ptr,
_.link,
_.remote_pid,
_.tls
FROM dblink('pgbouncer'::text, 'show clients'::text) _(type text, "user" text, database text, state text, addr text, port integer, local_addr text, local_port integer, connect_time timestamp with time zone, request_time timestamp with time zone, ptr text, link text, remote_pid smallint, tls text);
CREATE VIEW
t=# create user mapping FOR vao server pgbouncer options (user 'pgbouncer_known_user', password 'password_here');
CREATE USER MAPPING

现在我们可以使用 pg_stat_statements 加入 pgbouncer View :

t=# select
datname
, usename
, p.state
, case when b.user is not null then 'pgBouncer' else application_name end app
, case when b.user is null then client_addr else addr::inet end ip
, b.user
, b.state "bState"
, case when waiting then true else null end w
, b.connect_time
, query_start
, md5(query)::uuid
, pid
from pg_stat_activity p
left outer join bnc_client b
on addr||':'||b.port = regexp_replace(p.application_name,'^.{0,}(- )','')
where pid <> pg_backend_pid()
;
datname | usename | state | app | ip | user | bState | w | connect_time | query_start
| md5 | pid
---------+---------+-------+------------------------+--------------+------+--------+---+--------------+-------------------------------
+--------------------------------------+-------
dbn | usr | idle | | 192.168.0.1 | | | | | 2017-03-09 17:19:46.206643+00
| d1730c52-dffd-3650-a399-23f4dd4aa456 | 12614
dbn | usr | idle | app - 10.10.10.10:24514 | 10.10.10.10 | | | | | 2017-03-10 11:24:34.999174+00
| 92a0340c-9ecc-9375-37c0-e70e8b225db4 | 22563
(2 rows)

此处 app - 10.10.10.10:24514 表示此 pid 来自 pgbouncer,并且它从 0.10.10.10 连接到 pgbouncer

关于postgresql - 通过 pgbouncer 查找查询源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39734161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com