pg_stream
A low-level, zero-overhead Rust implementation of the Postgres wire protocol.
Overview
pg_stream provides direct access to the Postgres frontend/backend protocol, giving you full control over connection management, query execution, and data transfer. Unlike higher-level database libraries, this crate focuses on protocol implementation without abstraction overhead.
Features
- Zero-copy protocol handling - Direct buffer manipulation for maximum performance
- TLS support - Built-in SSL/TLS negotiation with custom upgrade functions
- Extended query protocol - Full support for prepared statements, portals, and parameter binding
- Function calls - Direct invocation of Postgres functions via protocol messages
- Type-safe message construction - Fluent API for building protocol messages
- Format optimization - Automatic optimization of format codes in bind and function call messages
Quick Start
use ;
async
Authentication
Supported authentication modes:
AuthenticationMode::Trust- No password requiredAuthenticationMode::Password(String)- Cleartext password authentication
Other authentication methods (SASL, MD5, Kerberos, etc.) are not yet implemented.
Extended Query Protocol
The crate provides full support for the extended query protocol with prepared statements:
// Parse a prepared statement
conn.put_parse
.flush
.await?;
// Bind parameters and execute
conn.put_bind
.put_execute
.put_sync
.flush
.await?;
Function Calls
Call Postgres functions directly via the protocol:
use ;
// Call sqrt function (OID 1344)
conn.put_fn_call
.flush
.await?;
Note: Function OIDs are not guaranteed to be stable across Postgres versions or installations. Look them up dynamically via system catalogs for production use.
TLS Support
Connect with TLS using a custom upgrade function:
let stream = connect.await.unwrap;
stream.set_nodelay.unwrap;
let = new
.connect_with_tls
.await
.unwrap;
Protocol Messages
The crate supports all major frontend protocol messages:
- Simple Query -
put_query() - Parse -
put_parse()for prepared statements - Bind -
put_bind()to bind parameters - Describe -
put_describe()for statement/portal metadata - Execute -
put_execute()to run a portal - Close -
put_close()to deallocate resources - Flush -
put_flush()to send buffered messages - Sync -
put_sync()to end an extended query sequence - Function Call -
put_fn_call()to invoke functions
Performance
This crate is designed for scenarios where you need maximum control and minimum overhead:
- Direct buffer manipulation with
bytes::BytesMut - No allocations in the hot path for protocol framing
- Zero-copy reads where possible
- Efficient format code optimization
- Minimum dependencies
Safety and Limitations
- No SQL injection protection - You are responsible for sanitizing inputs
- No connection pooling - Single connection per
PgStream - Manual resource management - You must close statements and portals
- Incomplete auth support - Only Trust, scram, and cleartext password