Skip to main content

Catalog Manager — System Catalogs

RookDB implements six system catalog tables that store all database metadata. Each catalog is stored as a dedicated .dat file in database/global/catalog_pages/ using the standard slotted-page format.

Catalog tuples are serialised as variable-length byte slices. Variable-length strings use a [u16 len (LE)] [bytes …] encoding; arrays use [u16 count (LE)] [element × count].


pg_database — Database Metadata

Stores one record per database in the system.

Column NameData TypeConstraintsDescription
db_oidUINT32PRIMARY KEYUnique database identifier
db_nameVARCHAR(64)UNIQUE, NOT NULLDatabase name
db_ownerVARCHAR(64)NOT NULLDatabase owner
db_created_atUINT64Creation timestamp (Unix epoch)
db_encodingUINT8NOT NULLCharacter encoding (1 = UTF-8, 2 = ASCII)

Binary layout:

db_oid(4) | db_name(var) | db_owner(var) | created_at(8) | encoding(1)

Example record: The bootstrap process creates a system database:

db_oid = 1, db_name = "system", db_owner = "rookdb", encoding = UTF-8

pg_table — Table Metadata

Stores one record per table (both user tables and system catalog tables).

Column NameData TypeConstraintsDescription
table_oidUINT32PRIMARY KEYUnique table identifier
table_nameVARCHAR(64)NOT NULLTable name
db_oidUINT32FOREIGN KEY → pg_databaseParent database
table_typeUINT8NOT NULLType: 0 = user table, 1 = system catalog
row_countUINT64Estimated row count
page_countUINT32Number of data pages
created_atUINT64Creation timestamp (Unix epoch)

Binary layout:

table_oid(4) | table_name(var) | db_oid(4) | table_type(1)
| row_count(8) | page_count(4) | created_at(8)

pg_column — Column Metadata

Stores one record per column across all tables.

Column NameData TypeConstraintsDescription
column_oidUINT32PRIMARY KEYUnique column identifier
table_oidUINT32FOREIGN KEY → pg_tableParent table
column_nameVARCHAR(64)NOT NULLColumn name
column_posUINT16NOT NULLPosition in table (1-based)
type_oidUINT32FOREIGN KEY → pg_typeData type OID
type_lengthINT16Fixed byte length (-1 for variable)
type_alignUINT8Alignment requirement (bytes)
type_categoryUINT8Category (1=numeric, 2=string, 3=datetime, 4=boolean, 5=binary)
type_nameVARCHAR(32)Type name string
type_mod_flagUINT8Type modifier: 0=none, 1=varchar len, 2=precision
type_mod_datavariableModifier payload (if flag > 0)
is_nullableBOOLNOT NULLNULL allowed (true/false)
has_defaultBOOLWhether a default value is defined
default_datavariableSerialised default value (if present)
constraint_oidsUINT32[]Array of constraint OIDs for this column

Binary layout:

column_oid(4) | table_oid(4) | column_name(var) | column_pos(2)
| type_oid(4) | type_length(2) | type_align(1) | type_category(1)
| type_name(var) | type_mod_flag(1) [type_mod_data]
| is_nullable(1) | has_default(1) [default_tag(1) default_data]
| num_constraints(2) | constraint_oid[*]

Default Value Tags

TagTypePayload
1Integer4 bytes (i32 LE)
2BigInt8 bytes (i64 LE)
3Float4 bytes (f32 LE)
4Double8 bytes (f64 LE)
5Stringvariable-length string
6Boolean1 byte
7Null0 bytes
8CurrentTimestamp0 bytes

pg_constraint — Constraint Metadata

Stores one record per constraint.

Column NameData TypeConstraintsDescription
constraint_oidUINT32PRIMARY KEYUnique constraint identifier
constraint_nameVARCHAR(64)NOT NULLConstraint name
constraint_typeUINT8NOT NULLType: 1=PK, 2=FK, 3=UNIQUE, 4=NOT NULL, 5=CHECK
table_oidUINT32FOREIGN KEY → pg_tableConstrained table
column_oidsUINT32[]Array of constrained column OIDs
is_deferrableBOOLDeferrable constraint (future)

