PostgreSQL wire protocol
A skaidb node speaks the PostgreSQL frontend/backend protocol (version 3) on
its own port, so psql and the PostgreSQL drivers of every language connect
to it as they would to a PostgreSQL server:
psql "host=db.example.com port=5432 user=ann dbname=shop sslmode=require"
It is the protocol, not the PostgreSQL dialect or catalog. The statements
a client sends are skaidb's SQL (QUERY_SYNTAX.md) and run
through the same path as every other surface — the same users and grants,
scan budgets, running-statement registry (SHOW QUERIES, KILL QUERY),
audit and query logs. What the listener adds is everything between the
socket and the statement: startup and TLS, login, the simple and the
extended query protocols, prepared statements and portals, COPY, cursors,
cancel requests, type OIDs with their text and binary encodings, SQLSTATE
codes, and the session commands drivers and pools send on their own.
What is not PostgreSQL lists where a tool written for PostgreSQL will notice the difference.
Enabling
[postgres]
enabled = true
port = 5432
max_connections = 1024 # this listener's own ceiling
max_message_bytes = 67108864 # largest single message from a client (64 MiB)
enabled and port take effect at startup; the two limits are
runtime-mutable (SET CONFIG postgres.max_connections = …). CLI and
environment: --postgres-enabled / SKAIDB_POSTGRES_ENABLED,
--postgres-port / SKAIDB_POSTGRES_PORT. The listener binds
server.bind_addr, like the other client ports. Off by default.
On a cluster, enable it on every member a client may connect to; each member serves the whole cluster's data.
Login and TLS
Users and roles are the node's own (CREATE USER … PASSWORD …, GRANT); an
account works on this port exactly as on the binary port.
- Password login is SCRAM-SHA-256, PostgreSQL's own default method,
checked against the verifier the node already stores — the password never
crosses the connection, and the client verifies the server too. Inside TLS
the server also offers SCRAM-SHA-256-PLUS: the proof is bound to the
server's certificate (
tls-server-end-point), so it cannot be relayed through another server (channel_binding=requirein libpq). Cleartext and MD5 password methods are never requested. - A client certificate is the credential when
auth.x509_enabledis on: a certificate that chains toauth.x509_ca_fileand whose Common Name is the user named at startup logs in without a password. - With authentication off (no
auth.superuser_password) every client acts as the superuser, as on every other listener.
TLS follows [encryption].client_tls:
client_tls |
The listener |
|---|---|
off |
answers SSLRequest with N; the session is in the clear |
opportunistic |
upgrades a client that asks (sslmode=prefer and up); a client may stay in the clear |
required |
refuses a session that is not in TLS, with an error the client shows |
Both ways of starting TLS are served: the SSLRequest upgrade and TLS from
the first byte (sslnegotiation=direct, ALPN postgresql). The certificate
is the [encryption] pair and rotates live. A failed login is charged to the
source address under auth.failed_login_rate_limit, shared with the other
listeners. GSSENCRequest is answered N (no GSSAPI encryption; Kerberos
login is on the binary port — KERBEROS.md).
The database
The database of the connection string is entered like USE. Two names are
not an error when no such database exists, because clients fall back to them
on their own — the user's name and postgres: the session then starts in
default. Any other missing database refuses the connection (3D000).
Clients
These connect and run their ordinary workloads — statements with typed and
untyped parameters, prepared statements, transactions, batches, large
results, COPY where the client has it:
| Client | Checked with |
|---|---|
psql / libpq |
17 (incl. \copy, channel_binding=require, sslnegotiation=direct) |
| psycopg (Python) | 3.3 (text and binary results, prepared statements, server-side cursors, COPY) |
| psycopg2 (Python) | 2.9 |
| asyncpg (Python) | 0.32 (prepared statements, cursors, pools) |
| pg8000 (Python) | 1.31 |
node-postgres (pg) |
8.23 (incl. pg-cursor, pools) |
| pgx (Go) | 5.13 (incl. batches, pools, context cancellation) |
| PostgreSQL JDBC | 42.7 (batches, binary transfer after prepareThreshold, setFetchSize) |
A connection is listed in the drivers table (endpoint postgres) under its
application_name.
The SQL it carries
Statements are skaidb SQL. Two PostgreSQL spellings are part of that SQL on every surface, because drivers and ORMs emit them:
$1,$2, … — numbered bind parameters. A number may repeat and may appear in any order; a statement uses$nor?, not both.<expr>::<type>— the cast, the same asCAST(<expr> AS <type>). Types: the integer types (int2/int4/int8/smallint/integer/bigint),float4/float8/real/double precision,numeric/decimal,text/varchar/char/character varying,bool/boolean,timestamp/timestamptz/date,uuid,json/jsonb. A length or precision (varchar(20),numeric(10, 2)) is read and says nothing: a value has no declared width. As with every coercion, a value that does not convert isNULL.
A function may be qualified with pg_catalog. (pg_catalog.lower(x)).
What a client asks about its session
Answered on the session, with the column name the call has in PostgreSQL:
version(), current_database(), current_catalog, current_schema()
(public), current_user, session_user, current_role,
pg_backend_pid(), pg_is_in_recovery(), current_setting('<name>').
version() reads PostgreSQL 16.0 (skaidb <version>) and server_version
is 16.0: drivers choose protocol features by it.
Session commands
Drivers and pools send these unasked; they are answered by the listener and never reach the SQL parser:
| Command | What it does here |
|---|---|
SET [SESSION\|LOCAL] <name> {=\|TO} <value>, SET TIME ZONE, SET NAMES, RESET <name>\|ALL, SHOW <name>\|ALL, SHOW TRANSACTION ISOLATION LEVEL |
Session parameters (below). skaidb's own SET CONSISTENCY, SET SCAN BUDGET, SET SEARCH WAIT, SET CONFIG and its SHOW … statements keep their meaning. |
BEGIN [WORK\|TRANSACTION] [modes], START TRANSACTION, COMMIT, END, ROLLBACK, ABORT |
Transaction control (below). |
SET TRANSACTION …, SET SESSION CHARACTERISTICS … |
Accepted; a mode that is not provided is a warning. |
DISCARD ALL |
Closes every prepared statement and cursor and resets the session's parameters, consistency level, scan budget and search wait. DISCARD PLANS\|SEQUENCES\|TEMP do nothing. |
DEALLOCATE <name>\|ALL, CLOSE <name>\|ALL |
Close prepared statements / cursors and portals. |
DECLARE … CURSOR, FETCH, MOVE |
Cursors (below). |
COPY |
Below. |
UNLISTEN |
Accepted (there is nothing to stop listening to). |
Types
A table declares no column types, so a result column's type is found from what it holds:
| The column holds | Type | OID |
|---|---|---|
| booleans | bool |
16 |
| integers | int8 |
20 |
| floats, or floats beside integers | float8 |
701 |
| decimals, alone or beside other numbers; floats beside an integer past 2^53, which a float would round | numeric |
1700 |
| text | text |
25 |
| bytes | bytea |
17 |
| UUIDs | uuid |
2950 |
| timestamps | timestamptz |
1184 |
| arrays whose elements are all one of the above (not bytes) | that type's array (int8[], text[], float8[], …) |
|
| documents, arrays of mixed or nested elements, or arrays beside scalars | jsonb |
3802 |
| several kinds that are not all numbers | text — each value as its own text |
25 |
| nothing (no rows, or only NULLs) | text |
25 |
Every type is sent in the text or the binary format, per column, as the
client asks. Timestamps are written in UTC (2026-11-17 15:18:35.535+00);
a float is written in the shortest form that reads back as the same value.
In a numeric column a float is the number of those digits, however large
or small, in both formats; NaN and the infinities are numeric's own. A float
without a fraction in a column described as bigint (see Describing a
prepared statement) is sent as the
integer it stands for.
Parameters are read in both formats for bool, int2/int4/int8/oid,
float4/float8, numeric, text/varchar/bpchar/name, bytea,
uuid, timestamptz/timestamp/date, json/jsonb, and one- or
multi-dimensional arrays of those. A parameter of any other type is taken as
its text in the text format and refused in the binary format. A timestamp
keeps milliseconds; a timestamp without a zone is read as UTC. A date its
month does not have (2026-02-31) is refused, and so is a bare run of digits
(for Unix milliseconds send an integer: to_timestamp($1::bigint)).
What a parameter's type is
In PostgreSQL the column a parameter meets decides its type. Here it is decided, in this order, by:
- The type the client fixed when it prepared the statement (JDBC
setLong, psycopg's integers, pgx after a describe, …). - A cast on the parameter in the statement:
$1::bigint,$2::timestamptz,$3::jsonb. - The column it meets — compared with (
id = $1,IN,BETWEEN), assigned to (SET n = $1), inserted into, or — a column of numbers — in arithmetic with (SET n = n + $1) — when that column has been seen to hold one kind of value (numbers of several kinds are one kind; a column of arrays takes atext[]). The table is sampled (`DESCRIBE
FULL`; inside a transaction block its own rows of the table are in the sample). In a join the column is found by its alias or table name (`c.id = $1`), or unqualified when one of the joined tables alone has it. `LIMIT $1` and `OFFSET $1` are integers, and a parameter in arithmetic with a number (`$1 + 100`, `$1 * 1.5`) is a number of that kind. 4. Otherwise **`text`**: the parameter is the text it was sent as. Rule 3 does not reach a column that holds several kinds that are not all numbers, a path into a document (`doc.k = $1`), a column of a CTE or of a set operation's branch, or an unqualified column two joined tables share. There an untyped parameter is text, **and text equals only text**: `WHERE doc.k = $1` with an untyped `42` matches no row whose `k` is the number 42. This is true of reads as much as of writes. Rule 4 is where a schemaless store differs from PostgreSQL, and it matters for clients that send every argument as untyped text — node-postgres, Go's `lib/pq`, Ruby's `pg`, PHP — and for the JDBC driver's timestamps, which it also sends untyped. Inserting the number `42` through such a client into a column that holds nothing yet stores the text `'42'`. The ways to say what is meant: - **Cast in the statement**: `INSERT INTO t (id, at) VALUES ($1::bigint, $2::timestamptz)`. - **Let the data say it**: once the column holds integers, an untyped `$1` that meets it is an integer (and a value that is not one is refused, `22P02`). - **Arrays**: a list sent untyped (psycopg sends a Python list of `str` so) into a column that holds nothing yet is the text `'{a,b}'`; wrap it (`Jsonb([...])`) or cast it (`$1::jsonb`). Once the column holds arrays, an untyped list is an array. - **`SET skaidb.untyped_parameters = infer`** (or in the connection string: `options=-c skaidb.untyped_parameters=infer`): a parameter nothing speaks for is read as what it plainly writes — an integer or a decimal number written the one way it can be, `true`/`false`, a JSON object or array, a timestamp written in full with its zone — and is text otherwise. A number that would not read back as written (`007`, `+1`, `1e3`) stays text. The default, `text`, never changes what was sent; `infer` is for the drivers above, at the price that the text `'42'` and the number `42` cannot be told apart when nothing else says which is meant. ### Describing a prepared statement Some clients ask for a statement's parameter types and result columns before it runs (asyncpg and pgx always; JDBC for a server-prepared statement) and then decode every later row by that answer. The parameters are described by the rules above. The columns are worked out from the statement — a literal, a cast, `count(*)`, `min`/`max` of a column, a column the table has been seen to hold one kind of — and are `text` where it does not say. `SELECT *` is described as the fields the table has been seen to hold: the columns `DESCRIBE
FULL` lists, each a top-level field a row can hold. An index over a path into a field (`doc.k`) or over an expression (`lower(name)`) adds no column named after its path or expression: the fields it reads (`doc`, `name`) are the columns ([`DESCRIBE`](query_syntax.html#describe) says how an index's component is read). What a column has been seen to hold is, for a result column (a parameter goes by the sample alone): - the table's sample (`DESCRIBE
FULL`, its first 1,000 rows in key order); - where the sample did not reach the table's end, the types live keys hold where the column leads a key — the first column of the table's key (of a clustered table, its first clustering column) or of a plain index (not on a table with a TTL) — which the sample cannot see: keys sort by type, so string ids beside integer ids all lie past a sample of integers, and the column is described as `text` from the start (the `keyed` column of `DESCRIBE … FULL`); - what the session's own results showed the column to hold (below). These only ever widen a column the sample typed. Floats beside integers are `float8` until the session meets an integer past 2^53 among them, which a float would round — then `numeric`. The execution that meets it is refused once, as below, even when the sample saw the row. An expression the statement does not type — `sum(n)`, `a + b`, a function call, a path into a document, any column of a join — is `text`: a client that describes first reads `sum(n)` as `'60'` unless the statement says `sum(n)::bigint`. A statement that **writes** (`INSERT`/`UPDATE`/`DELETE … RETURNING`) is described only by what is certain — a cast or a literal; every other returned column is `text`, which any written value fits — and `RETURNING *` cannot be described ahead at all (`0A000`, before anything runs: name the columns). A `CALL` cannot be described either — what a procedure returns is known only by calling it — and is refused the same way; run it without describing it (the simple protocol; psycopg and node-postgres as they are; asyncpg `execute()` without arguments; pgx in its simple-protocol mode). Not described, a `CALL` that returns several result sets answers a portal with its last. That is what makes this hold: **an error from a described statement that writes means nothing was written**. A described query then keeps the shape it promised. Its rows are checked against the description before the first of them is sent (of a streamed result, its first rows: see [Large results](#large-results)), each described column against its own column of the result, whatever the columns are named: two of one name (`SELECT id AS x, v AS x`, `SELECT v AS id, id`, a column named like a field of a `*` beside it) are two columns, each checked and sent its own values. A `*` that itself holds two columns of one name — a view's or a `WITH` query's whose select list repeats a field's name — is read in its description's places only: once the result no longer has the columns its description listed there (the field gone from every row, say), nothing tells which is which, and the statement fails before any row with the `0A000` below; prepared again, it is run and described again. - A value its column was not described as — text in a column described as `bigint`, a float with a fraction there, an integer past 2^53 in a `float8` column — fails the statement before any row is sent, with `0A000` `cached plan must not change result type` and the routine field `RevalidateCachedQuery` (PostgreSQL's own signal that a cached statement must be prepared again); the hint names the column. The session remembers what the column's rows held, so the statement prepared again — and every statement of the session over that column — is described to hold it. pgx prepares a cached statement again on its next call after such an error, and that call answers. A value is never sent as something the client would misread. - A session remembers what the rows held for up to 1,024 fields of a table. A value of another kind in a field past those fails with `42804` and no routine instead, naming the cast that settles it: preparing again would describe the column the same. Once the table's sample holds such a value (a row the sample reads holds one), the refusal carries the routine again, and the statement prepared again answers. - Inside a transaction block the error fails the block, as any error does. What the session remembers survives the rollback: the block run again, its statement prepared again, fits. - A column whose type the statement itself fixes (a cast, `count(*)`), and a value its type holds but the format cannot carry (a decimal past `numeric`'s binary scale), fail with `42804` instead, naming the column and the cast that settles it (`n::text`): preparing again would change nothing. - A column that the description did not list and that carries data — a new field under a prepared `SELECT *`, or one the table's sample missed — fails with the same `0A000` and routine. A streamed `SELECT *` finds its columns, every field a row it returns holds, before its first row is sent, and fails before any row on one the description did not list. The session remembers the field, and with it what the rows hold that their description does not (checked as above), so preparing the statement again describes both and answers. A session remembers up to 1,024 fields of a table past its sample. A field past those that the table's sample does not hold either fails with `0A000` in words of its own, `a result column is past the 1024 fields of a table a session describes; name its columns`, without the routine — prepared again, the statement would not list it — and its hint quotes the field; name the columns instead of `*`. Once the sample holds it, the refusal is the one above, with the routine. A statement whose columns only running it tells (a view's `*`, `NEAREST`, `RANK BY`) is run again when prepared again, and lists any field: its refusal always carries the routine. - A statement without parameters whose columns only its result shows (a join's `*`, `SHOW …`, `DESCRIBE`, `EXPLAIN`) is run once to describe it; when a later run's rows no longer fit, the same `0A000`, and prepared again it is run and described again. With parameters it cannot be, and says so: name the columns. A statement that is not described ahead (psycopg, node-postgres, JDBC before `prepareThreshold`, every use of the simple protocol) has no such promise to keep: its columns are typed from the rows it returns. ## Transactions `ReadyForQuery` reports the transaction status every client tracks (`I` idle, `T` in a block, `E` in a failed block). - **A standalone node** runs `BEGIN … COMMIT` / `ROLLBACK` as its own transactions. An error inside a block fails the block, as in PostgreSQL: every statement is refused (`25P02`) until the block ends, and ending it with `COMMIT` rolls it back and answers `ROLLBACK`. A transaction opened here holds the rows it writes until it ends, and another writer of one of them waits, as a PostgreSQL client expects at `READ COMMITTED`; a deadlock is refused with `40P01` (see [Row locks](query_syntax.html#row-locks)). Transactions opened over the binary protocol take no locks and are refused at `COMMIT` on a conflict. `COMMIT` answers when the transaction is durable — one disk flush, shared by the `COMMIT`s that arrive together — as PostgreSQL's does with `synchronous_commit = on` (see [`BEGIN`/`COMMIT`/`ROLLBACK`](query_syntax.html#begin-commit-rollback)). - **A cluster** has no multi-statement transactions; `BEGIN` is refused (`0A000`) and the session stays outside a block — nothing is silently autocommitted inside one. Connect in autocommit mode (psycopg `autocommit=True`, JDBC `setAutoCommit(true)`, SQLAlchemy `isolation_level="AUTOCOMMIT"`). `BEGIN ATOMIC PARTITION` works as on every surface. Isolation: `BEGIN ISOLATION LEVEL SERIALIZABLE` / `REPEATABLE READ` and `READ ONLY` are accepted with a **warning** saying the mode is not provided — the transaction is read-write, at the node's own isolation. `SHOW transaction_isolation` reports `read committed`. Several statements in one simple-protocol message run in order and stop at the first error; outside a block each commits on its own (PostgreSQL would undo them together). **DDL is not transactional.** `CREATE`, `DROP` and `ALTER` take effect at once, inside a block too, and `ROLLBACK` does not undo them; the statement answers with a warning saying so. A migration tool that relies on rolling a failed migration back will leave its DDL behind. A DDL that checks, rewrites, moves or removes the rows of a table the block has written — `ADD CHECK`, `SET NOT NULL`, `ADD FOREIGN KEY`, `ADD GENERATED … STORED`, `RENAME COLUMN`, `DROP TABLE` and the rest listed under [DDL inside a transaction](query_syntax.html#ddl-inside-a-transaction) — is refused with `25001`, and like any error it fails the block: its writes are rolled back. A migration that fills a table and then constrains or rewrites it commits the fill first (an example is under [Errors](#errors)). A cursor declared `WITH HOLD` inside a block keeps the rows it read even if the block is rolled back. There are no savepoints (`SAVEPOINT`, `ROLLBACK TO` — `0A000`) and no two-phase commit. An `EXCEPTION` block of a procedure undoes only the statement that failed; the block's earlier statements keep what they wrote, where PL/pgSQL would undo them too (see [Exception handlers](procedures.html#exception-handlers)). ## Large results A result is built in memory and bounded by the node's scan budgets. A plain `SELECT` of bare columns (or `*`) from one table — filtered by any row expression, unordered or in storage-key order (a time-series table's in `ts` order), without `LIMIT` — that goes over a budget is **streamed** instead: it runs as bounded pages, to which the row budget does not apply, and the node holds a few pages of the result, never the whole, however slowly the client reads. This is automatic, in both query protocols, and outside a transaction block (inside one a statement must see the transaction's own writes). A streamed result's column types are found from its first 8,192 rows (or 8 MiB of rows, whichever comes first). A described statement's stream reads the same rows ahead and checks them against its description before the first row is sent (see [Describing a prepared statement](#describing-a-prepared-statement)); they are held meanwhile, and count toward the session's held results below. A cancel request that comes while they are read, or a page of them that fails, is answered as itself before any row (`57014`, or the page's error), never as a refusal of the rows read before it. A later row that does not fit fails the statement with `42804` and the cast that settles it, after the rows before it were sent. Three ways to take a result in pieces. Over a streamed result each of them pulls the stream as it sends — `FETCH ALL` and `MOVE ALL` included — and never builds it: - **A row limit on `Execute`** — JDBC `setFetchSize`, psycopg `cursor.stream(size=…)`, `pg-cursor`: the portal is suspended between batches. A portal lives until its transaction ends; outside a block that is the next `Sync`. - **A cursor**: `DECLARE c CURSOR [WITH HOLD] FOR