elefant-client 0.1.0

A pure rust implementation of a postgres client that is independent of the executor runtime
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Mutex};

use crate::protocol::async_io::ElefantAsyncReadWrite;
use crate::protocol::PostgresConnection;
use crate::types::EnumTypeRegistry;
use crate::{ElefantClientError, PostgresConnectionSettings};
use tracing::debug;

pub trait ConnectionFactory {
    type Connection: ElefantAsyncReadWrite;

    fn create_connection(
        &self,
        settings: &PostgresConnectionSettings,
    ) -> impl std::future::Future<Output = Result<Self::Connection, ElefantClientError>>;
}

pub struct PostgresPool<F: ConnectionFactory>(Arc<PostgresPoolInner<F>>);

struct PostgresPoolInner<F: ConnectionFactory> {
    factory: F,
    settings: PostgresConnectionSettings,
    idle_connections: Mutex<Vec<crate::postgres_client::PostgresClient<F>>>,
    enum_registry: Arc<EnumTypeRegistry>,
}

impl<F: ConnectionFactory> Clone for PostgresPool<F> {
    fn clone(&self) -> Self {
        PostgresPool(Arc::clone(&self.0))
    }
}

impl<F: ConnectionFactory> PostgresPool<F> {
    pub async fn new(
        factory: F,
        settings: PostgresConnectionSettings,
    ) -> Result<Self, ElefantClientError> {
        let empty_registry = Arc::new(EnumTypeRegistry::new());

        if settings.enum_type_names().is_empty() {
            return Ok(PostgresPool(Arc::new(PostgresPoolInner {
                factory,
                settings,
                idle_connections: Mutex::new(Vec::new()),
                enum_registry: empty_registry,
            })));
        }

        // Create a temporary connection to query enum OIDs
        let raw_stream = factory.create_connection(&settings).await?;
        let (connection, channel_binding_data) =
            PostgresConnection::new_maybe_tls(raw_stream, &settings).await?;
        let mut client = crate::postgres_client::PostgresClient::new(
            connection,
            &settings,
            empty_registry,
            channel_binding_data,
        )
        .await?;

        // Build the SQL query filtering by the registered enum names
        let names_sql = settings
            .enum_type_names()
            .iter()
            .map(|n| format!("'{}'", n.replace('\'', "''")))
            .collect::<Vec<_>>()
            .join(", ");

        let query = format!(
            "SELECT t.typname, n.nspname, t.oid::int4, t.typarray::int4 \
             FROM pg_catalog.pg_type t \
             JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid \
             WHERE t.typtype = 'e' AND (t.typname IN ({names_sql}) \
                   OR (n.nspname || '.' || t.typname) IN ({names_sql}))"
        );

        let mut registry = EnumTypeRegistry::new();

        let mut query_result = client.query_simple(&query).await?;
        loop {
            let result_set = query_result.next_result_set().await?;
            match result_set {
                crate::postgres_client::QueryResultSet::QueryProcessingComplete => break,
                crate::postgres_client::QueryResultSet::RowDescriptionReceived(mut row_reader) => {
                    while let Some(row) = row_reader.next_row().await? {
                        let typname: &str = row.get_text(0)?;
                        let nspname: &str = row.get_text(1)?;
                        let oid: i32 = row.get_text(2)?;
                        let typarray: i32 = row.get_text(3)?;

                        let qualified_name = format!("{nspname}.{typname}");

                        // Insert under the unqualified name
                        registry.insert(
                            typname.to_string(),
                            crate::types::EnumOidEntry {
                                oid,
                                array_oid: typarray,
                                schema: nspname.to_string(),
                            },
                        );

                        // Also insert under the schema-qualified name
                        registry.insert(
                            qualified_name,
                            crate::types::EnumOidEntry {
                                oid,
                                array_oid: typarray,
                                schema: nspname.to_string(),
                            },
                        );
                    }
                }
            }
        }

        let registry = Arc::new(registry);
        client.enum_registry = registry.clone();

        // Return the client to the idle pool for reuse
        Ok(PostgresPool(Arc::new(PostgresPoolInner {
            factory,
            settings,
            idle_connections: Mutex::new(vec![client]),
            enum_registry: registry,
        })))
    }

    pub fn settings(&self) -> &PostgresConnectionSettings {
        &self.0.settings
    }

    pub fn enum_registry(&self) -> &Arc<EnumTypeRegistry> {
        &self.0.enum_registry
    }

    pub(crate) fn return_connection(&self, mut client: crate::postgres_client::PostgresClient<F>) {
        client.pool = None;
        self.0.idle_connections.lock().unwrap().push(client);
    }

