Skip to content

Commit

Permalink
Add support for pgbouncer 1.21 (#18)
Browse files Browse the repository at this point in the history
* fix: fix pgbouncer_server view missing column

* chore: update changelog

* fix: fix pgbouncer_dns_zones view missing column

* add support for pgbouncer 1.21

* add support for pgbouncer 1.21

* Apply suggestions from code review

Co-authored-by: David Christensen <[email protected]>

* fix comparison logic for inbetween bouncer versions

* fix invalid conditional and order of object dropping

---------

Co-authored-by: David Christensen <[email protected]>
  • Loading branch information
keithf4 and pgguru authored Oct 27, 2023
1 parent 965a549 commit 1bb22dd
Show file tree
Hide file tree
Showing 5 changed files with 924 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.1.0
-- Add support for PgBouncer 1.21. Adds prepared_statements column to clients, servers and sockets functions and views.


v1.0.1
-- Fix missing comma that caused missing column "pgbouncer_target_host" in "pgbouncer_servers" and "pgbouncer_dns_zones" views

Expand Down
2 changes: 1 addition & 1 deletion pgbouncer_fdw.control
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
default_version = '1.0.1'
default_version = '1.1.0'
comment = 'Extension for querying PgBouncer stats from normal SQL views & running pgbouncer commands from normal SQL functions'
requires = dblink
relocatable = false
160 changes: 157 additions & 3 deletions sql/functions/pgbouncer_fdw_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ CREATE FUNCTION @[email protected]_clients_func() RETURNS TABLE
, remote_pid int
, tls text
, application_name text
, prepared_statements int
)
LANGUAGE plpgsql
AS $$
Expand All @@ -100,7 +101,50 @@ LOOP BEGIN
INTO v_version_major, v_version_minor
FROM @[email protected]_version_func(v_row.target_host);

