1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! Connection pool for PostgreSQL.
use std::sync::Arc;
use std::time::Duration;
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod, Runtime};
use tokio_postgres::NoTls;
use tracing::{debug, info};
use crate::config::PgConfig;
use crate::connection::PgConnection;
use crate::error::{PgError, PgResult};
use crate::statement::PreparedStatementCache;
/// A connection pool for PostgreSQL.
#[derive(Clone)]
pub struct PgPool {
inner: Pool,
config: Arc<PgConfig>,
statement_cache: Arc<PreparedStatementCache>,
}
impl PgPool {
/// Create a new connection pool from configuration.
pub async fn new(config: PgConfig) -> PgResult<Self> {
Self::with_pool_config(config, PoolConfig::default()).await
}
/// Create a new connection pool with custom pool configuration.
pub async fn with_pool_config(config: PgConfig, pool_config: PoolConfig) -> PgResult<Self> {
let pg_config = config.to_pg_config();
let mgr_config = ManagerConfig {
recycling_method: RecyclingMethod::Fast,
};
let mgr = Manager::from_config(pg_config, NoTls, mgr_config);
// Build pool - set runtime to tokio for timeout support
let mut builder = Pool::builder(mgr).max_size(pool_config.max_connections);
// Only set timeouts if they are configured
if let Some(timeout) = pool_config.connection_timeout {
builder = builder
.wait_timeout(Some(timeout))
.create_timeout(Some(timeout));
}
if let Some(timeout) = pool_config.idle_timeout {
builder = builder.recycle_timeout(Some(timeout));
}
// Set runtime for timeout support
builder = builder.runtime(Runtime::Tokio1);
let pool = builder
.build()
.map_err(|e| PgError::config(format!("failed to create pool: {}", e)))?;
info!(
host = %config.host,
port = %config.port,
database = %config.database,
max_connections = %pool_config.max_connections,
"PostgreSQL connection pool created"
);
Ok(Self {
inner: pool,
config: Arc::new(config),
statement_cache: Arc::new(PreparedStatementCache::new(
pool_config.statement_cache_size,
)),
})
}
/// Get a connection from the pool.
pub async fn get(&self) -> PgResult<PgConnection> {
debug!("Acquiring connection from pool");
let client = self.inner.get().await?;
Ok(PgConnection::new(client, self.statement_cache.clone()))
}
/// Borrow the underlying `deadpool_postgres::Pool`.
///
/// Reserved for intra-crate paths that need a raw `Object` (e.g.
/// [`crate::engine::PgEngine::transaction`], which pins a single
/// connection for the lifetime of an in-flight transaction). The
/// standard path is [`PgPool::get`], which returns a
/// cache-wrapped [`PgConnection`].
pub(crate) fn inner(&self) -> &Pool {
&self.inner
}
/// Get the current pool status.
pub fn status(&self) -> PoolStatus {
let status = self.inner.status();
PoolStatus {
available: status.available,
size: status.size,
max_size: status.max_size,
waiting: status.waiting,
}
}
/// Get the pool configuration.
pub fn config(&self) -> &PgConfig {
&self.config
}
/// Check if the pool is healthy by attempting to get a connection.
pub async fn is_healthy(&self) -> bool {
match self.inner.get().await {
Ok(client) => {
// Try a simple query to verify the connection is actually working
client.query_one("SELECT 1", &[]).await.is_ok()
}
Err(_) => false,
}
}
/// Close the pool and all connections.
pub fn close(&self) {
self.inner.close();
info!("PostgreSQL connection pool closed");
}
/// Create a builder for configuring the pool.
pub fn builder() -> PgPoolBuilder {
PgPoolBuilder::new()
}
/// Warm up the connection pool by pre-establishing connections.
///
/// This eliminates the latency of establishing connections on the first queries.
/// The `count` parameter specifies how many connections to pre-establish.
///
/// # Example
///
/// ```rust,ignore
/// let pool = PgPool::builder()
/// .url("postgresql://localhost/db")
/// .max_connections(10)
/// .build()
/// .await?;
///
/// // Pre-establish 5 connections
/// pool.warmup(5).await?;
/// ```
pub async fn warmup(&self, count: usize) -> PgResult<()> {
info!(count = count, "Warming up connection pool");
let count = count.min(self.inner.status().max_size);
let mut connections = Vec::with_capacity(count);
// Acquire connections to force establishment
for i in 0..count {
match self.inner.get().await {
Ok(conn) => {
// Validate the connection with a simple query
if let Err(e) = conn.query_one("SELECT 1", &[]).await {
debug!(error = %e, "Warmup connection {} failed validation", i);
} else {
debug!("Warmup connection {} established", i);
connections.push(conn);
}
}
Err(e) => {
debug!(error = %e, "Failed to establish warmup connection {}", i);
}
}
}
// Connections are returned to pool when dropped
let established = connections.len();
drop(connections);
info!(
established = established,
requested = count,
"Connection pool warmup complete"
);
Ok(())
}
/// Warm up with common prepared statements.
///
/// This pre-prepares common SQL statements on warmed connections,
/// eliminating the prepare latency on first use.
pub async fn warmup_with_statements(&self, count: usize, statements: &[&str]) -> PgResult<()> {
info!(
count = count,
statements = statements.len(),
"Warming up connection pool with prepared statements"
);
let count = count.min(self.inner.status().max_size);
let mut connections = Vec::with_capacity(count);
for i in 0..count {
match self.inner.get().await {
Ok(conn) => {
// Pre-prepare all statements
for sql in statements {
if let Err(e) = conn.prepare_cached(sql).await {
debug!(error = %e, sql = %sql, "Failed to prepare statement");
}
}
debug!(
connection = i,
statements = statements.len(),
"Prepared statements on connection"
);
connections.push(conn);
}
Err(e) => {
debug!(error = %e, "Failed to establish warmup connection {}", i);
}
}
}
let established = connections.len();
drop(connections);
info!(
established = established,
"Connection pool warmup with statements complete"
);
Ok(())
}
}
/// Pool status information.
#[derive(Debug, Clone)]
pub struct PoolStatus {
/// Number of available (idle) connections.
pub available: usize,
/// Current total size of the pool.
pub size: usize,
/// Maximum size of the pool.
pub max_size: usize,
/// Number of tasks waiting for a connection.
pub waiting: usize,
}
/// Configuration for the connection pool.
#[derive(Debug, Clone)]
pub struct PoolConfig {
/// Maximum number of connections in the pool.
pub max_connections: usize,
/// Minimum number of connections to keep alive.
pub min_connections: usize,
/// Maximum time to wait for a connection.
pub connection_timeout: Option<Duration>,
/// Maximum idle time before a connection is closed.
pub idle_timeout: Option<Duration>,
/// Maximum lifetime of a connection.
pub max_lifetime: Option<Duration>,
/// Size of the prepared statement cache per connection.
pub statement_cache_size: usize,
}
impl Default for PoolConfig {
fn default() -> Self {
Self {
max_connections: 10,
min_connections: 1,
connection_timeout: Some(Duration::from_secs(30)),
idle_timeout: Some(Duration::from_secs(600)), // 10 minutes
max_lifetime: Some(Duration::from_secs(1800)), // 30 minutes
statement_cache_size: 100,
}
}
}
/// Builder for creating a connection pool.
#[derive(Debug, Default)]
pub struct PgPoolBuilder {
config: Option<PgConfig>,
url: Option<String>,
pool_config: PoolConfig,
}
impl PgPoolBuilder {
/// Create a new pool builder.
pub fn new() -> Self {
Self {
config: None,
url: None,
pool_config: PoolConfig::default(),
}
}
/// Set the database URL.
pub fn url(mut self, url: impl Into<String>) -> Self {
self.url = Some(url.into());
self
}
/// Set the configuration.
pub fn config(mut self, config: PgConfig) -> Self {
self.config = Some(config);
self
}
/// Set the maximum number of connections.
pub fn max_connections(mut self, n: usize) -> Self {
self.pool_config.max_connections = n;
self
}
/// Set the minimum number of connections.
pub fn min_connections(mut self, n: usize) -> Self {
self.pool_config.min_connections = n;
self
}
/// Set the connection timeout.
pub fn connection_timeout(mut self, timeout: Duration) -> Self {
self.pool_config.connection_timeout = Some(timeout);
self
}
/// Set the idle timeout.
pub fn idle_timeout(mut self, timeout: Duration) -> Self {
self.pool_config.idle_timeout = Some(timeout);
self
}
/// Set the maximum connection lifetime.
pub fn max_lifetime(mut self, lifetime: Duration) -> Self {
self.pool_config.max_lifetime = Some(lifetime);
self
}
/// Set the prepared statement cache size.
pub fn statement_cache_size(mut self, size: usize) -> Self {
self.pool_config.statement_cache_size = size;
self
}
/// Build the connection pool.
pub async fn build(self) -> PgResult<PgPool> {
let config = if let Some(config) = self.config {
config
} else if let Some(url) = self.url {
PgConfig::from_url(url)?
} else {
return Err(PgError::config("no database URL or config provided"));
};
PgPool::with_pool_config(config, self.pool_config).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pool_config_default() {
let config = PoolConfig::default();
assert_eq!(config.max_connections, 10);
assert_eq!(config.min_connections, 1);
assert_eq!(config.statement_cache_size, 100);
}
#[test]
fn test_pool_builder() {
let builder = PgPoolBuilder::new()
.url("postgresql://localhost/test")
.max_connections(20)
.statement_cache_size(200);
assert!(builder.url.is_some());
assert_eq!(builder.pool_config.max_connections, 20);
assert_eq!(builder.pool_config.statement_cache_size, 200);
}
}