    /// Gracefully close all idle connections by sending Terminate to each.
    /// Currently checked-out clients are not affected — close them individually
    /// via [`PoolableClient::close`] or let them return to the pool first.
    pub async fn close(&self) -> Result<(), ElefantClientError> {
        let clients: Vec<_> = self.0.idle_connections.lock().unwrap().drain(..).collect();
        for client in clients {
            client.close().await?;
        }
        Ok(())
    }

    pub async fn get_client(&self) -> Result<PoolableClient<F>, ElefantClientError> {
        // Try to reuse an idle connection
        loop {
            let idle_client = { self.0.idle_connections.lock().unwrap().pop() };
            match idle_client {
                Some(mut client) => match client.reset().await {
                    Ok(()) => {
                        client.pool = Some(self.clone());
                        client.enum_registry = self.0.enum_registry.clone();
                        return Ok(PoolableClient {
                            client: Some(client),
                            pool: self.clone(),
                        });
                    }
                    Err(e) => {
                        debug!("Idle connection reset failed, discarding: {:?}", e);
                        continue;
                    }
                },
                None => break,
            }
        }

        // No idle connection available, create a new one
        let raw_stream = self.0.factory.create_connection(&self.0.settings).await?;
        let (connection, channel_binding_data) =
            PostgresConnection::new_maybe_tls(raw_stream, &self.0.settings).await?;
        let mut client = crate::postgres_client::PostgresClient::new(
            connection,
            &self.0.settings,
            self.0.enum_registry.clone(),
            channel_binding_data,
        )
        .await?;
        client.pool = Some(self.clone());
        Ok(PoolableClient {
            client: Some(client),
            pool: self.clone(),
        })
    }
}

pub struct PoolableClient<F: ConnectionFactory> {
    client: Option<crate::postgres_client::PostgresClient<F>>,
    pool: PostgresPool<F>,
}

impl<F: ConnectionFactory> Deref for PoolableClient<F> {
    type Target = crate::postgres_client::PostgresClient<F>;

    fn deref(&self) -> &Self::Target {
        self.client
            .as_ref()
            .expect("client already returned to pool")
    }
}

impl<F: ConnectionFactory> DerefMut for PoolableClient<F> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.client
            .as_mut()
            .expect("client already returned to pool")
    }
}

impl<F: ConnectionFactory> PoolableClient<F> {
    /// Gracefully close this connection by sending Terminate to the backend.
    /// The connection is not returned to the pool. Consumes the client.
    pub async fn close(mut self) -> Result<(), ElefantClientError> {
        match self.client.take() {
            Some(client) => client.close().await,
            None => Ok(()),
        }
    }
}

impl<F: ConnectionFactory> Drop for PoolableClient<F> {
    fn drop(&mut self) {
        if let Some(client) = self.client.take() {
            self.pool.return_connection(client);
        }
    }
}

#[cfg(test)]
impl<F: ConnectionFactory> PostgresPool<F> {
    pub(crate) fn idle_connection_count(&self) -> usize {
        self.0.idle_connections.lock().unwrap().len()
    }
}

#[cfg(all(test, feature = "tokio"))]
mod tests {
    use crate::test_helpers::get_settings;
    use crate::tokio_connection::{new_client, TokioConnectionFactory, TokioPostgresPool};

    #[tokio::test]
    async fn pool_reuses_connection_after_drop() {
        let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();

        let pid1: i32;
        {
            let mut client = pool.get_client().await.unwrap();
            pid1 = client
                .read_single_value_simple("select pg_backend_pid()")
                .await;
        }

        let mut client2 = pool.get_client().await.unwrap();
        let pid2: i32 = client2
            .read_single_value_simple("select pg_backend_pid()")
            .await;

        assert_eq!(pid1, pid2, "Pool should reuse the same connection");
    }

    #[tokio::test]
    async fn pool_creates_new_connection_when_all_checked_out() {
        let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();

        let mut client1 = pool.get_client().await.unwrap();
        let mut client2 = pool.get_client().await.unwrap();

        let pid1: i32 = client1
            .read_single_value_simple("select pg_backend_pid()")
            .await;
        let pid2: i32 = client2
            .read_single_value_simple("select pg_backend_pid()")
            .await;

        assert_ne!(
            pid1, pid2,
            "Simultaneously checked-out clients must be different connections"
        );
    }

    #[tokio::test]
    async fn pool_idle_count_grows_on_drop() {
        let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();

        assert_eq!(pool.idle_connection_count(), 0);

        let client1 = pool.get_client().await.unwrap();
        let client2 = pool.get_client().await.unwrap();
        assert_eq!(pool.idle_connection_count(), 0);

        drop(client1);
        assert_eq!(pool.idle_connection_count(), 1);

        drop(client2);
        assert_eq!(pool.idle_connection_count(), 2);
    }

