@@ -59,6 +59,7 @@ pub struct Client<S, T> {
5959 client_server_map : ClientServerMap ,
6060
6161 /// Client parameters, e.g. user, client_encoding, etc.
62+ #[ allow( dead_code) ]
6263 parameters : HashMap < String , String > ,
6364
6465 /// Statistics
@@ -82,6 +83,9 @@ pub struct Client<S, T> {
8283 /// Postgres user for this client (This comes from the user in the connection string)
8384 username : String ,
8485
86+ /// Application name for this client (defaults to pgcat)
87+ application_name : String ,
88+
8589 /// Used to notify clients about an impending shutdown
8690 shutdown : Receiver < ( ) > ,
8791}
@@ -365,6 +369,11 @@ where
365369 None => return Err ( Error :: ClientError ) ,
366370 } ;
367371
372+ let application_name = match parameters. get ( "application_name" ) {
373+ Some ( application_name) => application_name,
374+ None => "pgcat" ,
375+ } ;
376+
368377 let admin = [ "pgcat" , "pgbouncer" ]
369378 . iter ( )
370379 . filter ( |db| * db == & pool_name)
@@ -493,6 +502,7 @@ where
493502 last_server_id : None ,
494503 pool_name : pool_name. clone ( ) ,
495504 username : username. clone ( ) ,
505+ application_name : application_name. to_string ( ) ,
496506 shutdown,
497507 connected_to_server : false ,
498508 } ) ;
@@ -526,6 +536,7 @@ where
526536 last_server_id : None ,
527537 pool_name : String :: from ( "undefined" ) ,
528538 username : String :: from ( "undefined" ) ,
539+ application_name : String :: from ( "undefined" ) ,
529540 shutdown,
530541 connected_to_server : false ,
531542 } ) ;
@@ -767,13 +778,10 @@ where
767778 server. address( )
768779 ) ;
769780
770- // Set application_name if any.
771781 // TODO: investigate other parameters and set them too.
772- if self . parameters . contains_key ( "application_name" ) {
773- server
774- . set_name ( & self . parameters [ "application_name" ] )
775- . await ?;
776- }
782+
783+ // Set application_name.
784+ server. set_name ( & self . application_name ) . await ?;
777785
778786 // Transaction loop. Multiple queries can be issued by the client here.
779787 // The connection belongs to the client until the transaction is over,
@@ -790,12 +798,7 @@ where
790798 Err ( err) => {
791799 // Client disconnected inside a transaction.
792800 // Clean up the server and re-use it.
793- // This prevents connection thrashing by bad clients.
794- if server. in_transaction ( ) {
795- server. query ( "ROLLBACK" ) . await ?;
796- server. query ( "DISCARD ALL" ) . await ?;
797- server. set_name ( "pgcat" ) . await ?;
798- }
801+ server. checkin_cleanup ( ) . await ?;
799802
800803 return Err ( err) ;
801804 }
@@ -837,16 +840,7 @@ where
837840
838841 // Terminate
839842 'X' => {
840- // Client closing. Rollback and clean up
841- // connection before releasing into the pool.
842- // Pgbouncer closes the connection which leads to
843- // connection thrashing when clients misbehave.
844- if server. in_transaction ( ) {
845- server. query ( "ROLLBACK" ) . await ?;
846- server. query ( "DISCARD ALL" ) . await ?;
847- server. set_name ( "pgcat" ) . await ?;
848- }
849-
843+ server. checkin_cleanup ( ) . await ?;
850844 self . release ( ) ;
851845
852846 return Ok ( ( ) ) ;
@@ -950,8 +944,10 @@ where
950944
951945 // The server is no longer bound to us, we can't cancel it's queries anymore.
952946 debug ! ( "Releasing server back into the pool" ) ;
947+ server. checkin_cleanup ( ) . await ?;
953948 self . stats . server_idle ( server. process_id ( ) , address. id ) ;
954949 self . connected_to_server = false ;
950+
955951 self . release ( ) ;
956952 self . stats . client_idle ( self . process_id , address. id ) ;
957953 }
0 commit comments