Module pool
June 1, 2026 · View on GitHub
Module pool
Pool — a managed connection pool for high-throughput PostgreSQL access.
The pool maintains a set of pre-opened Connection objects and lends them
to callers on demand. When a query completes (or a cursor is closed), the
connection is automatically returned to the pool rather than being closed.
Design
- Min connections —
pool.minconnections are created eagerly at startup and kept alive indefinitely. - Max connections — the pool will never open more than
pool.maxconnections simultaneously. - Idle eviction — connections idle for longer than
pool.idleTimeoutMillisare closed and removed from the pool. - Acquire timeout — if no connection is available within
pool.acquireTimeoutMillis, aPoolExhaustedErroris raised.
Example
import postgres { Pool }
var db = Pool({
host: 'localhost',
database: 'mydb',
user: 'alice',
password: 'secret',
pool: {
min: 2,
max: 10,
idleTimeoutMillis: 30000,
acquireTimeoutMillis: 5000,
}
})
var result = db.query('select count(*) from events')
echo result.rows[0]
db.close()
Classes
class Pool
A connection pool that manages a set of Connection objects.
This module implements the following decorated functions: @to_string.
Properties
-
min_size(number)Minimum number of connections kept open in the pool.
-
max_size(number)Maximum number of connections the pool may open simultaneously.
-
idle_timeout(number)Milliseconds a connection may sit idle before being closed. 0 = never evict.
-
acquire_timeout(number)Milliseconds to wait for an available connection before raising
PoolExhaustedError.
Methods
-
Pool(options)(Constructor)Creates a connection pool.
Parameters
- dict options Connection options merged with pool-specific options.
Pool-specific options (nested under
poolkey)Key Type Default Description minnumber 1Minimum connections. maxnumber 10Maximum connections. idleTimeoutMillisnumber 30000Idle eviction timeout (ms). acquireTimeoutMillisnumber 5000Max wait for a connection (ms).
- dict options Connection options merged with pool-specific options.
-
query(sql, options)Acquires a connection from the pool, executes the query, then returns the connection to the pool. Supports the same
optionsdict asConnection.query(). Whenoptions.cursoristrue, the returned cursor'sclose()method triggers the connection return.You must callcursor.close().*Parameters
-
string sql The SQL statement.
-
dict options Query options.
-
-
execute(sql, options)Acquires a connection, executes a statement, and releases the connection.
Parameters
-
string sql SQL statement.
-
dict options Query options.
-
-
with_connection(fn)Runs
fn(connection)with an acquired connection, automatically releasing it afterwards. Useful for multi-statement transactions.db.with_connection(@(conn) { conn.begin() conn.execute('update accounts set balance = balance - 100 where id = \$1', { params: [42] }) conn.execute('update accounts set balance = balance + 100 where id = \$1', { params: [99] }) conn.commit() })Parameters
- function fn Callback receiving the
Connectionobject.
- function fn Callback receiving the
-
close()Closes all connections in the pool (both idle and active) and shuts the pool down. Any in-flight queries will complete normally; subsequent calls to
query()will raiseClosedError. -
stats()Returns a snapshot of pool statistics.