niwid-db is a toy database management system built from scratch, designed to explore fundamental database concepts, including memory and disk management, ACID properties, basic transaction handling, indexing, and query execution.
ACID Compliance via Shadow Paging: Ensures Atomicity, Consistency, Isolation (read-committed), and Durability by implementing shadow paging, maintaining a stable, crash-resistant state. Shadow paging allows a table to have only a single writer for simplified concurrency control.
Transaction Management: Supports commits and rollbacks to execute or discard changes within a transaction block to ensure transactional atomicity and isolation.
Custom Execution Engine: A simple query engine that processes SQL statements, validates the query and datatypes, and performs basic DDL, TCL, and DML operations. Also supports EXPLAIN and EXPLAIN ANALYZE. Unoptimized logical plans are executed directly, without physical plan generation or optimizations yet.
SQL Parsing: The only part not written from scratch. Leverages the sqlparser-rs crate for SQL syntax parsing.
B+ Tree Indexing: Implements a B+ Tree index to ensure uniqueness, enabling efficient lookups and range queries for unique columns. Since there is no optimizer yet, you can force an index lookup using the PREWHERE clause. Check out index.slt for more examples.
Constraints: Supports NOT NULL and UNIQUE constraints to enforce data integrity, although primary keys and foreign keys (referential integrity) are not supported.
Joins: Performs basic nested loop join operations between tables, allowing for relational queries.
Clone the repository:
bash
git clone https://github.com/MohamedAbdeen21/niwid-db.git
cd niwid-db
Run the server:
bash
cargo run --release --bin idk-server
Connect to the server from a different terminal:
bash
nc localhost 8080
niwid-db can execute SQL queries for managing tables, running transactions, and executing basic SQL commands.
Example:
CREATE TABLE users (
id INT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT
);
INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
You can also use transactions to batch operations:
BEGIN;
UPDATE users SET email = 'bob@example.com' WHERE id = 1;
ROLLBACK; -- Discards the update
For more examples, check out:
SELECT * FROM __CATALOG__;
Using sqlparser.rs is the only part not written from scratch. I wrote my fair share of parsers (and contributed some to sqlparser-rs), but again, this was not the main focus of the project.Contributions are always welcome! Feel free to submit issues or pull requests.
$ claude mcp add niwid-db \
-- python -m otcore.mcp_server <graph>