Skip to main content

Crate spg_engine

Crate spg_engine 

Source
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;

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, a Row, and the row’s column schema, produce a Value. v0.4 implements:
fts
v7.12.1 — full-text search lexer / stemmer.
json
v4.14 minimal JSON parser for the -> / ->> operators.
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.
selectivity
v6.2.2 — selectivity estimation over per-column statistics.
statistics
v6.2.0 — per-column statistics for the cost-based optimizer.
subscriptions
v6.1.4 — logical-replication subscription catalog.
triggers
v7.12.4 — PL/pgSQL row-level trigger executor.
users
User table + RBAC types for v4.1.

Structs§

ActivityRow
v6.5.2 — one row of spg_stat_activity. Engine-public so spg-server can construct rows without re-exporting internal dispatch types.
AuditRow
v6.5.3 — one row of spg_audit_chain. Engine-public so spg-server can construct rows directly from AuditEntry.
CancelToken
CatalogSnapshot
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 the execute_readonly path actually reads. Cheap to Clone (each clone shares the underlying PersistentVec row storage; only the trie root pointers copy). Send + Sync so a snapshot can be moved across tokio::task::spawn_blocking boundaries without coordination.
Engine
EngineSnapshot
CoW-1 (v7.34) — frozen view of the persisted committed engine state. Carries every field the snapshot() envelope serializes; Clone is 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.
MemoryStats
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.
TableMemoryStats
v7.31 (memory campaign — ceiling-first / never-die, design v1) — per-table slice of the engine’s resident-memory accounting. hot_encoded_bytes is the storage layer’s maintained meter (what the rows encode to); approx_resident_bytes is what they COST in RAM (per-cell enum slots + heap payloads via approx_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_cancelled at row-loop checkpoints and bails with EngineError::Cancelled. The host (spg-server) creates an AtomicBool per query, spawns a watchdog thread that sets it after SPG_QUERY_TIMEOUT_MS, and passes it via execute_with_cancel / execute_readonly_with_cancel.

Enums§

EngineError
All errors the engine can return.
ParsedStatement
QueryResult
Result of executing one statement.
StreamItem
v7.37 — one item in the streaming SELECT emit channel. The engine yields exactly one Header (before any row) then zero or more Rows. Pgwire (or any other consumer) decides how to turn those into wire bytes.

Constants§

COMPACTION_TARGET_DEFAULT_BYTES
v6.7.3 — default segment-size threshold used by COMPACT COLD SEGMENTS when no explicit target is supplied. Segments whose OwnedSegment::bytes().len() is strictly less than this value are eligible to merge. spg-server reads SPG_COMPACTION_TARGET_SEGMENT_BYTES to override.
IMPLICIT_TX
Reserved slot used by Engine::execute(sql) — the legacy single- global-shadow path. New alloc_tx_id handles start at 1.

Functions§

format_bigint_2d_text_pub
format_hstore_text
v7.17.0 Phase 3.P0-39 — pub re-export so pgwire + sqllogictest share the single hstore renderer.
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_range_text
v7.17.0 Phase 3.P0-38 — render a Range value as its canonical PG text form. Re-exported via format_range_text for use from spg-server’s pgwire layer.
format_text_2d_text_pub
substitute_placeholders
v7.16.0 — walks a parsed statement and replaces every Expr::Placeholder(N) with the corresponding params[N-1] re-encoded as an Expr::Literal. Used internally by Engine::execute_prepared AND 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§

ActivityProvider
v6.5.2 — provider callback type. Fresh snapshot returned each call; engine doesn’t cache the slice.
AuditChainProvider
v6.5.3 — chain-table provider + verifier. spg-server registers fn pointers that snapshot / verify the audit log. verify returns (verified_count, broken_at_seq)broken_at_seq is -1 on a clean chain.
AuditVerifier
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 is no_std, so it can’t reach for std::time itself — callers (spg-server, the sqllogictest runner) inject a concrete implementation. None means NOW() / CURRENT_* raise Unsupported.
MonotonicNowFn
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 into Instant::now() on the server). The engine never calls Instant::now() directly so the crate stays #![no_std].
SaltFn
Function pointer that produces 16 cryptographically random bytes. Like ClockFn, the engine is no_std and can’t reach for /dev/urandom itself — host (spg-server) injects an OS-backed source. None means SQL-driven CREATE USER falls 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).
SlowQueryLogger
v6.5.6 — callback signature for slow-query log emission. Called with (sql, elapsed_us) once per successful execute that crosses the threshold.