giong-postgres
A PostgreSQL client and connection pool for the giong runtime. It speaks the wire protocol itself (no libpq), encrypts with giong-tls, and depends on nothing from the Tokio ecosystem.
[]
= "0.6"
= "0.6"
use ;
In a service, make one Pool per worker. Nothing is shared between threads, so nothing is locked:
use ;
What it does
- Pipelining across tasks. Tasks that share a connection send their statements together, and one round trip answers all of them. A few connections serve hundreds of concurrent tasks without each task waiting for a connection of its own.
- Prepared statements, kept. Every statement is prepared the first time a connection runs it and kept in a bounded cache (256 per connection by default), so from then on it takes one round trip. When the schema changes under a cached statement (
ALTER TABLE, a view replaced), the statement is prepared again and run once more, so the caller sees no error; inside a transaction the server has already aborted the transaction, so that one call fails with the server's error and the next one is prepared again. Stale statements are recognised by SQLSTATE and the server's routine name, so this works whatever language the server writes its messages in. - Binary results for the common types (
boolean,bytea, the integers,oid, the floats,timestamptz), and text for the rest, so any column can still be read as aString. - A pool per worker. Statements that stand alone share connections;
pool.get()takes one connection for a transaction, session settings or a slow statement. When it comes back, a transaction left open is rolled back and the session is reset (settings, temporary tables, advisory locks,LISTEN, cursors), so nothing one task did reaches the next. Tasks waiting inget()are served in the order they came, even while shared statements keep every connection busy. Transactions and session changes never run on the shared connections: the pool's own methods refuseBEGIN,SET,LISTEN,PREPARE, temporary tables and the like before sending them, and a batch that leaves a transaction open closes its connection rather than let the statements of other tasks run inside it. The pool steers new statements away from a connection whose oldest statement has been running for more than 5 ms, so a slow query does not hold up the fast ones behind it. Connections are closed after 10 minutes unused and replaced after 30 minutes, or as soon as their server turns out to be a standby after a failover. - TLS on by default for remote hosts, always verified. SCRAM-SHA-256, MD5, and clear-text passwords only over TLS or to this machine.
require_authlimits the methods the server may ask for. SCRAM's key derivation runs on a thread of its own, once per password and salt, so connecting does not stall the other tasks of the worker. - Errors that say what to do. Every error has an
ErrorKind; errors from the server carry the SQLSTATE code and every field the server sent (detail,hint,constraint...). - Finite limits everywhere. Connecting, running a statement, the size of a result, the statement cache and waiting for the pool all have a limit by default, so nothing waits forever and nothing grows without bound.
Connecting
use Duration;
use Config;
let config = from_url?;
let same_place = new
.host
.user
.password
.dbname
.statement_timeout;
A URL is postgres://user:password@host:port/dbname?param=value (postgresql:// too). The host can be an IPv6 address in brackets, or, as with libpq, empty with the socket directory in a host parameter: postgres:///shop?host=/var/run/postgresql. The parameters understood are sslmode, sslrootcert, host, port, user, password, dbname, application_name, connect_timeout (seconds) and require_auth. Any other parameter is an error, not silently ignored.
TLS. Unless Config::tls or sslmode says otherwise:
| Where the server is | What happens |
|---|---|
A Unix socket (a host starting with /) |
No TLS. |
A loopback address (localhost, 127.0.0.0/8, ::1) |
No TLS. |
| Anywhere else | TLS, with the certificate checked against the certificate authorities installed on the machine and against the host name. |
sslmode=disable turns TLS off. require, verify-ca and verify-full all check the certificate and the host name, which is stricter than libpq (libpq checks nothing for require). prefer and allow are refused, because they fall back to an unencrypted connection without saying so. sslrootcert=/path/ca.pem trusts only that authority; in code, use Tls::Verify(TlsConnector::with_root_pem(..)?). When TLS is requested and the server does not support it, connecting fails.
A clear-text password is never sent over an unencrypted connection to another machine: the connection fails with ErrorKind::Auth instead.
Authentication. Without TLS (a Unix socket, a loopback address, sslmode=disable), nothing proves the server is the real one, and an impostor could ask for MD5 instead of SCRAM or let the client in without asking. Config::require_auth(&[AuthMethod::ScramSha256]), or require_auth=scram-sha-256 in the URL (libpq's names, comma-separated, without libpq's ! negations), refuses any other method before anything is sent for the password.
Behind PgBouncer
In transaction mode, PgBouncer gives each transaction any server connection, so two things need setting up:
- Prepared statements need PgBouncer 1.21 or later with
max_prepared_statementsabove 0 (it then prepares them on whichever server connection runs them). With it, the statement cache works as usual. Without it, prepared statements cannot work through PgBouncer in transaction mode, whateverstatement_cache_capacityis. - The statement timeout cannot be sent when connecting: PgBouncer refuses the connection, and neither
track_extra_parametersnorignore_startup_parameterspasses it on to the server (checked with PgBouncer 1.22). CallConfig::statement_timeout(None)and set the limit on the role:ALTER ROLE app SET statement_timeout = '30s'. Connecting withoutNonefails with an error that says this.
Both are tested in CI against PgBouncer in transaction mode.
API
Client::connect(&config) |
Opens a connection: TCP or Unix socket, TLS, authentication, all within connect_timeout. |
client.query(sql, &[&a, &b]) |
All the rows, as Vec<Row>. Parameters are $1, $2... |
client.query_one(..), query_opt(..) |
Exactly one row (ErrorKind::RowCount otherwise); at most one. |
client.execute(..) |
The number of rows affected. |
client.batch_execute(sql) |
Several statements separated by ;, through the simple protocol, with no parameters and no results. For migrations and setup. |
client.transaction() |
Starts a transaction and borrows the client mutably until it ends. tx.commit() or tx.rollback(); dropping the transaction rolls it back. A COMMIT that the server turned into a rollback (because of an earlier error) is an error, not a success. |
client.cancel_token() |
A CancelToken whose cancel() asks the server, over a separate connection, to stop the statement running now. A token taken from a PooledClient stops working when the connection goes back to the pool, so it cannot cancel another task's statement. |
client.parameter(name) |
A setting the server reported, such as server_version or TimeZone. |
client.is_closed(), client.in_flight() |
Whether the connection is closed; how many requests are waiting for their answer. |
row.get::<T>(column) |
A column by position or name, converted to T (see Types). Borrowed types (&str, &[u8]) borrow from the row. |
row.is_null(column), row.columns(), row.len() |
Whether a value is NULL; the columns' names and types; how many there are. |
Pool::new(config, max_size) |
A pool for the current worker. Connections are opened when needed, up to max_size. Size it to the database server's cores, shared among the workers (see Performance). |
.wait_timeout(duration) |
How long get() and the statement methods wait when every connection is taken, 5 s by default. |
.idle_timeout(duration), .max_lifetime(duration) |
Close a connection unused for this long (10 min by default); replace one older than this (30 min, less up to 10% so that connections opened together are not replaced together). |
pool.query(..) and the other statement methods |
Run on the least busy connection, pipelined with other tasks' statements. They refuse transaction statements, session changes and COPY ... FROM STDIN before sending them (ErrorKind::Statement). |
pool.get() |
A PooledClient for this task alone, which derefs to Client. For transactions, session settings and statements known to be slow. Served in the order asked. When it is dropped, a transaction left open is rolled back and, if anything was sent, the session is reset; settings given to Config survive the reset. |
pool.stats() |
Connections open, taken, being opened; requests in flight; tasks waiting; and counters for connections opened, connect errors, wait timeouts and connections recycled. |
Config::require_auth(&[AuthMethod::..]) |
The only authentication methods the server may ask for. |
A Client belongs to the worker that opened it (it is not Send, like everything in giong). Its methods take &self, so tasks share one by cloning an Rc or by holding a reference, and their statements are pipelined.
Types
| Rust | PostgreSQL |
|---|---|
bool |
boolean |
i16, i32, i64 |
smallint, integer, bigint (any of them, checked for range); oid |
f32, f64 |
real, double precision |
&str, String |
text, varchar, char(n), name; as parameters, any type the server can parse from text (uuid, numeric, date, json...); as results, any column the rest of this table does not list |
&[u8], Vec<u8> |
bytea |
SystemTime |
timestamptz |
Option<T> |
NULL or a value |
Integers convert between the three integer types when the value fits and fail when it does not, with an error that names the parameter or column, so a conversion never loses anything silently. Implement ToSql and FromSql for your own types.
Errors
Error is cheap to clone, implements std::error::Error, and converts into io::Error. error.kind() says what went wrong. Each kind has a giong error code, shown in front of the message and linked to its page in docs/errors (GIONG-E3003: password authentication failed for user "app". See …); after ? into an io::Error, giong::error::code(&err) finds it:
ErrorKind |
Code | Meaning |
|---|---|---|
Config |
E3001 | The configuration or URL is not valid. |
Connect |
E3002 | Name resolution, TCP, TLS, or the connect timeout. Also an unknown database (3D000). |
Auth |
E3003 | The server refused the credentials, or asked for a method this crate does not support. |
Db |
E3004 | The server reported an error for the statement; error.code() is the SQLSTATE (23505 for a unique violation...) and error.db() has every field. A COMMIT that the server turned into a rollback is Db with code 25P02 and no db(). |
Type |
E3005 | A value could not be converted between Rust and PostgreSQL. The message names the parameter or column and suggests the Rust type to use. |
RowCount |
E3006 | query_one found no row, or more than one. |
TooLarge |
E3007 | The result was larger than max_result_size. The connection stays usable. |
Closed |
E3008 | The connection is closed. A Pool replaces it by itself. Also a cancel token whose pooled connection went back to the pool. |
PoolTimeout |
E3009 | No pooled connection became free within wait_timeout. |
Protocol |
E3010 | The server sent something this crate cannot understand. The connection is closed. |
Statement |
E3011 | Refused before sending, on the connections a Pool shares: a transaction statement, a statement that changes the session (SET, LISTEN, PREPARE, temporary tables...), or COPY ... FROM STDIN. Use pool.get(). |
An error from the server affects only its own statement: the statements that other tasks pipelined on the same connection get their own results. Two things close the connection instead, and every statement sent on it after them fails with ErrorKind::Closed: COPY ... FROM STDIN on a Client (COPY is not supported yet; the pool refuses it), and a batch_execute that leaves a transaction open on a pool's shared connection.
Limits
| Setting | Default | What happens at the limit |
|---|---|---|
Config::connect_timeout |
10 s | ErrorKind::Connect, "connect timeout". |
Config::statement_timeout |
30 s | Sent as the session's statement_timeout, so the server cancels exactly the statement that ran too long (SQLSTATE 57014). From 1 ms to i32::MAX ms (24.8 days); outside that, connecting fails with ErrorKind::Config. None sends nothing and leaves the limit to the server or the role. |
Config::max_result_size |
64 MiB of row data | ErrorKind::TooLarge; the rest of the result is read and thrown away. A notice larger than this is skipped; an error message larger than this fails its statement with TooLarge. Either way the connection stays usable. |
Config::statement_cache_capacity |
256 per connection | The least recently used statement is closed. 0 prepares and closes every statement each time. |
Pool::wait_timeout |
5 s | ErrorKind::PoolTimeout. It bounds the wait for a connection; a task that opens a new connection itself waits up to connect_timeout instead. |
Pool::idle_timeout |
10 min | The connection is closed. |
Pool::max_lifetime |
30 min, less up to 10% | The connection is closed once no statement uses it, and a new one is opened when needed. |
| Statements on one pooled connection at a time | 1024 | The statement methods use another connection, or wait for room. A Client used directly has no such bound: its callers decide how many tasks share it. |
Performance
Measured with bench/postgres.sh against tokio-postgres 0.7.18 with deadpool-postgres 0.14.2 (Tokio 1.54, current-thread runtime). Each client runs on one core; the same PostgreSQL 16 server runs on the other three cores of a 4-vCPU virtual machine (Xeon 2.1 GHz, Linux 6.18), reached over TCP on 127.0.0.1. 64 tasks issue statements back to back: point reads one row by primary key, list reads 100 rows, and every column of every row is decoded (i64, two &str, SystemTime). The figures are medians of 5 rounds of 5 s (3 rounds for the other pool sizes), with the range in brackets.
A pool, on one client core:
| Client | Connections | point statements/s |
p99 | list statements/s |
p99 |
|---|---|---|---|---|---|
| giong-postgres | 3 | 240.7k (241–249k) | 0.52 ms | 54.8k (54–57k) | 2.0 ms |
| giong-postgres | 4 | 243.9k (211–265k) | 4.2 ms | 53.9k (46–63k) | 5.3 ms |
| tokio-postgres + deadpool | 4 | 53.1k (51–55k) | 1.9 ms | 21.7k (21–25k) | 4.4 ms |
| tokio-postgres + deadpool | 64, its best | 93.8k (91–97k) | 1.5 ms | 28.6k (27–29k) | 4.1 ms |
- Against deadpool at its best: 2.6× the point lookups and 1.9× the 100-row reads, at a third to a half of the p99. deadpool's client core is the limit (98–99% busy): every statement is a pool checkout, a round trip and a wake-up. giong-postgres sends the statements of all 64 tasks in a few writes and reads their answers in a few reads.
- Size the pool to the database's cores. With more connections than the server has cores (4 on 3 here), one backend at a time waits for a core, and the statements pipelined on its connection, about 16 of them, wait with it: the same throughput, but a p99 of 4 ms. 8 connections give 208.6k with the same p99; 2 connections leave a server core idle, 172.9k. With several workers, the connections of all their pools add up. Statements that wait on disk or on locks need more connections than this.
- One connection shared by every task (both libraries pipeline): the single backend process is the limit, one server core at 100%, and the two are within the spread of the rounds of each other. giong-postgres's medians are 8–11% lower (point 47.6k against 53.6k, list 15.7k against 17.0k) and it uses less of its core (46% against 53%, 42% against 61%).
Not measured: a server on another machine (network latency, where pipelining should matter more), writes, more than one client core, PostgreSQL versions other than 16. Results are in bench/results/postgres/; pool sizes other than 4 in pool-size/.
Not there yet
COPY, LISTEN/NOTIFY, streaming a result one row at a time (a result is collected in full, up to max_result_size), arrays, numeric, uuid, date and the other types not listed above in binary form (read them as strings), channel binding (SCRAM-SHA-256-PLUS), client certificates in the URL (sslcert and sslkey are refused; in code, pass a rustls::ClientConfig with the certificate to TlsConnector::new), several hosts with failover, running through PgBouncer without max_prepared_statements, and SASLprep normalization of non-ASCII passwords. The crate has had one independent review before 1.0, whose findings are fixed, but no security audit.
Tests
cargo test -p giong-postgres: 57 tests against a real PostgreSQL server that the test binary starts for itself (a throwaway cluster from initdb, with users for SCRAM, MD5, clear text and TLS only, listening on a Unix socket, 127.0.0.1 and 127.0.0.2): every supported type in both directions; conversion errors; server errors and their fields; pipelining across tasks, and a failing statement in the middle of a pipeline; the statement cache, its bound, statement names an application took, and schema changes under cached statements, in and out of a transaction; notices and errors larger than the result limit; the result-size limit; the statement timeout, and leaving it to the server; cancel requests; a query dropped mid-flight; a connection the server terminates; transactions committed, rolled back and dropped, and a COMMIT turned into a rollback; no task or connection left behind after a client is dropped; TLS against the right and the wrong authority and host; each authentication method with the right and the wrong password, and require_auth; SCRAM keys derived once and off the worker; a server that answers the TLS request with extra bytes; a clear-text password never sent unencrypted; the connect timeout; and the pool (spreading statements, exclusive use and the rollback on return, even with BEGIN still in flight, the session reset on return and no reset for an unused connection, get() served in order while shared statements keep every connection busy, waiting, and waking when a dropped statement frees the connection, cancel tokens that stop working on return, idle and old connections closed, at most 1024 statements per connection at a time, replacing broken connections, connect errors, a slow statement not holding up the others, transaction and session statements refused on shared connections and a batch that leaves a transaction open). Two of them run a workload through PgBouncer in transaction mode, with the statement timeout left to the role, when PgBouncer is installed. 40 unit tests: the message encoder and parser, SCRAM against the RFC 7677 example, MD5 against RFC 1321, URLs, type conversions, the statements the pool refuses, 42,000 random inputs for the message parser, the URL parser and SCRAM, and 300 sessions against a fake server answering with mutated replies (also the session fuzz target). 8 documentation examples.
The tests find PostgreSQL through GIONG_PG_BIN, /usr/lib/postgresql/*/bin or PATH, and skip when it is not installed, unless GIONG_PG_REQUIRED=1 (CI sets it); PgBouncer likewise through GIONG_PGBOUNCER_BIN, PATH or /usr/sbin, and GIONG_PGBOUNCER_REQUIRED=1. Run as root, they start the server as the postgres user. They also pass under ThreadSanitizer and on the fallback io_uring setups (scripts/check.sh tsan and fallback).