The Internals of PostgreSQL: Understanding Database Clusters, Databases, and Tables

Jaeyeol Lee

Hatched by Jaeyeol Lee

Sep 22, 2023

3 min read

0

The Internals of PostgreSQL: Understanding Database Clusters, Databases, and Tables

Introduction:
PostgreSQL is a powerful relational database management system that is widely used for storing and managing data. To truly understand PostgreSQL, it is important to dive into its internals and explore how it handles database clusters, databases, and tables. In this article, we will explore the key concepts and mechanisms behind these components.

Database Cluster and Base Directory:
At the core of PostgreSQL is the concept of a database cluster. A database cluster is a collection of databases managed by a PostgreSQL server. It is essentially a single directory, often referred to as the base directory, which contains subdirectories and numerous files. Each database within the cluster is a subdirectory under the base directory.

Databases and Tables:
Within a database cluster, databases and tables are fundamental components. A database is a subdirectory under the base directory, and each table and index is stored as at least one file within the subdirectory of the database it belongs to. The objects in PostgreSQL, such as databases and heap tables, are internally managed using object identifiers (OIDs), which are unsigned 4-byte integers. The OIDs of databases and heap tables are stored in the pg_database and pg_class tables respectively.

Tablespaces:
PostgreSQL also supports the concept of tablespaces, which are single directories that contain data outside of the base directory. Tables and indexes whose size is less than 1GB are stored in a single file under the corresponding database directory. However, if the size exceeds 1GB, PostgreSQL creates new files named like relfilenode.1, relfilenode.2, and so on. Additionally, each table has two associated files, namely '_fsm' and '_vm', which refer to the free space map and visibility map respectively. Indexes, on the other hand, only have individual free space maps.

Understanding File Organization:
To facilitate efficient data storage and retrieval, PostgreSQL divides data files into fixed-length pages or blocks, typically 8192 bytes (8 KB) in size. These pages are organized within a data file, which can represent a heap table, index, free space map, or visibility map. The first fork of the table/index data file is the free space map (fork number 1), while the second fork is the visibility map (fork number 2). The data file itself has a fork number of 0.

Data Consistency and Isolation:
One of the key features of relational databases, including PostgreSQL, is their ability to ensure data consistency. Data consistency refers to the correctness of data, and PostgreSQL achieves this through various mechanisms. Isolation, another important aspect, ensures that concurrent transactions do not interfere with each other. These features are crucial for maintaining the integrity of the data stored in a PostgreSQL database.

Insights and Unique Ideas:
While exploring the internals of PostgreSQL, some unique ideas and insights emerge. For example, PostgreSQL uses the write-ahead log (WAL) to maintain data consistency during its operation. The WAL records changes made to the database before they are applied to the actual data files, providing a reliable mechanism for recovery in the event of a failure.

Actionable Advice:

  1. Optimize your database cluster by distributing data between tablespaces strategically. Consider storing archive data on slower disks while keeping actively updated data on faster disks for improved performance.
  2. Regularly vacuum your tables to reclaim space and update the free space map. This helps PostgreSQL efficiently find pages that can accommodate new data.
  3. Consider implementing connection pooling to limit the number of spawned backends and improve the performance of short and frequent connections. This reduces the overhead of establishing connections and improves overall scalability.

Conclusion:
Understanding the internals of PostgreSQL, particularly the concepts of database clusters, databases, and tables, is essential for effectively managing and optimizing your PostgreSQL deployment. By exploring the file organization, data consistency mechanisms, and unique insights, you can gain a deeper understanding of how PostgreSQL handles data storage and retrieval. Implementing the actionable advice provided can further enhance the performance and efficiency of your PostgreSQL database.

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