Expand description
§chopin-pg
A zero-copy, high-performance PostgreSQL driver designed for the Chopin thread-per-core, Shared-Nothing architecture.
§Features
- Thread-per-core: Each worker owns its own PG connections.
- Non-blocking I/O: Socket is set to non-blocking after connect; all reads/writes go through poll-based primitives with configurable timeouts.
- Zero-copy: Row data is sliced directly from the read buffer.
- SCRAM-SHA-256: Full authentication support.
- Extended Query Protocol: Parse/Bind/Execute with implicit caching.
- Transaction support: Safe closure-based API with auto-rollback.
- COPY protocol: Both COPY IN (writer) and COPY OUT (reader).
- LISTEN/NOTIFY: Notification buffering during query processing.
- Rich types: UUID, Date, Time, Timestamp, Interval, Numeric, INET, Arrays.
- Binary INET/CIDR: Proper binary encoding/decoding for network types.
- Type-safe queries:
ToSql/FromSqltraits for ergonomic parameter passing. - Connection pool: Worker-local pool with RAII
ConnectionGuard, FIFO idle queue,try_get()/get()with timeout, and automatic return on drop. - Error classification: Transient vs permanent errors for retry logic.
§Quick Start
ⓘ
use chopin_pg::{PgConfig, PgConnection};
let config = PgConfig::new("localhost", 5432, "user", "pass", "mydb");
let mut conn = PgConnection::connect(&config)?;
// Simple query
let rows = conn.query("SELECT $1::int4 + $2::int4", &[&1i32, &2i32])?;
let sum: i32 = rows[0].get_typed(0)?;
// Transaction with closure
conn.transaction(|tx| {
tx.execute("INSERT INTO users (name) VALUES ($1)", &[&"Alice"])?;
Ok(())
})?;§Pool Usage
ⓘ
use chopin_pg::{PgConfig, PgPool, PgPoolConfig};
let config = PgConfig::new("localhost", 5432, "user", "pass", "mydb");
let pool_cfg = PgPoolConfig::new().max_size(25).checkout_timeout(Duration::from_secs(3));
let mut pool = PgPool::connect_with_config(config, pool_cfg)?;
// Get a connection — returned to pool when guard drops
let mut guard = pool.get()?;
let rows = guard.query("SELECT 1", &[])?;
// guard drops → connection returned to idle queueRe-exports§
pub use connection::CopyReader;pub use connection::CopyWriter;pub use connection::Notification;pub use connection::PgConfig;pub use connection::PgConnection;pub use connection::Transaction;pub use error::ErrorClass;pub use error::PgError;pub use error::PgResult;pub use pool::ConnectionGuard;pub use pool::PgPool;pub use pool::PgPoolConfig;pub use pool::PoolStats;pub use row::Row;pub use statement::Statement;pub use types::FromSql;pub use types::PgValue;pub use types::ToParam;pub use types::ToSql;pub use types::TypeRegistry;pub use types::encode_inet_binary;
Modules§
- auth
- SCRAM-SHA-256 authentication for PostgreSQL.
- codec
- Zero-copy binary codec for PostgreSQL v3 wire protocol.
- connection
- PostgreSQL connection with poll-based non-blocking I/O.
- error
- pool
- Worker-local connection pool with RAII guards for Shared-Nothing architecture.
- protocol
- PostgreSQL v3 Wire Protocol message definitions.
- row
- Row abstraction for query results with inline small-value optimisation.
- statement
- Implicit statement caching with LRU eviction.
- types
- PostgreSQL type system — type OIDs, value conversions, and ToSql/FromSql traits.