API Doc
Index of APIs
Core Catalog APIs
- Init Catalog
- Load Catalog
- Create Database
- Drop Database
- Show Databases
- Select Database
- Show Tables
- Create Table
- Drop Table
- Alter Table Add Column
- Get Table Metadata
Constraint APIs
- Add Primary Key Constraint
- Add Foreign Key Constraint
- Add Unique Constraint
- Add Not Null Constraint
- Validate Constraints
- Get Constraints for Table
Index APIs
- Create Index
- Drop Index
Type APIs
- Register Built-in Types
- Lookup Type by Name
Page / Table / Tuple APIs
- Init Table
- Init Page
- Page Count
- Create Page
- Read Page
- Write Page
- Page Free Space
- Add Tuple to Page
- Read Item / Get Tuple
Core Catalog API Descriptions
0. init_catalog API
Description:
Dual-mode catalog initialisation called at startup. Detects whether page-based catalog storage exists and bootstraps the system if necessary.
Function:
pub fn init_catalog(bm: &mut BufferManager)
Input:
bm— Mutable reference to the buffer manager.
Implementation:
- Create
database/global/anddatabase/base/directories if they do not exist. - Check if
database/global/catalog_pages/exists. - If yes, report that the page backend is detected.
- If no, call
bootstrap_catalog(bm)to initialise the system catalogs, register built-in types, and create the system database.
1. load_catalog API
Description:
Loads the catalog from the active storage backend. Attempts page-based loading first; falls back to an empty catalog on failure.
Function:
pub fn load_catalog(bm: &mut BufferManager) -> Catalog
Output:
- Returns a
Catalogstruct populated with all databases, tables, columns, constraints, and index OIDs.
Implementation:
- If
catalog_pages/exists, load from pages:- Read OID counter from
pg_oid_counter.dat - Scan
pg_database→ populatecatalog.databases - Scan
pg_table→ attach tables to parent databases bydb_oid - Scan
pg_column→ attach columns to tables bytable_oid, sort by position - Scan
pg_constraint→ attach constraints to tables bytable_oid - Scan
pg_index→ attach index OIDs to tables bytable_oid
- Read OID counter from
- On any failure, return
Catalog::new()(empty).
2. create_database API
Description:
Creates a new database with metadata (owner, encoding) and persists it to pg_database.
Function:
pub fn create_database(
catalog: &mut Catalog,
pm: &mut CatalogPageManager,
bm: &mut BufferManager,
db_name: &str,
owner: &str,
encoding: Encoding,
) -> Result<u32, CatalogError>
Input:
catalog— In-memory catalog metadata.pm— Catalog page manager for page-based persistence.bm— Buffer manager for I/O.db_name— Name of the new database (must be non-empty and unique).owner— Owner string (e.g.,"default_user").encoding— Character encoding (Encoding::UTF8orEncoding::ASCII).
Output:
- Returns the allocated
db_oidon success.
Implementation:
- Validate that the name is non-empty and not already in use.
- Allocate a new OID via
catalog.alloc_oid(). - Create
database/base/{db_name}/directory. - Serialise and insert a record into
pg_database. - Add the
Databasestruct to the in-memory catalog. - Invalidate the database cache entry.
3. drop_database API
Description:
Drops a database and all its tables, constraints, and indexes.
Function:
pub fn drop_database(
catalog: &mut Catalog,
pm: &mut CatalogPageManager,
bm: &mut BufferManager,
db_name: &str,
) -> Result<(), CatalogError>
Implementation:
- Resolve
db_oidfrom the in-memory catalog. - Drop all tables in the database via
drop_table(). - Find and delete the database record from
pg_database. - Remove the database directory from disk.
- Remove from in-memory catalog and invalidate cache.
4. show_databases API
Description:
Displays all databases from the page-based catalog with additional metadata.
Function:
pub fn show_databases(catalog: &Catalog, pm: &mut CatalogPageManager, bm: &mut BufferManager)
Output:
- Prints a formatted table:
Database | Owner | Created At. - Data is fetched directly from
pg_databaseviapm.scan_catalog().
5. show_tables API
Description:
Displays all user tables in a database from the page-based catalog with statistics.
Function:
pub fn show_tables(catalog: &Catalog, pm: &mut CatalogPageManager, bm: &mut BufferManager, db_name: &str)
Output:
- Prints a formatted table:
Table Name | Rows | Pages | Created At.
6. create_table API
Description:
Creates a new table with columns and constraints, persisting metdata to pg_table and pg_column.
Function:
pub fn create_table(
catalog: &mut Catalog,
pm: &mut CatalogPageManager,
bm: &mut BufferManager,
db_name: &str,
table_name: &str,
col_defs: Vec<ColumnDefinition>,
constraint_defs: Vec<ConstraintDefinition>,
) -> Result<u32, CatalogError>
Input:
col_defs— Column definitions: name, type name, nullability, default value.constraint_defs— Constraint definitions (PrimaryKey, ForeignKey, Unique, NotNull).
Output:
- Returns the allocated
table_oidon success.
Implementation:
- Validate the database exists and the table name is unique within it.
- Allocate
table_oidand per-column OIDs. - Resolve each column's type via
DataType::from_name(). - Serialise and insert column records into
pg_column. - Create and initialise the table data file via
init_table(). - Serialise and insert table record into
pg_table. - Add to in-memory catalog; invalidate cache.
- Process constraint definitions (PK, FK, UNIQUE, NOT NULL).
7. drop_table API
Description:
Drops a table and all its dependent objects (indexes, constraints).
Function:
pub fn drop_table(
catalog: &mut Catalog,
pm: &mut CatalogPageManager,
bm: &mut BufferManager,
table_oid: u32,
) -> Result<(), CatalogError>
Implementation:
- Check for FK dependencies from other tables — return
ForeignKeyDependencyerror if found. - Drop all indexes on this table.
- Remove the table data file.
- Delete the record from
pg_table. - Remove from in-memory catalog; invalidate cache entries.
8. alter_table_add_column API
Description:
Adds a new column to an existing table.
Function:
pub fn alter_table_add_column(
catalog: &mut Catalog,
pm: &mut CatalogPageManager,
bm: &mut BufferManager,
table_oid: u32,
col_def: ColumnDefinition,
) -> Result<u32, CatalogError>
Output:
- Returns the allocated
column_oid.
Constraints:
- If the column is NOT NULL, a default value must be provided.
- Column name must not already exist in the table.
9. get_table_metadata API
Description:
Retrieves complete table metadata including resolved columns, constraints, and indexes.
Function:
pub fn get_table_metadata(
catalog: &Catalog,
pm: &CatalogPageManager,
bm: &mut BufferManager,
db_name: &str,
table_name: &str,
) -> Result<TableMetadata, CatalogError>
Constraint APIs
For complete constraint API documentation, see the Catalog Manager API Reference.
10. add_primary_key_constraint API
pub fn add_primary_key_constraint(catalog, pm, bm, table_oid, column_names, constraint_name) -> Result<u32, CatalogError>
Adds a PK constraint with a backing unique B-Tree index. Sets referenced columns to NOT NULL.
11. add_foreign_key_constraint API
pub fn add_foreign_key_constraint(catalog, pm, bm, table_oid, column_names, referenced_table_oid, referenced_column_names, on_delete, on_update, constraint_name) -> Result<u32, CatalogError>
Adds an FK constraint. Validates column counts match and referenced columns are covered by PK/UNIQUE.
12. add_unique_constraint API
pub fn add_unique_constraint(catalog, pm, bm, table_oid, column_names, constraint_name) -> Result<u32, CatalogError>
Adds a UNIQUE constraint with a backing B-Tree index.
13. add_not_null_constraint API
pub fn add_not_null_constraint(catalog, pm, bm, table_oid, column_oid) -> Result<(), CatalogError>
Sets is_nullable = false on the specified column.
14. validate_constraints API
pub fn validate_constraints(catalog, pm, bm, table_oid, tuple_values) -> Result<(), ConstraintViolation>
Validates all constraints for a tuple before insertion. Returns specific violation errors (NotNull, Unique, ForeignKey).
Index APIs
For complete index API documentation, see the Catalog Manager API Reference.
15. create_index API
pub fn create_index(catalog, pm, bm, table_oid, column_oids, is_unique, is_primary, index_name) -> Result<u32, CatalogError>
Creates a B-Tree index on specified columns. Creates the index .idx file and persists metadata to pg_index.
16. drop_index API
pub fn drop_index(catalog, pm, bm, index_oid) -> Result<(), CatalogError>
Drops an index. Cannot drop indexes referenced by PK or UNIQUE constraints.
Page / Table / Tuple APIs
17. init_table API
Description:
Initializes the Table Header by writing the first page (8,192 bytes) with page_count = 1 in the first 4 bytes, followed by an empty data page.
Function:
pub fn init_table(file: &mut File) -> Result<(), io::Error>
Implementation:
- Write an 8,192-byte header page with page count = 1.
- Write an 8,192-byte empty data page via
create_page.
18. init_page API
Description:
Initializes a page header with lower offset (PAGE_HEADER_SIZE) and upper offset (PAGE_SIZE).
Function:
pub fn init_page(page: &mut Page)
19. page_count API
Description: Returns the total number of pages in a file by reading the first 4 bytes of page 0.
Function:
pub fn page_count(file: &mut File) -> u32
20. create_page API
Description:
Creates a new data page at the end of a file and increments the page count.
Function:
pub fn create_page(file: &mut File) -> Result<(), io::Error>
21. read_page API
Description:
Reads a page from disk into memory.
Function:
pub fn read_page(file: &mut File, page: &mut Page, page_num: u32) -> Result<(), io::Error>
22. write_page API
Description:
Writes a page from memory to disk.
Function:
pub fn write_page(file: &mut File, page: &mut Page, page_num: u32) -> Result<(), io::Error>
23. page_free_space API
Description:
Calculates free space = upper - lower.
Function:
pub fn page_free_space(page: &Page) -> Result<u32, io::Error>
24. Add Tuple API
Description: Adds raw data to a table file using the slotted-page layout.
Implementation:
- Read the last page.
- Check if free space ≥ data size +
ITEM_ID_SIZE. - If yes: write data at
upper - data.len(), update pointers, write ItemId. - If no: create a new page and insert there.
- Reference: Postgres Internals – Page Layouts & Data
Note: Some APIs have undergone implementation changes during development. Refer to the Catalog Manager Implementation Notes for details on deviations from the original design.