IF v_version_major >= 1 AND v_version_minor >= 18 THEN
IF v_version_major >= 1 AND v_version_minor >= 21 THEN
RETURN QUERY SELECT
v_row.target_host AS pgbouncer_target_host
, x."type"
, x."user"
, x.database
, x.state
, x.addr
, x.port
, x.local_addr
, x.local_port
, x.connect_time
, x.request_time
, x.wait
, x.wait_us
, x.close_needed
, x.ptr
, x.link
, x.remote_pid
, x.tls
, x.application_name
, x.prepared_statements
FROM dblink(v_row.target_host, 'show clients') AS x
( "type" text
, "user" text
, database text
, state text
, addr text
, port int
, local_addr text
, local_port int
, connect_time timestamp with time zone
, request_time timestamp with time zone
, wait int
, wait_us int
, close_needed int
, ptr text
, link text
, remote_pid int
, tls text
, application_name text
, prepared_statements int
);
ELSIF v_version_major = 1 AND v_version_minor >= 18 AND v_version_minor < 21 THEN
RETURN QUERY SELECT
v_row.target_host AS pgbouncer_target_host
, x."type"
Expand All @@ -121,6 +165,7 @@ LOOP BEGIN
, x.remote_pid
, x.tls
, x.application_name
, 0 AS prepared_statements
FROM dblink(v_row.target_host, 'show clients') AS x
( "type" text
, "user" text
Expand Down Expand Up @@ -163,6 +208,7 @@ LOOP BEGIN
, x.remote_pid
, x.tls
, '' AS application_name
, 0 AS prepared_statements
FROM dblink(v_row.target_host, 'show clients') AS x
( "type" text
, "user" text
Expand Down Expand Up @@ -665,6 +711,7 @@ CREATE FUNCTION @[email protected]_servers_func() RETURNS TABLE
, remote_pid int
, tls text
, application_name text
, prepared_statements int
)
LANGUAGE plpgsql
AS $$
Expand All @@ -686,7 +733,7 @@ LOOP BEGIN
INTO v_version_major, v_version_minor
FROM @[email protected]_version_func(v_row.target_host);

IF v_version_major >= 1 AND v_version_minor >= 18 THEN
IF v_version_major >= 1 AND v_version_minor >= 21 THEN
RETURN QUERY SELECT
v_row.target_host AS pgbouncer_target_host
, x."type"
Expand All @@ -707,6 +754,51 @@ LOOP BEGIN
, x.remote_pid
, x.tls
, x.application_name
, x.prepared_statements
FROM dblink(v_row.target_host, 'show servers') AS x
(
"type" text
, "user" text
, database text
, state text
, addr text
, port int
, local_addr text
, local_port int
, connect_time timestamp with time zone
, request_time timestamp with time zone
, wait int
, wait_us int
, close_needed int
, ptr text
, link text
, remote_pid int
, tls text
, application_name text
, prepared_statements int
);
ELSIF v_version_major = 1 AND v_version_minor >= 18 and v_version_minor < 21 THEN
RETURN QUERY SELECT
v_row.target_host AS pgbouncer_target_host
, x."type"
, x."user"
, x.database
, x.state
, x.addr
, x.port
, x.local_addr
, x.local_port
, x.connect_time
, x.request_time
, x.wait
, x.wait_us
, x.close_needed
, x.ptr
, x.link
, x.remote_pid
, x.tls
, x.application_name
, 0 AS prepared_statements
FROM dblink(v_row.target_host, 'show servers') AS x
(
"type" text
Expand Down Expand Up @@ -750,6 +842,7 @@ LOOP BEGIN
, x.remote_pid
, x.tls
, '' AS application_name
, 0 AS prepared_statements
FROM dblink(v_row.target_host, 'show servers') AS x
(
"type" text
Expand Down Expand Up @@ -823,6 +916,7 @@ CREATE FUNCTION @[email protected]_sockets_func() RETURNS TABLE
, send_remain int
, pkt_avail int
, send_avail int
, prepared_statements int
)
LANGUAGE plpgsql
AS $$
Expand All @@ -843,7 +937,65 @@ LOOP BEGIN
INTO v_version_major, v_version_minor
FROM @[email protected]_version_func(v_row.target_host);

IF v_version_major >= 1 AND v_version_minor >= 18 THEN
IF v_version_major >= 1 AND v_version_minor >= 21 THEN
RETURN QUERY SELECT
v_row.target_host AS pgbouncer_target_host
, x."type"
, x."user"
, x.database
, x.state
, x.addr
, x.port
, x.local_addr
, x.local_port
, x.connect_time
, x.request_time
, x.wait
, x.wait_us
, x.close_needed
, x.ptr
, x.link
, x.remote_pid
, x.tls
, x.application_name
, x.recv_pos
, x.pkt_pos
, x.pkt_remain
, x.send_pos
, x.send_remain
, x.pkt_avail
, x.send_avail
, x.prepared_statements
FROM dblink(v_row.target_host, 'show sockets') AS x
(
"type" text
, "user" text
, database text
, state text
, addr text
, port int
, local_addr text
, local_port int
, connect_time timestamp with time zone
, request_time timestamp with time zone
, wait int
, wait_us int
, close_needed int
, ptr text
, link text
, remote_pid int
, tls text
, application_name text
, recv_pos int
, pkt_pos int
, pkt_remain int
, send_pos int
, send_remain int
, pkt_avail int
, send_avail int
, prepared_statements int
);
ELSIF v_version_major = 1 AND v_version_minor >= 18 AND v_version_minor < 21 THEN
RETURN QUERY SELECT
v_row.target_host AS pgbouncer_target_host
, x."type"
Expand Down Expand Up @@ -871,6 +1023,7 @@ LOOP BEGIN
, x.send_remain
, x.pkt_avail
, x.send_avail
, 0 AS prepared_statements
FROM dblink(v_row.target_host, 'show sockets') AS x
(
"type" text
Expand Down Expand Up @@ -928,6 +1081,7 @@ LOOP BEGIN
, x.send_remain
, x.pkt_avail
, x.send_avail
, 0 AS prepared_statements
FROM dblink(v_row.target_host, 'show sockets') AS x
(
"type" text
Expand Down
3 changes: 3 additions & 0 deletions sql/views/pgbouncer_fdw_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CREATE VIEW @[email protected]_clients AS
, remote_pid
, tls
, application_name
, prepared_statements
FROM @[email protected]_clients_func();


Expand Down Expand Up @@ -120,6 +121,7 @@ CREATE VIEW @[email protected]_servers AS
, remote_pid
, tls
, application_name
, prepared_statements
FROM @[email protected]_servers_func();


Expand Down Expand Up @@ -150,6 +152,7 @@ CREATE VIEW @[email protected]_sockets AS
, send_remain
, pkt_avail
, send_avail
, prepared_statements
FROM @[email protected]_sockets_func();


Expand Down
Loading

0 comments on commit 1bb22dd

Please sign in to comment.