Skip to content

Commit

Permalink
Support for PgBouncer 1.18 and 1.19
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevan committed May 9, 2023
1 parent e3747ab commit b97cb56
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 87 deletions.
165 changes: 80 additions & 85 deletions collector/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2686,20 +2686,20 @@ func (c *collector) collectPgBouncer() {
}

/*
-[ RECORD 1 ]---------
database | pgbouncer
user | pgbouncer
cl_active | 1
cl_waiting | 0
sv_active | 0
sv_idle | 0
sv_used | 0
sv_tested | 0
sv_login | 0
maxwait | 0
maxwait_us | 0
pool_mode | statement
*/
* PgBouncer "SHOW POOLS" changes across recent PgBouncer versions:
*
* 1.15: (12) database, user, cl_active, cl_waiting, sv_active, sv_idle,
* sv_used, sv_tested, sv_login, maxwait, maxwait_us, pool_mode
* 1.16: (13) database, user, cl_active, cl_waiting, cl_cancel_req, sv_active,
* sv_idle, sv_used, sv_tested, sv_login, maxwait, maxwait_us, pool_mode
* 1.17: same as 1.16
* 1.18: (16) database, user, cl_active, cl_waiting, cl_active_cancel_req,
* cl_waiting_cancel_req, sv_active, sv_active_cancel,
* sv_being_canceled, sv_idle, sv_used, sv_tested, sv_login, maxwait,
* maxwait_us, pool_mode
* 1.19: same as 1.18
*/

func (c *collector) getPBPools() {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
Expand Down Expand Up @@ -2728,6 +2728,12 @@ func (c *collector) getPBPools() {
&pool.ClWaiting, &pool.ClCancelReq, &pool.SvActive, &pool.SvIdle,
&pool.SvUsed, &pool.SvTested, &pool.SvLogin, &pool.MaxWait,
&maxWaitUs, &pool.Mode)
} else if ncols == 16 {
err = rows.Scan(&pool.Database, &pool.UserName, &pool.ClActive,
&pool.ClWaiting, &pool.ClActiveCancelReq, &pool.ClWaitingCancelReq,
&pool.SvActive, &pool.SvActiveCancel, &pool.SvBeingCanceled,
&pool.SvIdle, &pool.SvUsed, &pool.SvTested, &pool.SvLogin,
&pool.MaxWait, &maxWaitUs, &pool.Mode)
} else {
log.Fatalf("pgbouncer: unsupported number of columns %d in 'SHOW POOLS'", ncols)
}
Expand All @@ -2743,25 +2749,19 @@ func (c *collector) getPBPools() {
}

/*
-[ RECORD 1 ]+--------------------
type | S
user | user1
database | db1
state | active
addr | 127.0.0.1
port | 5432
local_addr | 127.0.0.1
local_port | 52664
connect_time | 2019-02-19 09:24:42
request_time | 2019-02-19 09:25:02
wait | 0
wait_us | 0
close_needed | 0
ptr | 0x55cec2a87f40
link | 0x55cec2a82f70
remote_pid | 5017
tls |
*/
* PgBouncer "SHOW SERVERS" changes across recent PgBouncer versions:
*
* 1.15: (17) type, user, database, state, addr, port, local_addr, local_port,
* connect_time, request_time, wait, wait_us, close_needed, ptr,
* link, remote_pid, tls
* 1.16: same as 1.15
* 1.17: same as 1.16
* 1.18: (18) type, user, database, state, addr, port, local_addr, local_port,
* connect_time, request_time, wait, wait_us, close_needed, ptr,
* link, remote_pid, tls, application_name
* 1.19: same as 1.18
*/

