Referencia · §8.2

Catalog and keyspace

catalog

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

Key Value
00 00 the format record: keyspace layout (1) and key encoding (2) versions, u16 each
00 01 <table name> table definition (columns, primary key, indexes, checks)
00 02 <sequence> counter (table_id)
00 03 <table id> next hidden row id (tables without a primary key)
00 04 <table id> statistics (§8.8)
00 04 <table id> 01 the rows ANALYZE keeps for estimates (§8.8)
00 05 <type> 00 <ordering> an ordering taught to a type: its mapping (SQL over value)
00 06 <symbol> 00 <left> 00 <right> an operator taught: its function
00 07 <from> 00 <to> a cast taught: its function and context
00 08 <type> the oldest encoding version a registered type’s stored values may be in
00 09 <enum> an enum: its labels and their sort keys, in order
00 0A <role> a role: its attributes and password verifier (§9)
00 0B <role> 00 <member> a membership: its admin option
00 0C <kind> <object> an object’s ACL (privileges per grantee, and per column)
00 0D <role> 00 <kind> a role’s default privileges for objects it makes
00 0E <table> 00 <policy> a row-level security policy
00 0F <login> a token: its role, ceiling, expiry and secret’s verifier (§9)
00 14 <name> a secret: its owner and its sealed value, or the environment variable it names (§9)
00 15 <name> a model: its provider, options, secret, prefixes, dimensions, batch and files (§8.23)
00 16 <SHA-256> <chunk> a model’s file, by content: 32 KiB chunks, shared by the models made from it (§8.23)
00 17 <table id> <column u16> <pk> an embedding column’s job: the row’s vector to make; column bit 0x8000: its next model’s (§8.24)
00 18 <table id> <column u16> <pk> a moving embedding column’s next model’s vector, until the swap (§8.24)
00 19 <seq> an erasure record (§8.29); 00 1A the last number
00 1B <version u64> a migration applied: its name, checksum, when and by whom (§8.34)
00 1C <table id> a table being moved to another engine: the id its keys are copied under (§4.6)
00 1D <name> a property graph: its owner, vertex and edge tables, keys, ends, labels and properties (§M14.34 G4)
00 1E <name> a sequence: its id, owner, type, increment, bounds, start, cache, cycle, OWNED BY and version (issue #23)
00 1F <id u64> a sequence’s reservation: the furthest value it may have handed out, and whether one was; 00 1F alone the next sequence id
00 25 00 a replica’s position in its primary’s log, in each commit it applies: version, restart LSN, commit, slot (§9, §M17.5)
00 25 01 the database’s epochs: version, then each epoch after the first and the last commit before it (§9 “Failover”, §M17.5 R4)
00 25 02 the database’s identity: 8 random bytes, made when a primary first serves replicas (§9 “Failover”, §M17.5 R4b)
00 26 00 the next range id (u64) (§6.6, §M18)
00 26 02 01 <end>, 00 26 02 02 a range’s descriptor, keyed by its end key (02: the last range): version, id, generation, sticky, start, end (§6.6)
00 27 <id u64> 01 … 05 a replicated range’s state on its node: 01 the last entry applied, 02 frozen and its descriptor, 03 <client> a client’s last command and its result, 04 removed here for good, 05 the members it started with (§6.7, §M19 R6)
01 <table id> 00000000 <pk> the row
01 <table id> <index id> <index values> [<pk>] index entry → encoded pk
01 <table id> FFFFFFFF <pk> <column u16> <chunk u32> a chunk of a value stored out of line

A unique index entry is keyed by the index values alone (unless one is NULL), so two transactions inserting the same value write the same key and the second conflicts; other entries append the primary key. The catalog is read through the statement’s own transaction, so DDL is transactional.

A session’s catalog cache (catalog_cache, §M14.20 W8) keeps what its statements derived from the catalog — tables’ definitions, database settings, objects’ ACLs and owners, an identity’s effective roles — for the statements after. A session asks the KV store to watch the catalog’s keys (00 00–00 01 and 00 04–00 11: not the counters, which inserts change, nor the audit log) and lends its cache to the thread running its statement; a lookup (memo) uses the cache when the transaction reads the catalog as the snapshot the cache was filled at (KvTxn::watched_as_at, §5.2), else empties it and fills it at the transaction’s snapshot if that reads the current catalog, else reads the catalog itself: an older snapshot (a transaction begun before another’s DDL, AS OF), a transaction that wrote the catalog, a serializable one that writes. Work on other threads, and any other implementation of the KV contract, read the catalog as before. A parameterized point read on one connection: 52 → 42 µs.

The same cache keeps a query’s plan (bound, checked, optimized, pruned: session::Planned) and a statement’s description (Session::describe(stmt, text)) by the statement’s text — the text a front end notes for a statement it runs (note_text), and gives when it describes one. An entry holds its statement and serves an equal one only; a kept plan replays the tables its privilege checks looked at (audit_tables). A text is kept the second time it comes (its hash remembered the first, up to 4 096), at most 512 of them, all dropped past that; the cache empties when the session’s identity changes (SET ROLE, a login). Only the statement a session runs is kept by its text (catalog_cache::with_text), not those a function runs inside it.

A read by key is kept by its shape (shape::by_key, PLAN.md §M14.33 R2): a key inline makes every text new, so such a read was bound and planned each time (16.5 of its 23.6 µs in the process). A SELECT of one stored table — not partitioned, no WITH, GROUP BY, window, sample or AS OF — whose WHERE is only equalities of a column and a literal naming every column of the primary key runs as its shape: those literals become parameters $1, $2, … and the plan is kept by the normalized text (statements::normalize), so one plan serves every key (9.2 µs). Only where the plan cannot depend on the value — at most one row, found by the key — as SQL Server’s simple parameterization; and only where the parameter carries what the literal did: an integer for a smallint, integer or bigint column it fits, a string for a text column. Anything else is planned as it comes.

A session keeps two more things by a statement’s text, with the same admission and bound (by_text::ByText, the one type every entry by a text is kept in): what the text parses to (Session::parse, which execute and both front ends use) and its key in the statement statistics (statements::normalize). Neither reads the catalog, so neither empties with it, nor with the session’s identity.

The format record (principle 6) is checked by a session’s first statement and written by the first write to a database the SQL layer has not used; a newer format than the build reads is refused with 0A000 rather than misread. check::check(txn) lists every inconsistency between the catalog, the rows and the indexes (rows under their primary key, NOT NULL, index entries exactly as the rows imply, unique indexes, values stored out of line whole and no chunks without one, foreign keys, no keys of unknown tables or indexes).