Type-specific metadata (appended after the base fields):

Primary Key

FieldTypeDescription
index_oidUINT32OID of the backing unique index

Foreign Key

FieldTypeDescription
referenced_table_oidUINT32Referenced table OID
referenced_column_oidsUINT32[]Referenced column OIDs
on_deleteUINT8Action: 0=NO ACTION, 1=CASCADE, 2=SET NULL, 3=RESTRICT
on_updateUINT8Action: 0=NO ACTION, 1=CASCADE, 2=SET NULL, 3=RESTRICT

Unique

FieldTypeDescription
index_oidUINT32OID of the backing unique index

NOT NULL

No additional metadata.

Check

FieldTypeDescription
check_expressionVARCHAR(256)SQL expression string

Binary layout:

constraint_oid(4) | constraint_name(var) | constraint_type(1)
| table_oid(4) | column_oids(var array) | is_deferrable(1)
| <type-specific payload>

pg_index — Index Metadata

Stores one record per index.

Column NameData TypeConstraintsDescription
index_oidUINT32PRIMARY KEYUnique index identifier
index_nameVARCHAR(64)NOT NULLIndex name
table_oidUINT32FOREIGN KEY → pg_tableIndexed table
index_typeUINT8NOT NULLType: 1=B-Tree, 2=Hash (future)
column_oidsUINT32[]NOT NULLIndexed column OIDs
is_uniqueBOOLNOT NULLUnique index flag
is_primaryBOOLNOT NULLPrimary key index flag
index_pagesUINT32Number of index pages

Binary layout:

index_oid(4) | index_name(var) | table_oid(4) | index_type(1)
| column_oids(var array) | is_unique(1) | is_primary(1) | index_pages(4)

Index File Storage

Each index is stored as a separate B-Tree file at:

database/base/{database}/indexes/{index_name}.idx

The B-Tree page layout:

  • Byte 0: Node type (1 = leaf, 0 = internal)
  • Bytes 1–2: Number of keys (u16 LE)
  • Bytes 3–4: Lower pointer (u16 LE)
  • Bytes 5–6: Upper pointer (u16 LE)
  • Bytes 7–10: Right sibling pointer / next leaf (u32 LE, leaf only)
  • Bytes 11+: Slot directory (4 bytes per entry: offset + length)

pg_type — Data Type Metadata

Stores one record per registered data type.

Column NameData TypeConstraintsDescription
type_oidUINT32PRIMARY KEYUnique type identifier
type_nameVARCHAR(32)UNIQUE, NOT NULLType name (e.g., INT, VARCHAR)
type_categoryUINT8NOT NULLCategory: 1=numeric, 2=string, 3=datetime, 4=boolean, 5=binary
type_lengthINT16Fixed length (-1 for variable)
type_alignUINT8Alignment requirement (bytes)
is_builtinBOOLNOT NULLBuilt-in type flag

Binary layout:

type_oid(4) | type_name(var) | type_category(1)
| type_length(2) | type_align(1) | is_builtin(1)

Built-in Types

The following types are registered during bootstrap:

OIDNameCategoryLengthAlignment
1INTNumeric44
2BIGINTNumeric88
3FLOATNumeric44
4DOUBLENumeric88
5BOOLBoolean11
6TEXTString-1 (variable)1
7VARCHAR(255)String-1 (variable)1
8DATEDateTime44
9TIMESTAMPDateTime88
10BYTESBinary-1 (variable)1

Type Name Aliases

The type resolver accepts the following aliases (case-insensitive):

CanonicalAliases
INTINTEGER, INT32
BIGINTINT64
FLOATREAL, FLOAT32
DOUBLEFLOAT64
BOOLBOOLEAN
TEXTSTRING
BYTESBYTEA, BLOB

Relationships Between System Catalogs

pg_database

└──(db_oid)──→ pg_table

├──(table_oid)──→ pg_column ──→ pg_type (via type_oid)

├──(table_oid)──→ pg_constraint

└──(table_oid)──→ pg_index

All cross-references use OID-based foreign keys, enabling consistent lookups across the catalog hierarchy.


See Also