    #[tokio::test]
    async fn pool_reset_rolls_back_uncommitted_transaction() {
        let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();

        {
            let mut client = pool.get_client().await.unwrap();
            client.execute_non_query_simple("BEGIN").await.unwrap();
            client
                .execute_non_query_simple("CREATE TEMP TABLE pool_txn_test (id int)")
                .await
                .unwrap();
            // Intentionally do NOT commit -- drop returns to pool
        }

        // get_client() calls reset() which rolls back the transaction
        let mut client = pool.get_client().await.unwrap();

        // The temp table should not exist because the transaction was rolled back
        let result = client
            .execute_non_query_simple("SELECT 1 FROM pool_txn_test")
            .await;
        assert!(
            result.is_err(),
            "Temp table should not exist after rollback"
        );
    }

    #[tokio::test]
    async fn pool_reset_recovers_from_failed_transaction() {
        let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();

        {
            let mut client = pool.get_client().await.unwrap();
            client.execute_non_query_simple("BEGIN").await.unwrap();
            // This will fail and put the connection into InFailedTransaction state
            let _ = client
                .execute_non_query_simple("SELECT * FROM nonexistent_table_that_does_not_exist")
                .await;
            // Drop without ROLLBACK or COMMIT
        }

        // reset() should detect InFailedTransaction and issue ROLLBACK
        let mut client = pool.get_client().await.unwrap();
        let value: i32 = client.read_single_value_simple("select 42").await;
        assert_eq!(value, 42);
    }

    #[tokio::test]
    async fn pool_clone_shares_state() {
        let pool1 = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();
        let pool2 = pool1.clone();

        let pid1: i32;
        {
            let mut client = pool1.get_client().await.unwrap();
            pid1 = client
                .read_single_value_simple("select pg_backend_pid()")
                .await;
        }

        let mut client2 = pool2.get_client().await.unwrap();
        let pid2: i32 = client2
            .read_single_value_simple("select pg_backend_pid()")
            .await;

        assert_eq!(
            pid1, pid2,
            "Cloned pool should share the idle connection vec"
        );
    }

    #[tokio::test]
    async fn pool_client_works_across_multiple_reuse_cycles() {
        let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();

        for i in 0..5 {
            let mut client = pool.get_client().await.unwrap();
            let value: i32 = client
                .read_single_value_simple(&format!("select {}", i + 1))
                .await;
            assert_eq!(value, i + 1);
        }
    }

    #[tokio::test]
    async fn pool_close_terminates_connections() {
        let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
            .await
            .unwrap();

        let mut client1 = pool.get_client().await.unwrap();
        let mut client2 = pool.get_client().await.unwrap();

        let pid1: i32 = client1
            .read_single_value_simple("select pg_backend_pid()")
            .await;
        let pid2: i32 = client2
            .read_single_value_simple("select pg_backend_pid()")
            .await;

        // Close one client directly (bypasses pool return)
        client1.close().await.unwrap();

        // Drop the other so it returns to the pool's idle vec
        drop(client2);
        assert_eq!(pool.idle_connection_count(), 1);

        // Close idle connections in the pool
        pool.close().await.unwrap();
        assert_eq!(pool.idle_connection_count(), 0);

        // No sleep needed — Terminate triggers immediate backend cleanup
        let mut checker = new_client(get_settings()).await.unwrap();
        let count: i64 = checker
            .read_single_value_simple(&format!(
                "select count(*) from pg_stat_activity where pid in ({}, {})",
                pid1, pid2
            ))
            .await;
        assert_eq!(
            count, 0,
            "All connections should be closed after graceful shutdown"
        );
    }

    #[tokio::test]
    async fn pool_drop_closes_all_connections() {
        let pid1: i32;
        let pid2: i32;

        {
            let pool = TokioPostgresPool::new(TokioConnectionFactory, get_settings())
                .await
                .unwrap();

            let mut client1 = pool.get_client().await.unwrap();
            let mut client2 = pool.get_client().await.unwrap();

            pid1 = client1
                .read_single_value_simple("select pg_backend_pid()")
                .await;
            pid2 = client2
                .read_single_value_simple("select pg_backend_pid()")
                .await;

            // Drop clients first (returns to idle vec), then drop pool (closes connections)
            drop(client1);
            drop(client2);
        }
        // pool is dropped here -- Arc refcount hits 0, idle connections are dropped, TCP streams close

        // Small delay to let PostgreSQL clean up the backend processes
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        // Use a separate connection to verify the old connections are gone
        let mut checker = new_client(get_settings()).await.unwrap();
        let count: i64 = checker
            .read_single_value_simple(&format!(
                "select count(*) from pg_stat_activity where pid in ({}, {})",
                pid1, pid2
            ))
            .await;
        assert_eq!(
            count, 0,
            "All pool connections should be closed after pool drop"
        );
    }
}