I also wanted to query pool_nodes
and as of 2023, the issue is still alive and kicking. Also documentation is scarce. This is how I ended up solving it:
Run the following on the database you are querying via NpgSQL:
CREATE EXTENSION postgres_fdw;
CREATE SERVER ds1 FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost');
CREATE USER MAPPING FOR localuserhere SERVER ds1 OPTIONS (USER 'pcpuserhere', PASSWORD 'pcppasswordhere');
CREATE EXTENSION pgpool_adm;
ds1
: A name for the server you will be querying. You can use anything.
localhost
: Change if you connect via IP/hostname
localuserhere
: This is the normal user you use in your other Npgsql queries.
pcpuserhere
: Your pcp user.
pcppasswordhere
: Your pcp password.
You should now be able to use this query via Npgsql to get node_info status similar to a row from show pool_nodes
:
SELECT * FROM public.pcp_node_info(0,'ds1');
- 0: Your node index (0,1,2, etc)
- ds1: The name your entered for the server.
However, in my case, the above query returned an error:
Returned row contains 11 attributes, but query expects 4.
Probably due to a version mismatch of pgpool2/pgpool_adm?
So I had to create this function:
CREATE OR REPLACE FUNCTION public.my_node_info(node_id integer, pcp_server text,
OUT host text,
OUT port integer,
OUT status text,
OUT pg_status TEXT,
OUT lb_weight REAL,
OUT status_role TEXT,
OUT pg_role TEXT,
OUT replication_delay bigint,
OUT replication_state TEXT,
OUT sync_replication_state TEXT,
OUT last_status_change timestamp
)
RETURNS record
LANGUAGE c
STRICT
AS '$libdir/pgpool_adm', $function$_pcp_node_info$function$
;
So now I can run:
SELECT * FROM public.my_node_info(0,'ds1');
And I can finally view node status via npgsql.
The above were for pgpool 4.2, in 4.4 the relevant function is pgpool_adm_pcp_node_info. Maybe they fixed the bug with the mismatched arguments as well, didn't have time to test.