De manier die ik gebruik om klanten van pgbouncer te blijven volgen, is lid worden van pg_stat_statements
met pgbouncer show clients
opdracht. Hier is een voorbeeld:
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
(Je kunt in ieder geval gewoon dblink of gewoon postgres_fdw gebruiken - ik gebruik mix om verouderd gebruik op prod te ondersteunen... Als je geen notities had, negeer dat dan...)
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
Nu kunnen we deelnemen aan de pgbouncer-weergave met pg_stat_statements
:
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)
Hier app - 10.10.10.10:24514
betekent dat deze pid van pgbouncer is, en naar pgbouncer is het verbonden vanaf 0.10.10.10