func (c *collector) getPBServers() {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
Expand All @@ -2778,7 +2778,7 @@ func (c *collector) getPBServers() {
}

for rows.Next() {
var s [14]sql.NullString
var s [15]sql.NullString
var state string
var wait, waitUs float64
if ncols == 16 {
Expand All @@ -2788,6 +2788,10 @@ func (c *collector) getPBServers() {
err = rows.Scan(&s[0], &s[1], &s[2], &state, &s[3], &s[4], &s[5],
&s[6], &s[7], &s[8], &wait, &waitUs, &s[9], &s[10], &s[11], &s[12],
&s[13])
} else if ncols == 18 {
err = rows.Scan(&s[0], &s[1], &s[2], &state, &s[3], &s[4], &s[5],
&s[6], &s[7], &s[8], &wait, &waitUs, &s[9], &s[10], &s[11], &s[12],
&s[13], &s[14])
} else {
log.Fatalf("pgbouncer: unsupported number of columns %d in 'SHOW SERVERS'", ncols)
}
Expand All @@ -2813,25 +2817,19 @@ func (c *collector) getPBServers() {
}

/*
-[ RECORD 1 ]+--------------------
type | C
user | user1
database | db1
state | active
addr | 127.0.0.1
port | 33374
local_addr | 127.0.0.1
local_port | 16432
connect_time | 2019-02-19 09:24:42
request_time | 2019-02-19 09:38:05
wait | 0
wait_us | 0
close_needed | 0
ptr | 0x55cec2a82f70
link | 0x55cec2a87f40
remote_pid | 0
tls |
*/
* PgBouncer "SHOW CLIENTS" changes across recent PgBouncer versions:
*
* 1.15: (17) type, user, database, state, addr, port, local_addr, local_port,
* connect_time, request_time, wait, wait_us, close_needed, ptr,
* link, remote_pid, tls
* 1.16: same as 1.15
* 1.17: same as 1.16
* 1.18: (18) type, user, database, state, addr, port, local_addr, local_port,
* connect_time, request_time, wait, wait_us, close_needed, ptr,
* link, remote_pid, tls, application_name
* 1.19: same as 1.18
*/

func (c *collector) getPBClients() {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
Expand All @@ -2849,7 +2847,7 @@ func (c *collector) getPBClients() {

var totalWait float64
for rows.Next() {
var s [14]sql.NullString
var s [15]sql.NullString
var state string
var wait, waitUs float64
if ncols == 16 {
Expand All @@ -2859,6 +2857,10 @@ func (c *collector) getPBClients() {
err = rows.Scan(&s[0], &s[1], &s[2], &state, &s[3], &s[4], &s[5],
&s[6], &s[7], &s[8], &wait, &waitUs, &s[9], &s[10], &s[11], &s[12],
&s[13])
} else if ncols == 18 {
err = rows.Scan(&s[0], &s[1], &s[2], &state, &s[3], &s[4], &s[5],
&s[6], &s[7], &s[8], &wait, &waitUs, &s[9], &s[10], &s[11], &s[12],
&s[13], &s[14])
} else {
log.Fatalf("pgbouncer: unsupported number of columns %d in 'SHOW CLIENTS'", ncols)
}
Expand Down Expand Up @@ -2890,23 +2892,18 @@ func (c *collector) getPBClients() {
}

/*
-[ RECORD 1 ]-----+----------
database | db1
total_xact_count | 2
total_query_count | 5
total_received | 724
total_sent | 458
total_xact_time | 782187281
total_query_time | 183481
total_wait_time | 95445
avg_xact_count | 0
avg_query_count | 0
avg_recv | 6
avg_sent | 3
avg_xact_time | 782187281
avg_query_time | 45718
avg_wait_time | 0
*/
* PgBouncer "SHOW STATS" changes across recent PgBouncer versions:
*
* 1.15: (15) database, total_xact_count, total_query_count, total_received,
* total_sent, total_xact_time, total_query_time, total_wait_time,
* avg_xact_count, avg_query_count, avg_recv, avg_sent,
* avg_xact_time, avg_query_time, avg_wait_time
* 1.16: same as 1.15
* 1.17: same as 1.16
* 1.18: same as 1.17
* 1.19: same as 1.18
*/

func (c *collector) getPBStats() {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
Expand Down Expand Up @@ -2941,20 +2938,18 @@ func (c *collector) getPBStats() {
}

/*
-[ RECORD 1 ]-------+----------
name | db1
host | localhost
port | 5432
database | db1
force_user |
pool_size | 5
reserve_pool | 0
pool_mode |
max_connections | 0
current_connections | 0
paused | 0
disabled | 0
*/
* PgBouncer "SHOW DATABASES" changes across recent PgBouncer versions:
*
* 1.15: (12) name, host, port, database, force_user, pool_size, reserve_pool,
* pool_mode, max_connections, current_connections, paused, disabled
* 1.16: (13) name, host, port, database, force_user, pool_size, min_pool_size,
* reserve_pool, pool_mode, max_connections, current_connections,
* paused, disabled
* 1.17: same as 1.16
* 1.18: same as 1.17
* 1.19: same as 1.18
*/

func (c *collector) getPBDatabases() {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()
Expand Down
11 changes: 9 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package pgmetrics
// ModelSchemaVersion is the schema version of the "Model" data structure
// defined below. It is in the "semver" notation. Version history:
//
// 1.14 - PgBouncer 1.19
// 1.13 - Citus 11 support, Postgres 15
// 1.12 - Azure metrics, queryid in plan, progress views
// 1.11 - Postgres 14, PgBouncer 1.16, other attributes
Expand All @@ -34,7 +35,7 @@ package pgmetrics
// 1.2 - more table and index attributes
// 1.1 - added NotificationQueueUsage and Statements
// 1.0 - initial release
const ModelSchemaVersion = "1.13"
const ModelSchemaVersion = "1.14"

// Model contains the entire information collected by a single run of
// pgmetrics. It can be converted to and from json without loss of
Expand Down Expand Up @@ -683,7 +684,13 @@ type PgBouncerPool struct {
Mode string `json:"pool_mode"`

// following fields present only in schema 1.11 and later
ClCancelReq int `json:"cl_cancel_req,omitempty"` // only in pgbouncer >= v1.16.0
ClCancelReq int `json:"cl_cancel_req,omitempty"` // only in pgbouncer v1.16 & v1.17

// following fields present only in schema 1.14 and later
ClActiveCancelReq int `json:cl_active_cancel_req",omitempty"` // only in pgbouncer >= v1.18
ClWaitingCancelReq int `json:cl_waiting_cancel_req",omitempty"` // only in pgbouncer >= v1.18
SvActiveCancel int `json:sv_active_cancel",omitempty"` // only in pgbouncer >= v1.18
SvBeingCanceled int `json:sv_being_canceled",omitempty"` // only in pgbouncer >= v1.18
}

// PgBouncerDatabase contains information about one database of PgBouncer
Expand Down

0 comments on commit b97cb56

Please sign in to comment.