@@ -4,7 +4,7 @@ use tokio::net::tcp::OwnedWriteHalf;
44
55use std:: collections:: HashMap ;
66
7- use crate :: config:: { get_config, parse, Role } ;
7+ use crate :: config:: { get_config, parse} ;
88use crate :: errors:: Error ;
99use crate :: messages:: * ;
1010use crate :: pool:: ConnectionPool ;
@@ -41,6 +41,15 @@ pub async fn handle_admin(
4141 } else if query. starts_with ( "SHOW DATABASES" ) {
4242 trace ! ( "SHOW DATABASES" ) ;
4343 show_databases ( stream, & pool) . await
44+ } else if query. starts_with ( "SHOW POOLS" ) {
45+ trace ! ( "SHOW POOLS" ) ;
46+ show_pools ( stream, & pool) . await
47+ } else if query. starts_with ( "SHOW LISTS" ) {
48+ trace ! ( "SHOW LISTS" ) ;
49+ show_lists ( stream, & pool) . await
50+ } else if query. starts_with ( "SHOW VERSION" ) {
51+ trace ! ( "SHOW VERSION" ) ;
52+ show_version ( stream) . await
4453 } else if query. starts_with ( "SET " ) {
4554 trace ! ( "SET" ) ;
4655 ignore_set ( stream) . await
@@ -49,6 +58,118 @@ pub async fn handle_admin(
4958 }
5059}
5160
61+ /// SHOW LISTS
62+ async fn show_lists ( stream : & mut OwnedWriteHalf , pool : & ConnectionPool ) -> Result < ( ) , Error > {
63+ let stats = get_stats ( ) ;
64+
65+ let columns = vec ! [ ( "list" , DataType :: Text ) , ( "items" , DataType :: Int4 ) ] ;
66+
67+ let mut res = BytesMut :: new ( ) ;
68+ res. put ( row_description ( & columns) ) ;
69+ res. put ( data_row ( & vec ! [
70+ "databases" . to_string( ) ,
71+ pool. databases( ) . to_string( ) ,
72+ ] ) ) ;
73+ res. put ( data_row ( & vec ! [ "users" . to_string( ) , "1" . to_string( ) ] ) ) ;
74+ res. put ( data_row ( & vec ! [
75+ "pools" . to_string( ) ,
76+ pool. databases( ) . to_string( ) ,
77+ ] ) ) ;
78+ res. put ( data_row ( & vec ! [
79+ "free_clients" . to_string( ) ,
80+ stats[ "cl_idle" ] . to_string( ) ,
81+ ] ) ) ;
82+ res. put ( data_row ( & vec ! [
83+ "used_clients" . to_string( ) ,
84+ stats[ "cl_active" ] . to_string( ) ,
85+ ] ) ) ;
86+ res. put ( data_row ( & vec ! [
87+ "login_clients" . to_string( ) ,
88+ "0" . to_string( ) ,
89+ ] ) ) ;
90+ res. put ( data_row ( & vec ! [
91+ "free_servers" . to_string( ) ,
92+ stats[ "sv_idle" ] . to_string( ) ,
93+ ] ) ) ;
94+ res. put ( data_row ( & vec ! [
95+ "used_servers" . to_string( ) ,
96+ stats[ "sv_active" ] . to_string( ) ,
97+ ] ) ) ;
98+ res. put ( data_row ( & vec ! [ "dns_names" . to_string( ) , "0" . to_string( ) ] ) ) ;
99+ res. put ( data_row ( & vec ! [ "dns_zones" . to_string( ) , "0" . to_string( ) ] ) ) ;
100+ res. put ( data_row ( & vec ! [ "dns_queries" . to_string( ) , "0" . to_string( ) ] ) ) ;
101+ res. put ( data_row ( & vec ! [ "dns_pending" . to_string( ) , "0" . to_string( ) ] ) ) ;
102+
103+ res. put ( command_complete ( "SHOW" ) ) ;
104+
105+ res. put_u8 ( b'Z' ) ;
106+ res. put_i32 ( 5 ) ;
107+ res. put_u8 ( b'I' ) ;
108+
109+ write_all_half ( stream, res) . await
110+ }
111+
112+ /// SHOW VERSION
113+ async fn show_version ( stream : & mut OwnedWriteHalf ) -> Result < ( ) , Error > {
114+ let mut res = BytesMut :: new ( ) ;
115+
116+ res. put ( row_description ( & vec ! [ ( "version" , DataType :: Text ) ] ) ) ;
117+ res. put ( data_row ( & vec ! [ "PgCat 0.1.0" . to_string( ) ] ) ) ;
118+ res. put ( command_complete ( "SHOW" ) ) ;
119+
120+ res. put_u8 ( b'Z' ) ;
121+ res. put_i32 ( 5 ) ;
122+ res. put_u8 ( b'I' ) ;
123+
124+ write_all_half ( stream, res) . await
125+ }
126+
127+ /// SHOW POOLS
128+ async fn show_pools ( stream : & mut OwnedWriteHalf , _pool : & ConnectionPool ) -> Result < ( ) , Error > {
129+ let stats = get_stats ( ) ;
130+ let config = {
131+ let guard = get_config ( ) ;
132+ & * guard. clone ( )
133+ } ;
134+
135+ let columns = vec ! [
136+ ( "database" , DataType :: Text ) ,
137+ ( "user" , DataType :: Text ) ,
138+ ( "cl_active" , DataType :: Numeric ) ,
139+ ( "cl_waiting" , DataType :: Numeric ) ,
140+ ( "cl_cancel_req" , DataType :: Numeric ) ,
141+ ( "sv_active" , DataType :: Numeric ) ,
142+ ( "sv_idle" , DataType :: Numeric ) ,
143+ ( "sv_used" , DataType :: Numeric ) ,
144+ ( "sv_tested" , DataType :: Numeric ) ,
145+ ( "sv_login" , DataType :: Numeric ) ,
146+ ( "maxwait" , DataType :: Numeric ) ,
147+ ( "maxwait_us" , DataType :: Numeric ) ,
148+ ( "pool_mode" , DataType :: Text ) ,
149+ ] ;
150+
151+ let mut res = BytesMut :: new ( ) ;
152+ res. put ( row_description ( & columns) ) ;
153+
154+ let mut row = vec ! [ String :: from( "all" ) , config. user. name. clone( ) ] ;
155+
156+ for column in & columns[ 2 ..columns. len ( ) - 1 ] {
157+ let value = stats. get ( column. 0 ) . unwrap_or ( & 0 ) . to_string ( ) ;
158+ row. push ( value) ;
159+ }
160+
161+ row. push ( config. general . pool_mode . to_string ( ) ) ;
162+
163+ res. put ( data_row ( & row) ) ;
164+ res. put ( command_complete ( "SHOW" ) ) ;
165+
166+ res. put_u8 ( b'Z' ) ;
167+ res. put_i32 ( 5 ) ;
168+ res. put_u8 ( b'I' ) ;
169+
170+ write_all_half ( stream, res) . await
171+ }
172+
52173/// SHOW DATABASES
53174async fn show_databases ( stream : & mut OwnedWriteHalf , pool : & ConnectionPool ) -> Result < ( ) , Error > {
54175 let guard = get_config ( ) ;
@@ -79,23 +200,13 @@ async fn show_databases(stream: &mut OwnedWriteHalf, pool: &ConnectionPool) -> R
79200
80201 for shard in 0 ..pool. shards ( ) {
81202 let database_name = & config. shards [ & shard. to_string ( ) ] . database ;
82- let mut replica_count = 0 ;
83203
84204 for server in 0 ..pool. servers ( shard) {
85205 let address = pool. address ( shard, server) ;
86- let name = match address. role {
87- Role :: Primary => format ! ( "shard_{}_primary" , shard) ,
88-
89- Role :: Replica => {
90- let name = format ! ( "shard_{}_replica_{}" , shard, replica_count) ;
91- replica_count += 1 ;
92- name
93- }
94- } ;
95206 let pool_state = pool. pool_state ( shard, server) ;
96207
97208 res. put ( data_row ( & vec ! [
98- name, // name
209+ address . name( ) , // name
99210 address. host. to_string( ) , // host
100211 address. port. to_string( ) , // port
101212 database_name. to_string( ) , // database
@@ -222,7 +333,7 @@ async fn show_stats(stream: &mut OwnedWriteHalf) -> Result<(), Error> {
222333 res. put ( row_description ( & columns) ) ;
223334
224335 let mut row = vec ! [
225- String :: from( "all shards " ) , // TODO: per-database stats,
336+ String :: from( "all" ) , // TODO: per-database stats,
226337 ] ;
227338
228339 for column in & columns[ 1 ..] {
0 commit comments