PostgreSQL v3 wire-protocol framing: message encoders, a message
reader over ::hot::tcp, and cursor-style readers for parsing
message payloads. Internal to the pg package.
Every backend/frontend message except the startup and SSL requests is framed as: 1 type byte, Int32 length (covering the length field and body but not the type byte), then the body.
Values
PROTOCOL-VERSION
PROTOCOL-VERSION: Int 196608
SSL-REQUEST-CODE
SSL-REQUEST-CODE: Int 80877103
Functions
cstring
fn (s: Str): Bytes
Encode a string as null-terminated bytes.
Example
::hot::hex/encode(::pg::wire/cstring("hi")) // "686900"
frame
fn (type-char: Str, body: Bytes): Bytes
Frame a body with a 1-char type tag and Int32 length prefix.
Example
::hot::hex/encode(::pg::wire/frame("Q", ::pg::wire/cstring("hi"))) // "5100000007686900"
int16
fn (n: Int): Bytes
Encode an Int as a 2-byte big-endian value.
Example
::hot::hex/encode(::pg::wire/int16(1)) // "0001"
int32
fn (n: Int): Bytes
Encode an Int as a 4-byte big-endian value.
Example
::hot::hex/encode(::pg::wire/int32(13)) // "0000000d"
password-message
fn (password: Str): Bytes
PasswordMessage ('p') carrying a cleartext or md5 password.
Example
::hot::hex/encode(::pg::wire/password-message("x")) // "70000000067800"
query-message
fn (sql: Str): Bytes
Simple Query message ('Q').
Example
::hot::hex/encode(::pg::wire/query-message("hi")) // "5100000007686900"
read-byte
fn (buf: Bytes, pos: Int): Vec
Read one byte as an Int at pos. -> [value, pos+1].
Example
::pg::wire/read-byte(Bytes([82, 9]), 1) // [9, 2]
read-cstring
fn (buf: Bytes, pos: Int): Vec
Read a null-terminated string starting at pos.
-> [string, position-after-terminator].
Example
::pg::wire/read-cstring(::pg::wire/cstring("user"), 0) // ["user", 5]
read-int32
fn (buf: Bytes, pos: Int): Vec
Read a signed 4-byte big-endian Int at pos. -> [value, pos+4].
Example
::pg::wire/read-int32(Bytes([0, 0, 0, 7, 1]), 0) // [7, 4]
read-message
fn (socket): Map
Read one framed backend message from socket. Returns
{type: Str, payload: Bytes} where payload excludes the type and
length fields. Blocks until a full message arrives.
Example
socket ::hot::tcp/connect("127.0.0.1", 5432)
::hot::tcp/write(socket, ::pg::wire/startup-message("app", "app"))
msg ::pg::wire/read-message(socket)
// Type "R" asks for authentication. Type "E" is an error.
msg.type
read-uint16
fn (buf: Bytes, pos: Int): Vec
Read an unsigned 2-byte big-endian Int at pos. -> [value, pos+2].
Example
::pg::wire/read-uint16(Bytes([255, 255]), 0) // [65535, 2]
ssl-request
fn (): Bytes
SSLRequest message (untyped): asks the server to start TLS.
Example
::hot::hex/encode(::pg::wire/ssl-request()) // "0000000804d2162f"
startup-message
fn (user: Str, database: Str): Bytes
StartupMessage (untyped) with user and database parameters.
Example
msg ::pg::wire/startup-message("app", "app")
length(msg) // 31
sync-message
fn (): Bytes
Sync message ('S') for the extended-query protocol.
Example
::hot::hex/encode(::pg::wire/sync-message()) // "5300000004"
terminate-message
fn (): Bytes
Terminate message ('X').
Example
::hot::hex/encode(::pg::wire/terminate-message()) // "5800000004"