Sealed rows
sealed, §M14.30 K4a
Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.
A table stored sealed (CREATE TABLE … WITH (storage = sealed),
or ALTER TABLE t SET STORAGE sealed) keeps each row as row does, its
stored value sealed under the key of the row’s subjects (SET SUBJECT,
§8.29) — the keys of §8.30, kept outside the data and its backups. So
FORGET SUBJECT does more than delete the subject’s rows: destroying
its key leaves every copy of them unreadable at once — the versions
history keeps (AS OF), a branch made before, the log, pages not yet
scrubbed, backups. A tenant is a subject like a person (SET SUBJECT (tenant)): its departure is a key destroyed.
The format. The whole row’s encoding (every value inline, none out
of line) sealed by AES-256-GCM-SIV (nucleus_kernels::aes, the CPU’s
instructions) under a key derived from the subjects’ data key (the
SHA-256 of their keys in their ids’ order, as a backup seals with): a
tag byte (0xE5, which no row encoding begins with), a version, the
subjects’ key ids, a nonce, the sealed length, then the sealed bytes up
to 32 KiB — the rest in chunks under the table’s large-value index,
column 0xFFFF. The row key and the header are the associated data: a
sealed value opens only under its own key, as written. A row needs a
subject (its subject columns all NULL: 23502); a table with no
subject columns cannot be written (55000, SET SUBJECT first); no
store of subject keys set: 55000. A write for a subject forgotten
after the writer’s snapshot began fails (40001); run again, it finds
the rows gone and makes a new key.
What stays in the clear — said so, not hidden: the primary key (make
it a uuid or a number that says nothing), the subject columns (they
are indexed, and a sealed table’s indexes may cover only them: an index
on another column, an expression, a UNIQUE constraint on one, a text
or HNSW index — 0A000, checked whenever the table’s definition is
stored), how many rows and versions each subject has, and when they were
written. ANALYZE counts a sealed table’s rows and keeps no values
(its statistics and kept sample would hold them in the clear).
Reading. A row whose key is destroyed reads as absent — as forgetting it deleted it — in scans, lookups and through the subject index (whose entries for it stay where the rows stay: on a branch, in history), and the integrity check counts it as no fault. A backup copies the sealed rows (and seals them again: §8.30); a restore after the key is destroyed leaves them out.
Converting. SET STORAGE sealed on a row table (with subject
columns, and no index the rule refuses — dropped first) switches the
table at once — state Sealing: reads take both forms, writes seal —
then seals the rows a thousand at a time, each part a transaction of
its own; SET STORAGE row the other way (state Unsealing: writes plain,
the sealed rows opened, a forgotten one removed). Cut short, running
it again finishes it, as §8.15’s conversions. The plain rows’ bytes
stay in the pages and log segments they were in until those are reused —
or zeroed by a scrub (FORGET SUBJECT runs one, §8.29) — and in pages
a branch made before still shares.
Keys their users hold (K4b). A subject’s key can be given into its
user’s keeping: Session::hold_subject_key(subject, secret) (an
administrator’s, 42501; over the protocol the k message, Rust
Client::hold_subject_key), secret being 32 bytes only the user has
— a passkey’s PRF output (WebAuthn’s prf extension), a device’s key.
The store then keeps the key only wrapped (AES-256-GCM-SIV under
HMAC-SHA256 of the secret and the subject’s id, so one secret opens one
subject’s key; the key file’s line h id key-id wrapped), and the
subject’s sealed rows open only in a session that presented the secret
(Session::present_subject_key; every driver’s present_subject_key,
§11 — protocol version 8’s k message) — the operator’s sessions included. Kept in the session’s memory only,
zeroed when it ends, never written or logged; a statement never carries
it. Without it the rows are refused, not hidden (28000, naming the
table — a query that would read them fails rather than answer short); a
wrong secret is refused when presented. A thread keeps no such key
between statements (threads serve several sessions).
Who a row is stays readable without the user: the header holds the
subject columns’ values sealed apart, under a key derived from the
store’s pepper (outside the data and its backups, like the keys — a
backup alone says nothing of whose its rows are). So the work no user
is present for reads such a row as its primary key and subject columns,
the others NULL (sealed::Held::Who): FORGET SUBJECT finds and
deletes it as any other, an index on the subject columns is filled
(CREATE INDEX, a restore’s rebuild) — while expiry passes it over
(its time is sealed) and the embedding queue lets its job go (its text
is sealed: such a table’s embedding columns are made only while the user
is present). A backup copies the rows sealed (Held::Absent), and
refuses a database where a subject whose user holds its key has rows in
a table not stored sealed (55000: it could neither seal them nor leave
them out). A key lost is the rows lost: nothing else opens them.
Sealed memory stores (K4c). CREATE MEMORY STORE notes USING m WITH (sealed) (§8.28) is a memory store whose table is stored sealed
under the subject column: its only index is the subject’s, and
recall ranks the subject’s current memories exactly — BM25 by those
rows’ statistics (bm25_stats, bm25, §8.19), the vectors’ nearness
computed for each — so no index holds a text or a vector in the clear.
REMEMBER merges as in any store (the same text, or the nearest vector
among the subject’s, computed exactly). A user’s memories are few: at
20 a subject recall costs 376 µs against the indexed store’s 299 µs,
at 100 alike (docs/benchmarks.md).
Tests: crates/sql/tests/sealed.rs (nothing of a sealed table in any
file, logged or checkpointed, a 100 KB row’s chunks included; every way
of indexing a sealed column refused; a subject forgotten — unreadable on
a branch made before, AS OF before, in a backup restored, with the
integrity check clean on the branch, the subject’s new rows under a new
key; a row table sealed and opened again online, its index rules, a
FORGET between; opened again on a branch where a subject was
forgotten, its index entries gone with its rows; a key its user holds —
the key file keeping it wrapped, another session refused until it
presents the secret, a wrong secret refused, a backup restored opening
for the user alone, FORGET by a session without it deleting the rows
and the key), the CLI’s a_key_its_user_holds_over_the_network (the
k message against a server); a sealed memory store — its indexes,
REMEMBER’s merge, recall within the subject, its definition, nothing
in the clear, nothing to recall after FORGET. The cost
(the_cost_of_sealing, ignored; docs/benchmarks.md): ×1.9 to load,
×4 to scan, lookups alike — each thread keeps the derived key of each
set of key ids, dropped whenever a key is destroyed.