Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 8af1bfb

Browse files
pimeysJakub Wieczorek
authored andcommitted
Enable transactional pool mode configuration
1 parent 9efd813 commit 8af1bfb

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

postgres/src/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,20 @@ impl Config {
433433
self
434434
}
435435

436+
/// When enabled, the client skips all internal caching for statements,
437+
/// allowing usage with a connection pool with transaction mode.
438+
///
439+
/// Defaults to `false`.
440+
pub fn transaction_pool_mode(&mut self, enable: bool) -> &mut Config {
441+
self.config.transaction_pool_mode(enable);
442+
self
443+
}
444+
445+
/// Gets the transaction pool mode status.
446+
pub fn get_transaction_pool_mode(&self) -> bool {
447+
self.config.get_transaction_pool_mode()
448+
}
449+
436450
/// Opens a connection to a PostgreSQL database.
437451
pub fn connect<T>(&self, tls: T) -> Result<Client, Error>
438452
where

tokio-postgres/src/client.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ struct CachedTypeInfo {
8383

8484
pub struct InnerClient {
8585
sender: mpsc::UnboundedSender<Request>,
86+
transaction_pool_mode: bool,
8687
cached_typeinfo: Mutex<CachedTypeInfo>,
8788

8889
/// A buffer to use when writing out postgres commands.
@@ -108,31 +109,39 @@ impl InnerClient {
108109
}
109110

110111
pub fn set_typeinfo(&self, statement: &Statement) {
111-
self.cached_typeinfo.lock().typeinfo = Some(statement.clone());
112+
if !self.transaction_pool_mode {
113+
self.cached_typeinfo.lock().typeinfo = Some(statement.clone());
114+
}
112115
}
113116

114117
pub fn typeinfo_composite(&self) -> Option<Statement> {
115118
self.cached_typeinfo.lock().typeinfo_composite.clone()
116119
}
117120

118121
pub fn set_typeinfo_composite(&self, statement: &Statement) {
119-
self.cached_typeinfo.lock().typeinfo_composite = Some(statement.clone());
122+
if !self.transaction_pool_mode {
123+
self.cached_typeinfo.lock().typeinfo_composite = Some(statement.clone());
124+
}
120125
}
121126

122127
pub fn typeinfo_enum(&self) -> Option<Statement> {
123128
self.cached_typeinfo.lock().typeinfo_enum.clone()
124129
}
125130

126131
pub fn set_typeinfo_enum(&self, statement: &Statement) {
127-
self.cached_typeinfo.lock().typeinfo_enum = Some(statement.clone());
132+
if !self.transaction_pool_mode {
133+
self.cached_typeinfo.lock().typeinfo_enum = Some(statement.clone());
134+
}
128135
}
129136

130137
pub fn type_(&self, oid: Oid) -> Option<Type> {
131138
self.cached_typeinfo.lock().types.get(&oid).cloned()
132139
}
133140

134141
pub fn set_type(&self, oid: Oid, type_: &Type) {
135-
self.cached_typeinfo.lock().types.insert(oid, type_.clone());
142+
if !self.transaction_pool_mode {
143+
self.cached_typeinfo.lock().types.insert(oid, type_.clone());
144+
}
136145
}
137146

138147
pub fn clear_type_cache(&self) {
@@ -190,12 +199,14 @@ impl Client {
190199
ssl_mode: SslMode,
191200
process_id: i32,
192201
secret_key: i32,
202+
transaction_pool_mode: bool,
193203
) -> Client {
194204
Client {
195205
inner: Arc::new(InnerClient {
196206
sender,
197207
cached_typeinfo: Default::default(),
198208
buffer: Default::default(),
209+
transaction_pool_mode,
199210
}),
200211
#[cfg(feature = "runtime")]
201212
socket_config: None,

tokio-postgres/src/config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ pub struct Config {
210210
pub(crate) channel_binding: ChannelBinding,
211211
pub(crate) load_balance_hosts: LoadBalanceHosts,
212212
pub(crate) max_backend_message_size: Option<usize>,
213+
pub(crate) transaction_pool_mode: bool,
213214
}
214215

215216
impl Default for Config {
@@ -244,6 +245,7 @@ impl Config {
244245
channel_binding: ChannelBinding::Prefer,
245246
load_balance_hosts: LoadBalanceHosts::Disable,
246247
max_backend_message_size: None,
248+
transaction_pool_mode: false,
247249
}
248250
}
249251

@@ -511,6 +513,20 @@ impl Config {
511513
self.channel_binding
512514
}
513515

516+
/// When enabled, the client skips all internal caching for statements,
517+
/// allowing usage with a connection pool with transaction mode.
518+
///
519+
/// Defaults to `false`.
520+
pub fn transaction_pool_mode(&mut self, enable: bool) -> &mut Config {
521+
self.transaction_pool_mode = enable;
522+
self
523+
}
524+
525+
/// Gets the transaction pool mode status.
526+
pub fn get_transaction_pool_mode(&self) -> bool {
527+
self.transaction_pool_mode
528+
}
529+
514530
/// Sets the host load balancing behavior.
515531
///
516532
/// Defaults to `disable`.

tokio-postgres/src/connect_raw.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,15 @@ where
112112
let (process_id, secret_key, parameters) = read_info(&mut stream).await?;
113113

114114
let (sender, receiver) = mpsc::unbounded();
115-
let client = Client::new(sender, config.ssl_mode, process_id, secret_key);
115+
116+
let client = Client::new(
117+
sender,
118+
config.ssl_mode,
119+
process_id,
120+
secret_key,
121+
config.transaction_pool_mode,
122+
);
123+
116124
let connection = Connection::new(stream.inner, stream.delayed, parameters, receiver);
117125

118126
Ok((client, connection))

0 commit comments

Comments
 (0)