Skip to main content
Home › Categories › Artificial Intelligence & Computing › Database Internals: Relational Algebra, ACID Transactions, MVCC, and Query Optimizers

Database Internals: Relational Algebra, ACID Transactions, MVCC, and Query Optimizers

Author: ZUVDEX Team 4 min read
relational databases acid transactions mvcc acid atomicity isolation serializable write ahead logging wal crash recovery multi version concurrency control mvcc cost based query optimizer hash merge nested loop join oltp vs columnar olap duckdb

Edgar F. Codd’s 1970 relational model separated logical declarative queries ("what data you want" in SQL) from physical storage layout ("how disk pages and indexes fetch it"). Beneath every reliable transactional system—banking ledgers, inventory reservations, user authentication, and hybrid AI metadata stores—relational database engines enforce ACID guarantees via Write-Ahead Logging (WAL), Multi-Version Concurrency Control (MVCC), and cost-based relational query optimizers.

Key Highlights

  • ACID comprises Atomicity (all-or-nothing), Consistency (schema/foreign-key/check invariants), Isolation (concurrent transaction safety), and Durability (committed writes survive power loss via `fsync` Write-Ahead Logs).
  • Multi-Version Concurrency Control (MVCC) keeps timestamped tuple versions so **readers never block writers and writers never block readers**.
  • Default `Read Committed` isolation prevents dirty reads but still permits lost updates under concurrent read-modify-write unless guarded by `SELECT ... FOR UPDATE`, atomic `UPDATE x = x - 1`, or `Serializable` / Snapshot isolation.
  • Row-oriented OLTP engines (`PostgreSQL`, `MySQL`, `SQLite`) excel at millisecond point reads/writes, whereas vectorized Columnar OLAP engines (`DuckDB`, `ClickHouse`, `BigQuery`, `Parquet`) scan billions of analytical rows 50× faster.

Write-Ahead Logging, MVCC Snapshots, and Isolation Levels

Randomly flushing modified 8 KB B+ Tree pages scattered across an SSD on every commit would bottleneck throughput; instead, WAL appends a sequential redo record to the log and calls `fsync()` once—guaranteeing full crash recovery even if the server loses power microsecond later before dirty buffer-pool pages flush.

Under MVCC (used by PostgreSQL, Oracle, InnoDB, and DuckDB), updating a row writes a new physical version tagged with transaction ID `xmin` rather than overwriting bytes in place while concurrent analytical queries read a consistent historical snapshot. Background `autovacuum` (or undo-log purge) reclaims dead row versions once no active transaction snapshot can see them.

Isolation Level Dirty Read Lost Update / Non-Repeatable Read Write Skew Phantom When to Use
Read Committed (Postgres Default) Prevented Possible on naive read-then-write Possible General CRUD web reads + atomic SQL updates
Repeatable Read (Snapshot) Prevented Prevented (aborts concurrent conflict) Possible on disjoint rows Consistent multi-query reports & financial snapshots
Serializable (SSI) Prevented Prevented Prevented (100% serial equivalence) Ledger double-spend, seat/inventory booking invariants

Cost-Based Query Optimizers, `EXPLAIN ANALYZE`, and Columnar OLAP

When SQL arrives, the Cost-Based Optimizer consults column cardinality histograms and correlation statistics to choose between B+ Tree Index Scans, Bitmap Heap Scans, or full Sequential Scans, and selects Nested-Loop Joins (fast for small indexed outer sets), Hash Joins (workhorse for large unsorted equi-joins), or Sort-Merge Joins. For analytical aggregations (`SUM(revenue) GROUP BY region`) over 500 million rows of 80-column wide tables, row stores waste I/O reading all 80 columns; columnar engines like DuckDB read only the 2 compressed column vectors directly into SIMD CPU registers.

  • Order composite B+ Tree index columns `(tenant_id, status, created_at DESC)` matching equality predicates first (`=`) followed by range/sort columns (`>`, `ORDER BY`).
  • Always inspect `EXPLAIN (ANALYZE, BUFFERS)` on slow queries—stale table statistics (`ANALYZE`) cause the planner to misestimate row counts by 1,000× and pick catastrophic nested loops.
  • Never hold a database transaction open across a slow external HTTP or LLM API call—exhausting connection pools and blocking MVCC vacuuming!

FAQs

Why shouldn’t you add a B+ Tree index to every single column in a table?

Every extra index consumes RAM/disk and forces every `INSERT`, `UPDATE`, and `DELETE` to modify multiple B+ Trees simultaneously (preventing PostgreSQL HOT heap-only tuple updates and slowing write throughput).

How can embedded SQLite or DuckDB be so fast without a separate database server?

Eliminating client-server TCP serialization lets in-process SQLite execute indexed C function reads in microseconds for edge/local apps, while embedded DuckDB runs multi-threaded SIMD vectorized analytics directly over local Parquet files.

References & Sources

  • Codd, Edgar F. (1970) — A Relational Model of Data for Large Shared Data Banks, Communications of the ACM, 13(6), 377–387.
  • Petrov, Alex — Database Internals: A Deep Dive into How Distributed Data Systems Work, O’Reilly Media.
  • Pavlo, Andrew — CMU 15-445 / 15-721 Database Systems & Advanced Database Systems Lectures.