Expand description
SPG execution engine — v0.3 wires the SQL front-end to the in-memory
storage layer. Implements CREATE TABLE, single-row INSERT VALUES, and
SELECT * FROM <table> (no WHERE yet — that lands in v0.4 alongside
expression evaluation against rows).
Re-exports§
pub use crate::users::Role;pub use crate::users::ScramSecrets;pub use crate::users::UserError;pub use crate::users::UserStore;pub use subquery::BATCHED_SCALAR_FALL_THROUGH_COUNT;pub use subquery::BATCHED_SCALAR_KEYED_FIRE_COUNT;pub use subquery::BATCHED_SCALAR_KEYED_PROBE_COUNT;pub use subquery::EXISTS_BATCH_FALL_THROUGH_COUNT;pub use subquery::EXISTS_BATCH_FIRE_COUNT;pub use subquery::EXISTS_PULLUP_BAIL_INNER_FROM;pub use subquery::EXISTS_PULLUP_BAIL_INNER_SHAPE;pub use subquery::EXISTS_PULLUP_BAIL_MULTICOL_DISABLED;pub use subquery::EXISTS_PULLUP_BAIL_NO_CORR;pub use subquery::EXISTS_PULLUP_BAIL_NO_WHERE;pub use subquery::EXISTS_PULLUP_BAIL_RESIDUAL_NOT_INNER;pub use subquery::EXISTS_PULLUP_BAIL_UNIQUE_KEY_MISSING;pub use subquery::EXISTS_PULLUP_CANDIDATE_COUNT;pub use subquery::EXISTS_PULLUP_FIRE_COUNT;pub use subquery::EXISTS_PULLUP_MULTICOL_DISABLE;pub use subquery::PULLUP_LIMIT1_FIRE_COUNT;pub use subquery::SCALARSQ_PK_PROBE_FIRED;pub use subquery::ScalarPkProbeFastPath;pub use subquery::expr_tree_has_subquery;pub use tempstore::SpillStats;pub use tempstore::TempRun;pub use tempstore::TempRunFactory;pub use tempstore::TempStoreError;
Modules§
- aggregate
- Aggregate executor.
- copy
- v7.22 (mailrs round-13 / T2) — shared COPY text-format helpers.
- describe
- v6.3.3 — Describe statement pre-Execute.
- eval
- Expression evaluator. Given a parsed
Expr, aRow, and the row’s column schema, produce aValue. v0.4 implements: - fts
- v7.12.1 — full-text search lexer / stemmer.
- json
- v4.14 minimal JSON parser for the
->/->>operators. - largeobject
- v7.39 (round 342+, V40) — the two large-object calls that touch a SERVER FILE.
- locks
- v7.37.15 (Phase C.4) — row-level lock table, the four PG tuple-lock modes, and wait-for deadlock detection.
- memoize
- v6.2.6 — Memoize cache for correlated subqueries.
- plan_
cache - v6.3.0 — Engine-level plan cache.
- publications
- v6.1.2 — logical-replication publication catalog.
- query_
stats - v6.5.1 — per-distinct-SQL LRU stat collector.
- reorder
- v6.2.3 — JOIN reorder planner pass.
- scalarsq_
streaming - selectivity
- v6.2.2 — selectivity estimation over per-column statistics.
- statistics
- v6.2.0 — per-column statistics for the cost-based optimizer.
- subquery
- Correlated-subquery evaluation split out of
lib.rs(lib.rs split 5): the per-roweval_expr_with_correlatedpath (clones the expression, substitutes outer-row columns into each surviving subquery node, runs the inner SELECT, folds the literal result back) plus thesubquery_replacementpre-walk that materialises uncorrelated subquery nodes once, and thetry_batch_correlated_scalarkeyed-probe optimisation (round-22 phase 3) that runs a correlated scalar subquery ONCE without the correlation and folds rows into a key→value map.impl Enginemethods; the bare-SELECT / DML / join row loops driveeval_expr_with_correlated, andselect.rsdrivessubquery_replacement/try_batch_correlated_scalar. - subscriptions
- v6.1.4 — logical-replication subscription catalog.
- tempstore
- v7.39 (round 786, T35 Phase A) — host-provided temporary storage for query-time spilling.
- testkit
- Test-mode GUC framework (v7.38 P0 元机制 D).
- triggers
- v7.12.4 — PL/pgSQL row-level trigger executor.
- users
- User table + RBAC types for v4.1.
Macros§
- bump_
counter - injection_
point - Hook a perf-irrelevant test-only injection point into a code path.
Structs§
- Activity
Row - v6.5.2 — one row of
spg_stat_activity. Engine-public so spg-server can construct rows without re-exporting internal dispatch types. - AllCommitted
- An oracle that reports every version as committed. Equivalent to
the pre-Phase-C.2 two-state world; lets a caller that does not yet
track aborts reuse
Snapshot::visible_with_statusand get behaviour identical toSnapshot::visible. - Audit
Row - v6.5.3 — one row of
spg_audit_chain. Engine-public so spg-server can construct rows directly fromAuditEntry. - Cancel
Token - Catalog
Snapshot - v7.11.0 — frozen read-only view of the engine’s committed state.
Constructed via
Engine::clone_snapshot. Holds clones of the catalog, statistics, clock function, and row-cap config — the four fields theexecute_readonlypath actually reads. Cheap toClone(each clone shares the underlyingPersistentVecrow storage; only the trie root pointers copy). Send + Sync so a snapshot can be moved acrosstokio::task::spawn_blockingboundaries without coordination. - Engine
- Engine
Snapshot Cloneis O(1) on the catalog (Arc bump) and cheap typed-clones on the trailers. Decouples “capture state” from “serialize bytes” so the background-checkpoint worker can hold the snapshot and produce bytes off the engine write lock.- InProgress
Set - Compact in-progress set. Stored as a sorted
Vec<u64>so thecontainscheck is a binary search — O(log n) and zero allocation per lookup. We expectnto be tens at most (the active transaction count); for that range bsearch beats a hashset by a wide margin on both wall-clock and cache. - Memory
Stats - v7.31 — whole-engine memory snapshot: the polling form of the
round-26 ask-4 watermark signal. Hosts compare
total_approx_resident_bytes(+ their own WAL/file accounting) against their deployment ceiling and shed/shrink before the kernel does it for them. - Notice
- v7.39 (round 318, V41) — one diagnostic the statement raised, in PG’s exact wording minus the severity banner (the wire layer adds that).
- Parallel
Runner Slot - Engine slot for the injected runner — a newtype so the
Enginederive(Debug) keeps working over the non-Debug trait object. - RowHeader
- Per-row MVCC visibility header.
- Select
Statement - Snapshot
- Per-statement / per-transaction snapshot.
- Table
Memory Stats - v7.31 (memory campaign — ceiling-first / never-die, design v1) —
per-table slice of the engine’s resident-memory accounting.
hot_encoded_bytesis the storage layer’s maintained meter (what the rows encode to);approx_resident_bytesis what they COST in RAM (per-cell enum slots + heap payloads viaapprox_row_bytes) — the gap between the two is the representation multiplier the round-26 report measured at ~11× end-to-end. - TxId
- v4.5 cooperative cancellation token. A long-running SELECT /
UPDATE / DELETE checks
is_cancelledat row-loop checkpoints and bails withEngineError::Cancelled. The host (spg-server) creates anAtomicBoolper query, spawns a watchdog thread that sets it afterSPG_QUERY_TIMEOUT_MS, and passes it viaexecute_with_cancel/execute_readonly_with_cancel.
Enums§
- Engine
Error - All errors the engine can return.
- Notice
Severity - v7.39 (round 318, V41) — how loud a diagnostic the statement raised is.
PG distinguishes them on the wire (
S/Vfields of NoticeResponse) and clients act on it: psql printsWARNING:in a different colour, and several drivers surface warnings to the application while dropping notices. Emitting everything as NOTICE loses that. - Parsed
Statement - Query
Result - Result of executing one statement.
- RowCells
- One row’s cells, in whichever shape the producer already holds them.
- RowChange
- In-memory table: schema + a persistent row vector + secondary indices.
- Session
Tz - v7.39 (tz epic) — per-statement snapshot of the session TimeZone, consumed per-VALUE by the timestamptz renderers (a DST zone’s offset depends on the instant being rendered).
- Stream
Item - v7.37 — one item in the streaming SELECT emit channel. The
engine yields exactly one
Header(before any row) then zero or moreRows. Pgwire (or any other consumer) decides how to turn those into wire bytes. - Xact
Status - v7.37.15 (Phase C.2) — terminal state of a transaction / row version, as seen by the visibility oracle.
Constants§
- COMPACTION_
TARGET_ DEFAULT_ BYTES - v6.7.3 — default segment-size threshold used by
COMPACT COLD SEGMENTSwhen no explicit target is supplied. Segments whoseOwnedSegment::bytes().len()is strictly less than this value are eligible to merge. spg-server readsSPG_COMPACTION_TARGET_SEGMENT_BYTESto override. - IMPLICIT_
TX - Reserved slot used by
Engine::execute(sql)— the legacy single- global-shadow path. Newalloc_tx_idhandles start at 1. - MUTATING_
CALL_ NEEDLES - v7.39 (round 498) — the function names that must reach the
&mutexecutor, as lowercase byte needles including the opening paren. - XMAX_
ALIVE - Sentinel value used for
xmaxwhen the row has NOT been deleted. PG usesInvalidTransactionId = 0; SPG matches. - XMIN_
FROZEN - Sentinel used for
xminon rows loaded from a pre-v7.37.15 envelope. Any non-zero value < every real transaction id works — we pick 1 (PG usesFrozenTransactionId = 2).
Statics§
- ANTI_
JOIN_ FAST_ PATH_ FIRED - ANTI_
JOIN_ FAST_ PATH_ TRIED - DISTINCT_
DUP_ DROPPED - MATVIEW_
DELTA_ APPLIED - MATVIEW_
DELTA_ BAILED - MATVIEW_
FANOUT_ BUFFERED - v7.39 — diagnostic counter: how many aggregate scans took the sharded path (read by benches to ground-truth activation). v7.39 (round 740) — matview delta ground-truth counters (the r735 lesson: a green content pin cannot distinguish “delta applied” from “silently fell back to full”; these can).
- PARALLEL_
AGG_ FIRED - PROJ_
DIRECT_ FIRE - PROJ_
ROW_ BUILT - SCAN_
PATH_ ENTERED - v7.39 (round 485) — how many projected rows the single-table scan builds, and how many of those the DISTINCT probe throws away again.
- UNIQ_
PROBE_ CALLS - v7.39 (round 166) — probe
idxfor a live row whose collated key equalskey(the fold of the row being written). Returns the row position of the first conflicting live row.foldrecomputes the collated key of a candidate row so collation / bpchar semantics stay byte-identical with the HashSet path; tombstoned rows are skipped the same way; Cold locators are skipped because the fold path only ever scanned hot rows. v7.39 (round 492) — how many locators the uniqueness probe walks, and how many probes there are. - UNIQ_
PROBE_ LOCATORS
Traits§
- Parallel
Runner - v7.39 (parallel-agg P0) — host-injected parallel executor. The
engine is
no_stdand cannot spawn threads; likeClockFn/RandomFn, the std-side host (spg-server / embedded-tokio) injects an implementation at startup.None(the default, and the only option in pure-no_stdembeddings) keeps every code path single-threaded and byte-identical to pre-P0 behaviour. - Xact
Status Oracle - v7.37.15 (Phase C.2) — the visibility oracle: maps a version id to
its terminal
XactStatus.
Functions§
- format_
bigint_ 2d_ text_ pub - format_
bit_ string - v7.37.5 ζ-A — render a BIT / BIT VARYING as a binary string of
'0'and'1'chars (PG canonical text form). Bytes are packed big-endian within each byte: the most-significant bit of byte 0 is bit 0 of the bit string. - format_
circle - v7.37.5 ε — render a Circle as PG canonical
<(x,y),r>. - format_
hstore_ text - v7.17.0 Phase 3.P0-39 — pub re-export so pgwire + sqllogictest share the single hstore renderer.
- format_
inet - format_
int_ 2d_ text_ pub - v7.17.0 Phase 3.P0-40 — pub re-exports so pgwire + sqllogictest share the single 2D-array renderer.
- format_
line - v7.37.5 ε — render a Line as PG canonical
{a,b,c}(Ax+By+C=0). - format_
lseg - v7.37.5 ε — render an Lseg as PG canonical
[(x1,y1),(x2,y2)]. - format_
macaddr - v7.37.5 ζ-A — render a MACADDR (6 bytes) as
aa:bb:cc:dd:ee:ff. - format_
macaddr8 - v7.37.5 ζ-A — render a MACADDR8 (8 bytes) as
aa:bb:cc:dd:ee:ff:00:11. - format_
multirange - v7.37.5 δ — render a Multirange in PG external form
{[a,b),[c,d)}. Empty multirange renders as{}. Each range element is formatted with the same[/(/]/)bracket grammar as scalarValue::Range. RangeSpan carries nokind(it lives on the parent Multirange), so this routes element formatting throughformat_range_elementas Value::Range does. - format_
path - v7.37.5 ε — render a Path as PG canonical
[(x,y),...]open or((x,y),...)closed. - format_
pg_ box - v7.37.5 ε — render a Box as PG canonical
(ux,uy),(lx,ly). PG normalises the corner order on input; we trust the engine’s constructor has already normalised so the field order here is the canonical upper-right + lower-left. - format_
pg_ lsn - Render an LSN in PG’s
%X/%Xform (uppercase hex, no zero-padding). - format_
point - v7.37.5 ε — render a Point as PG canonical
(x,y). - format_
polygon - v7.37.5 ε — render a Polygon as PG canonical
((x,y),...). - format_
range_ text - v7.17.0 Phase 3.P0-38 — render a Range value as its canonical
PG text form. Re-exported via
format_range_textfor use from spg-server’s pgwire layer. - format_
text_ 2d_ text_ pub - pg_
guc_ boot_ value - v7.39 (tz epic) — host-injected IANA timezone lookups (the no_std engine can’t read the system zoneinfo directory; spg-tzif is the std-side implementation). All instants are MICROSECONDS. UTC offset (µs east) of a zone at a UTC instant; None = unknown zone. v7.39 (round 534) — the compiled-in default PG18 reports for a configuration parameter, for the wire’s own SHOW shortcut.
- silent_
for_ update_ count - v7.37.14 (A2.5-stub) — read the process-wide silent-FOR-UPDATE counter. Engines / spgctl / monitoring use this to surface “how many advisory row locks did the workload ask for since process start”. Returns 0 if no FOR UPDATE / FOR SHARE clause has hit the parser yet.
- substitute_
placeholders - v7.16.0 — walks a parsed statement and replaces every
Expr::Placeholder(N)with the correspondingparams[N-1]re-encoded as anExpr::Literal. Used internally byEngine::execute_preparedAND surfaced for the spg-embedded WAL path (which needs the bind-final AST so replay sees a simple-query-shaped statement, not a$1-shaped one). Errors when a placeholder references an index past the params slice.
Type Aliases§
- Activity
Provider - v6.5.2 — provider callback type. Fresh snapshot returned each call; engine doesn’t cache the slice.
- Audit
Chain Provider - v6.5.3 — chain-table provider + verifier. spg-server registers
fn pointers that snapshot / verify the audit log.
verifyreturns(verified_count, broken_at_seq)—broken_at_seqis-1on a clean chain. - Audit
Verifier - Backend
Count Fn - v7.39 (pg_stat knife A) — host-provided live connection count for
pg_stat_database.numbackends. - Backend
PidFn - v7.39 (read01 pgstatfuncs.c) — host-provided identity of the CALLING
connection for
pg_backend_pid()/ the pg_stat_activity self-join. The host reads a connection-thread-local set at session start; the no_std engine just calls through.None(embedded) → pid 1. - Backend
Signal Fn - v7.39 (round 318, V51) — host-provided connection control.
terminatefalse = cancel the target’s running statement (PGpg_cancel_backend, MySQLKILL QUERY); true = also close the connection (PGpg_terminate_backend, MySQLKILL CONNECTION). Returns whether a connection with that id exists — the engine has no registry of its own, so the answer has to come from the host that accepted the sockets.None(embedded, no connections) ⇒ nothing to signal. - ClockFn
- The execution engine. Holds the catalog and (later) other server-scope
state.
Engine::new()is intentionally cheap so callers can construct one per database, per test. Function pointer that returns “now” as microseconds since Unix epoch. The engine isno_std, so it can’t reach forstd::timeitself — callers (spg-server, the sqllogictest runner) inject a concrete implementation.NonemeansNOW()/CURRENT_*raiseUnsupported. - Monotonic
NowFn - v7.17.0 Phase 2.3 — monotonic time source for deadline-aware
cancellation (PG
statement_timeout). Returns microseconds since some host-stable monotonic origin (typically the first call intoInstant::now()on the server). The engine never callsInstant::now()directly so the crate stays#![no_std]. - SaltFn
- Function pointer that produces 16 cryptographically random bytes.
Like
ClockFn, the engine isno_stdand can’t reach for /dev/urandom itself — host (spg-server) injects an OS-backed source.Nonemeans SQL-drivenCREATE USERfalls back to a deterministic salt derived from the username (acceptable in tests; the server always installs a real RNG so production paths never see this). - Slow
Query Logger - v6.5.6 — callback signature for slow-query log emission. Called
with
(sql, elapsed_us)once per successful execute that crosses the threshold. - TzAbbrev
Fn - Zone designation (“JST”, “EDT”) at a UTC instant.
- TzAllFn
- v7.39 (round 502) — every zone the host knows at a UTC instant, as
(name, abbrev, utc_offset_secs, is_dst). - TzCanon
Fn - Canonical zone spelling (“asia/tokyo” -> “Asia/Tokyo”).
- TzLocalize
Fn - Local wall-clock µs -> UTC µs with PG’s DST disambiguation.
- TzOffset
Fn - WalLsn
Fn - v7.39 (round 476) — the WAL’s current byte position, as a PG LSN.