‘PGSimCity’ shows how the PostgreSQL database actually works internally in a SimCity-like 3D environment.

PostgreSQL, a robust and widely adopted database management system, often presents challenges in visualizing its internal operations during SQL query execution. To bridge this gap, a new web application named PGSimCity has emerged, offering a unique 3D representation of PostgreSQL’s architecture reminiscent of the classic SimCity game.

Exploring PGSimCity

Upon accessing PGSimCity, users are greeted with an interactive interface. A guided tour initiates, showcasing the connection process to PostgreSQL. The central feature, labeled ‘The service address,’ serves as the entry point for client connections, illustrating how the TCP connection reaches the Postmaster before being directed to the backend for processing. An Inspector panel on the right highlights ‘removed dead tuples,’ emphasizing the flow of client connections originating from the bottom left, with the Postmaster acting as the reception point.

As connections increase, so do the backend processes, represented by buildings in the foreground. Each backend corresponds to a dedicated process, and the importance of connection pooling becomes evident, as even idle connections incur memory and management costs.

Understanding Query Execution

In the ‘Query Lab’ section, SQL statements transform into execution plans. The SELECT statement appears prominently at the top, while various processes such as Parser, Rewriter, Planner, and Executor are displayed below, alongside potential execution paths like Seq Scan, Index Scan, and Bitmap Heap Scan. This segment illustrates how PostgreSQL interprets the requested data and determines the optimal retrieval method based on statistical insights.

The shared_buffers area represents PostgreSQL’s shared buffer cache. Here, data is read in 8KiB pages rather than row by row, optimizing performance. A higher CACHE HIT ratio indicates that the necessary pages are already in memory, thus minimizing disk access.

Data Management Mechanisms

Visualizations further depict the contents of data pages stored on disk, with the accounts table illustrated as a series of 8KiB pages. The green pathways signify the movement of pages from disk into shared_buffers. Additionally, the Write-Ahead Log (WAL) mechanism is highlighted, demonstrating how changes are recorded before data pages are written. This ensures recovery is possible post-failure, as long as the WAL remains intact.

Transaction commits are designed to wait for WAL records to be safely flushed, rather than for the modified data pages to be written immediately. The interface also illustrates the implications of having synchronous_commit disabled, showcasing the balance between reduced waiting times and the risk of losing the latest commits during failures.

Checkpointing and Maintenance

Checkpointing is another critical aspect, where the checkpointer writes dirty pages from shared_buffers to storage in batches. The interface indicates when a checkpoint occurs, revealing how frequent checkpoints can lead to latency spikes due to concentrated write loads.

PostgreSQL employs Multi-Version Concurrency Control (MVCC), which allows updated rows to coexist with their previous versions. This mechanism results in the creation of dead tuples, which, while not immediately blocking operations, necessitate eventual cleanup through autovacuum processes. The autovacuum worker identifies tables with excessive dead tuples and frees up space, although it does not typically reduce the overall file size.

Replication and Overall Process Flow

The application also addresses replication, illustrating how WAL generated on the primary server is transmitted to the standby server. This process involves the walsender and walreceiver, which ensure that physical changes on data pages are sequentially applied on the standby, maintaining data consistency across systems.

As users navigate through PGSimCity, they can observe the interconnectedness of various components: client connections, Postmaster, backend processes, Query Lab, shared_buffers, WAL, and maintenance operations. This holistic view underscores the complexity of PostgreSQL’s architecture and highlights potential performance issues that may arise from bottlenecks in caching, WAL management, checkpointing, vacuuming, or replication.

Tech Optimizer
'PGSimCity' shows how the PostgreSQL database actually works internally in a SimCity-like 3D environment.