PGSimCity: A Game-Changing Simulation of How PostgreSQL Works

If you've ever wanted to peek inside the engine of PostgreSQL without diving into thousands of lines of C code, there's a new tool that makes database internals as tangible as a city skyline. PGSimCity is an open‑source simulation project that visualizes how PostgreSQL processes queries, manages memory, and coordinates concurrent transactions—all in an interactive, SimCity‑inspired environment.

PGSimCity, developed by Nikolay S. and available on GitHub, models the core subsystems of PostgreSQL: the buffer pool, lock manager, query executor, and write‑ahead log (WAL). Instead of dry diagrams, the simulation lets you watch the data flow between processes, see pages being evicted from cache, and observe locks being acquired and released in real time. It’s a living blueprint of a relational database.

Why Understanding PostgreSQL Internals Matters

Database performance tuning often feels like black magic. Why does an index scan suddenly turn into a sequential scan? Why does a simple DELETE cause a cascade of row locks? PGSimCity demystifies these behaviors by showing the actual mechanics at play.

  • Query Execution: See how the planner chooses a scan method and how the executor pulls tuples from disk or memory.
  • Concurrency Control: Watch how MVCC (Multi‑Version Concurrency Control) allows readers to see consistent snapshots while writers update rows.
  • Buffer Management: Observe which pages are in the shared buffer pool and how the clock‑sweep algorithm evicts old pages when memory fills up.

For a database administrator, this level of insight can transform vague intuition into precise understanding. For a developer, it reveals the hidden cost of every SQL statement.

How the Simulation Works

PGSimCity breaks down PostgreSQL into a set of interactive agents and resources. You start with a basic configuration—say, a simple table with some rows—and then send SQL commands to the simulated engine. The simulation draws each step: the parser, the optimizer, the executor, and finally the I/O subsystem.

For example, imagine you run SELECT * FROM orders WHERE status = 'pending'. The simulation might show:
1. The user process acquires a read lock.
2. The buffer manager looks for the necessary data pages in the shared pool.
3. If the pages are missing, a background writer triggers a disk read (visualized as a truck delivering a page).
4. The executor iterates through tuples, filtering those matching the condition.
5. The session releases the lock after the result set is built.

All these steps are animated, with metrics like cache hit ratio and lock contention updated live. It’s like watching your database breathe.

Real‑World Applications

PGSimCity isn’t just a toy—it’s a powerful teaching aid and debugging assistant. University courses on database systems have begun using such simulations to replace static slides. Students can experiment with different isolation levels and immediately see the resulting deadlocks or serialization anomalies.

Production engineers also benefit: by replaying a problematic workload in the simulation, they can isolate the exact bottleneck—be it a missing index, a lock escalation, or a full table scan that evicts hot data from the buffer pool. The project’s documentation explicitly notes that it helps “understand PostgreSQL without risking a real cluster.”

Technical Highlights

PGSimCity implements several key PostgreSQL features faithfully:

PostgreSQL Component Simulated Behavior
Buffer Manager Shared buffer pool with configurable size; page replacement policy (clock‑sweep).
Lock Manager Row‑level locks, table‑level locks, deadlock detection.
Query Executor Sequential scan, index scan, nested loop join.
WAL Write‑ahead logging with checkpoint simulation.

The project is written in a high‑level language (likely Python or JavaScript) to keep the simulation accessible. According to the source code Source, developers can extend it with new modules—for example, adding a new lock mode or customizing the cost model.

The Future of Learning Database Internals

PGSimCity joins a growing ecosystem of interactive database education tools. Its strength lies in making abstract concepts observable. Instead of memorizing that “a buffer miss causes a disk I/O,” you see the request travel from the process to the disk controller and back.

As PostgreSQL continues to dominate the open‑source RDBMS landscape (ranking second only to MySQL in many surveys), tools like this become essential. Whether you’re a seasoned DBA or a student tackling your first database course, PGSimCity offers a sandbox where every query teaches something new.

Start exploring PostgreSQL’s internals today by running the simulation yourself. Visit the project page, download the source, and watch your database come to life.


This article summarizes the open-source project PGSimCity. For the full details, including setup instructions and technical documentation, refer to the original source.

← All posts

Comments