pub struct PgConnection { /* private fields */ }Expand description
A raw PostgreSQL connection.
Implementations§
Source§impl PgConnection
impl PgConnection
Sourcepub fn get_cancel_key(&self) -> (i32, i32)
pub fn get_cancel_key(&self) -> (i32, i32)
Get the cancel key for this connection.
Source§impl PgConnection
impl PgConnection
Sourcepub async fn connect(
host: &str,
port: u16,
user: &str,
database: &str,
) -> PgResult<Self>
pub async fn connect( host: &str, port: u16, user: &str, database: &str, ) -> PgResult<Self>
Connect to PostgreSQL server without authentication (trust mode).
Sourcepub async fn connect_with_password(
host: &str,
port: u16,
user: &str,
database: &str,
password: Option<&str>,
) -> PgResult<Self>
pub async fn connect_with_password( host: &str, port: u16, user: &str, database: &str, password: Option<&str>, ) -> PgResult<Self>
Connect to PostgreSQL server with optional password authentication.
Sourcepub async fn connect_tls(
host: &str,
port: u16,
user: &str,
database: &str,
password: Option<&str>,
) -> PgResult<Self>
pub async fn connect_tls( host: &str, port: u16, user: &str, database: &str, password: Option<&str>, ) -> PgResult<Self>
Connect to PostgreSQL server with TLS encryption.
Sourcepub async fn connect_mtls(
host: &str,
port: u16,
user: &str,
database: &str,
config: TlsConfig,
) -> PgResult<Self>
pub async fn connect_mtls( host: &str, port: u16, user: &str, database: &str, config: TlsConfig, ) -> PgResult<Self>
Connect with mutual TLS (client certificate authentication).
§Arguments
host- PostgreSQL server hostnameport- PostgreSQL server portuser- Database userdatabase- Database nameconfig- TLS configuration with client cert/key
§Example
let config = TlsConfig {
client_cert_pem: include_bytes!("client.crt").to_vec(),
client_key_pem: include_bytes!("client.key").to_vec(),
ca_cert_pem: Some(include_bytes!("ca.crt").to_vec()),
};
let conn = PgConnection::connect_mtls("localhost", 5432, "user", "db", config).await?;Source§impl PgConnection
impl PgConnection
Sourcepub async fn copy_in_raw(
&mut self,
table: &str,
columns: &[String],
data: &[u8],
) -> PgResult<u64>
pub async fn copy_in_raw( &mut self, table: &str, columns: &[String], data: &[u8], ) -> PgResult<u64>
Fastest bulk insert using COPY protocol with pre-encoded data. Accepts raw COPY text format bytes, no encoding needed. Use when caller has already encoded rows to COPY format.
§Format
Data should be tab-separated rows with newlines:
1\thello\t3.14\n2\tworld\t2.71\n
Source§impl PgConnection
impl PgConnection
Sourcepub async fn send(&mut self, msg: FrontendMessage) -> PgResult<()>
pub async fn send(&mut self, msg: FrontendMessage) -> PgResult<()>
Send a frontend message.
Sourcepub async fn recv(&mut self) -> PgResult<BackendMessage>
pub async fn recv(&mut self) -> PgResult<BackendMessage>
Loops until a complete message is available.
Sourcepub async fn send_bytes(&mut self, bytes: &[u8]) -> PgResult<()>
pub async fn send_bytes(&mut self, bytes: &[u8]) -> PgResult<()>
Send raw bytes to the stream.
Sourcepub fn buffer_bytes(&mut self, bytes: &[u8])
pub fn buffer_bytes(&mut self, bytes: &[u8])
Buffer bytes for later flush (NO SYSCALL). Use flush_write_buf() to send all buffered data.
Sourcepub async fn flush_write_buf(&mut self) -> PgResult<()>
pub async fn flush_write_buf(&mut self) -> PgResult<()>
Flush the write buffer to the stream. This is the only syscall in the buffered write path.
Source§impl PgConnection
impl PgConnection
Sourcepub async fn query_pipeline(
&mut self,
queries: &[(&str, &[Option<Vec<u8>>])],
) -> PgResult<Vec<Vec<Vec<Option<Vec<u8>>>>>>
pub async fn query_pipeline( &mut self, queries: &[(&str, &[Option<Vec<u8>>])], ) -> PgResult<Vec<Vec<Vec<Option<Vec<u8>>>>>>
Execute multiple SQL queries in a single network round-trip (PIPELINING).
Sourcepub async fn pipeline_ast(
&mut self,
cmds: &[Qail],
) -> PgResult<Vec<Vec<Vec<Option<Vec<u8>>>>>>
pub async fn pipeline_ast( &mut self, cmds: &[Qail], ) -> PgResult<Vec<Vec<Vec<Option<Vec<u8>>>>>>
Execute multiple Qail ASTs in a single network round-trip.
Sourcepub async fn pipeline_ast_fast(&mut self, cmds: &[Qail]) -> PgResult<usize>
pub async fn pipeline_ast_fast(&mut self, cmds: &[Qail]) -> PgResult<usize>
FAST AST pipeline - returns only query count, no result parsing.
Sourcepub async fn pipeline_bytes_fast(
&mut self,
wire_bytes: &[u8],
expected_queries: usize,
) -> PgResult<usize>
pub async fn pipeline_bytes_fast( &mut self, wire_bytes: &[u8], expected_queries: usize, ) -> PgResult<usize>
FASTEST extended query pipeline - takes pre-encoded wire bytes.
Sourcepub async fn pipeline_simple_fast(&mut self, cmds: &[Qail]) -> PgResult<usize>
pub async fn pipeline_simple_fast(&mut self, cmds: &[Qail]) -> PgResult<usize>
Simple query protocol pipeline - uses ‘Q’ message.
Sourcepub async fn pipeline_simple_bytes_fast(
&mut self,
wire_bytes: &[u8],
expected_queries: usize,
) -> PgResult<usize>
pub async fn pipeline_simple_bytes_fast( &mut self, wire_bytes: &[u8], expected_queries: usize, ) -> PgResult<usize>
FASTEST simple query pipeline - takes pre-encoded bytes.
Sourcepub async fn pipeline_ast_cached(&mut self, cmds: &[Qail]) -> PgResult<usize>
pub async fn pipeline_ast_cached(&mut self, cmds: &[Qail]) -> PgResult<usize>
CACHED PREPARED STATEMENT pipeline - Parse once, Bind+Execute many.
- Generate SQL template with $1, $2, etc. placeholders
- Parse template ONCE (cached in PostgreSQL)
- Send Bind+Execute for each instance (params differ per query)
Sourcepub async fn pipeline_prepared_fast(
&mut self,
stmt: &PreparedStatement,
params_batch: &[Vec<Option<Vec<u8>>>],
) -> PgResult<usize>
pub async fn pipeline_prepared_fast( &mut self, stmt: &PreparedStatement, params_batch: &[Vec<Option<Vec<u8>>>], ) -> PgResult<usize>
ZERO-LOOKUP prepared statement pipeline.
- Hash computation per query
- HashMap lookup per query
- String allocation for stmt_name
§Example
// Prepare once (outside timing loop):
let stmt = PreparedStatement::from_sql("SELECT id, name FROM harbors LIMIT $1");
let params_batch: Vec<Vec<Option<Vec<u8>>>> = (1..=1000)
.map(|i| vec![Some(i.to_string().into_bytes())])
.collect();
// Execute many (no hash, no lookup!):
conn.pipeline_prepared_fast(&stmt, ¶ms_batch).await?;Sourcepub async fn prepare(&mut self, sql: &str) -> PgResult<PreparedStatement>
pub async fn prepare(&mut self, sql: &str) -> PgResult<PreparedStatement>
Prepare a statement and return a handle for fast execution. PreparedStatement handle for use with pipeline_prepared_fast.
Sourcepub async fn pipeline_prepared_results(
&mut self,
stmt: &PreparedStatement,
params_batch: &[Vec<Option<Vec<u8>>>],
) -> PgResult<Vec<Vec<Vec<Option<Vec<u8>>>>>>
pub async fn pipeline_prepared_results( &mut self, stmt: &PreparedStatement, params_batch: &[Vec<Option<Vec<u8>>>], ) -> PgResult<Vec<Vec<Vec<Option<Vec<u8>>>>>>
Execute a prepared statement pipeline and return all row data.
Source§impl PgConnection
impl PgConnection
Sourcepub async fn query_cached(
&mut self,
sql: &str,
params: &[Option<Vec<u8>>],
) -> PgResult<Vec<Vec<Option<Vec<u8>>>>>
pub async fn query_cached( &mut self, sql: &str, params: &[Option<Vec<u8>>], ) -> PgResult<Vec<Vec<Option<Vec<u8>>>>>
Execute a query with cached prepared statement.
Like query(), but reuses prepared statements across calls.
The statement name is derived from a hash of the SQL text.
OPTIMIZED: Pre-allocated buffer + ultra-fast encoders.
Sourcepub async fn query_prepared_single(
&mut self,
stmt: &PreparedStatement,
params: &[Option<Vec<u8>>],
) -> PgResult<Vec<Vec<Option<Vec<u8>>>>>
pub async fn query_prepared_single( &mut self, stmt: &PreparedStatement, params: &[Option<Vec<u8>>], ) -> PgResult<Vec<Vec<Option<Vec<u8>>>>>
ZERO-HASH sequential query using pre-computed PreparedStatement. This is the FASTEST sequential path because it skips:
- SQL generation from AST (done once outside loop)
- Hash computation for statement name (pre-computed in PreparedStatement)
- HashMap lookup for is_new check (statement already prepared)
§Example
let stmt = conn.prepare("SELECT * FROM users WHERE id = $1").await?;
for id in 1..10000 {
let rows = conn.query_prepared_single(&stmt, &[Some(id.to_string().into_bytes())]).await?;
}Source§impl PgConnection
impl PgConnection
Sourcepub async fn begin_transaction(&mut self) -> PgResult<()>
pub async fn begin_transaction(&mut self) -> PgResult<()>
Begin a new transaction.
After calling this, all queries run within the transaction
until commit() or rollback() is called.
Sourcepub async fn commit(&mut self) -> PgResult<()>
pub async fn commit(&mut self) -> PgResult<()>
Commit the current transaction.
Makes all changes since begin_transaction() permanent.
Sourcepub async fn rollback(&mut self) -> PgResult<()>
pub async fn rollback(&mut self) -> PgResult<()>
Rollback the current transaction.
Discards all changes since begin_transaction().
Sourcepub async fn savepoint(&mut self, name: &str) -> PgResult<()>
pub async fn savepoint(&mut self, name: &str) -> PgResult<()>
Create a named savepoint within the current transaction.
Savepoints allow partial rollback within a transaction.
Use rollback_to() to return to this savepoint.
Sourcepub async fn rollback_to(&mut self, name: &str) -> PgResult<()>
pub async fn rollback_to(&mut self, name: &str) -> PgResult<()>
Rollback to a previously created savepoint. Discards all changes since the named savepoint was created, but keeps the transaction open.
Sourcepub async fn release_savepoint(&mut self, name: &str) -> PgResult<()>
pub async fn release_savepoint(&mut self, name: &str) -> PgResult<()>
Release a savepoint (free resources, if no longer needed).