Heap storage model in PostgreSQL

The heap storage model in PostgreSQL offers simplicity and flexibility, for specific use cases where ordered storage or specialized access methods are required.

In PostgreSQL, the heap storage model refers to the way data is organized within tables. Here's how it works:

  1. Unsorted Storage: In a heap storage model, rows are stored in no particular order within the data pages of a table. When new rows are inserted, they are simply added to the end of the table's data pages.

  2. Tuple Structure: Each row, also known as a tuple, consists of a header and the actual data values. The header contains information such as the number of columns, null flags, and other metadata.

  3. Visibility Information: PostgreSQL uses a multiversion concurrency control (MVCC) mechanism to manage concurrent transactions. Each tuple contains visibility information, including a transaction ID indicating the creating transaction and a "till when" field indicating until when the tuple is visible.

  4. Indexes for Retrieval: While the data within the heap itself is unsorted, PostgreSQL uses indexes to efficiently retrieve data based on specific criteria. Indexes are separate data structures that provide ordered access to the tuples based on the values of certain columns.

  5. Autovacuum: PostgreSQL employs a background process called autovacuum to manage the space within the heap. Autovacuum is responsible for reclaiming space occupied by deleted or updated tuples, thereby preventing excessive bloat and ensuring efficient use of storage.

Techgoda

We are a community of developers who are passionate about technology and programming.

Check out Techgoda code