@@ -1316,7 +1316,7 @@ where
1316
1316
{
1317
1317
match protocol_data {
1318
1318
ExtendedProtocolData :: Parse { data, metadata } => {
1319
- let ( parse, hash) = match metadata {
1319
+ let ( client_given_name , parse, hash) = match metadata {
1320
1320
Some ( metadata) => metadata,
1321
1321
None => {
1322
1322
let first_char_in_name = * data. get ( 5 ) . unwrap_or ( & 0 ) ;
@@ -1350,7 +1350,13 @@ where
1350
1350
// TODO: Consider adding the close logic that this function can send for eviction to the client buffer instead
1351
1351
// In this case we don't want to send the parse message to the server since the client is sending it
1352
1352
self . register_parse_to_server_cache (
1353
- false , hash, & parse, & pool, server, & address,
1353
+ false ,
1354
+ client_given_name,
1355
+ hash,
1356
+ parse,
1357
+ & pool,
1358
+ server,
1359
+ & address,
1354
1360
)
1355
1361
. await ?;
1356
1362
@@ -1651,24 +1657,32 @@ where
1651
1657
/// Makes sure the the checked out server has the prepared statement and sends it to the server if it doesn't
1652
1658
async fn ensure_prepared_statement_is_on_server (
1653
1659
& mut self ,
1654
- client_name : String ,
1660
+ client_given_name : String ,
1655
1661
pool : & ConnectionPool ,
1656
1662
server : & mut Server ,
1657
1663
address : & Address ,
1658
1664
) -> Result < ( ) , Error > {
1659
- match self . prepared_statements . get ( & client_name ) {
1665
+ match self . prepared_statements . get ( & client_given_name ) {
1660
1666
Some ( ( parse, hash) ) => {
1661
1667
debug ! ( "Prepared statement `{}` found in cache" , parse. name) ;
1662
1668
// In this case we want to send the parse message to the server
1663
1669
// since pgcat is initiating the prepared statement on this specific server
1664
- self . register_parse_to_server_cache ( true , * hash, parse, pool, server, address)
1665
- . await ?;
1670
+ self . register_parse_to_server_cache (
1671
+ true ,
1672
+ client_given_name,
1673
+ * hash,
1674
+ parse. clone ( ) ,
1675
+ pool,
1676
+ server,
1677
+ address,
1678
+ )
1679
+ . await ?;
1666
1680
}
1667
1681
1668
1682
None => {
1669
1683
return Err ( Error :: ClientError ( format ! (
1670
1684
"prepared statement `{}` not found" ,
1671
- client_name
1685
+ client_given_name
1672
1686
) ) )
1673
1687
}
1674
1688
} ;
@@ -1679,21 +1693,34 @@ where
1679
1693
/// Register the parse to the server cache and send it to the server if requested (ie. requested by pgcat)
1680
1694
///
1681
1695
/// Also updates the pool LRU that this parse was used recently
1696
+ #[ allow( clippy:: too_many_arguments) ]
1682
1697
async fn register_parse_to_server_cache (
1683
- & self ,
1698
+ & mut self ,
1684
1699
should_send_parse_to_server : bool ,
1700
+ client_given_name : String ,
1685
1701
hash : u64 ,
1686
- parse : & Arc < Parse > ,
1702
+ mut parse : Arc < Parse > ,
1687
1703
pool : & ConnectionPool ,
1688
1704
server : & mut Server ,
1689
1705
address : & Address ,
1690
1706
) -> Result < ( ) , Error > {
1691
1707
// We want to update this in the LRU to know this was recently used and add it if it isn't there already
1692
1708
// This could be the case if it was evicted or if doesn't exist (ie. we reloaded and it got removed)
1693
- pool. register_parse_to_cache ( hash, parse) ;
1709
+ if let Some ( new_parse) = pool. register_parse_to_cache ( hash, & parse) {
1710
+ // If the pool has renamed this parse, we need to update the client cache with the new name
1711
+ if parse. name != new_parse. name {
1712
+ warn ! (
1713
+ "Pool renamed prepared statement prepared statement `{}` to `{}` saving new name to client cache" ,
1714
+ parse. name, new_parse. name
1715
+ ) ;
1716
+ }
1717
+ self . prepared_statements
1718
+ . insert ( client_given_name. clone ( ) , ( new_parse. clone ( ) , hash) ) ;
1719
+ parse = new_parse;
1720
+ } ;
1694
1721
1695
1722
if let Err ( err) = server
1696
- . register_prepared_statement ( parse, should_send_parse_to_server)
1723
+ . register_prepared_statement ( & parse, should_send_parse_to_server)
1697
1724
. await
1698
1725
{
1699
1726
pool. ban ( address, BanReason :: MessageSendFailed , Some ( & self . stats ) ) ;
@@ -1741,12 +1768,12 @@ where
1741
1768
) ;
1742
1769
1743
1770
self . prepared_statements
1744
- . insert ( client_given_name, ( new_parse. clone ( ) , hash) ) ;
1771
+ . insert ( client_given_name. clone ( ) , ( new_parse. clone ( ) , hash) ) ;
1745
1772
1746
1773
self . extended_protocol_data_buffer
1747
1774
. push_back ( ExtendedProtocolData :: create_new_parse (
1748
1775
new_parse. as_ref ( ) . try_into ( ) ?,
1749
- Some ( ( new_parse. clone ( ) , hash) ) ,
1776
+ Some ( ( client_given_name , new_parse. clone ( ) , hash) ) ,
1750
1777
) ) ;
1751
1778
1752
1779
Ok ( ( ) )
0 commit comments