PostgreSQL Internals – Freelists implementation in PostgreSQL?

The freelists in PostgreSQL are implemented as a linked list of pages. Each page is a unit of disk space that is managed by the freelist, and its size is determined by the block size of the underlying file system, which is usually either 4KB or 8KB.

When a row is deleted from a table or updated and its size changes, the freed space becomes available for reuse. The freelist keeps track of this freed space and makes it available for new data by inserting it into a list of free pages. When new data is inserted into the table, it is allocated from the freelist rather than creating a new page, which helps to reduce disk fragmentation and improve performance.

The freelist for a tablespace is maintained by a set of control structures that keep track of the free pages within the tablespace. The freelist is used to manage the free space within a tablespace, and it is consulted each time a new row is inserted into the table. If there is sufficient free space within the freelist, the new row is inserted into the free space and the freelist is updated accordingly. If there is not enough free space within the freelist, a new page is allocated and added to the freelist.

The freelist is implemented as a linked list of pages, where each page is a unit of disk space that is managed by the freelist. The size of a page is determined by the block size of the underlying file system, which is usually either 4KB or 8KB. The freelist keeps track of the free pages within a tablespace and makes them available for reuse as needed.

By monitoring the freelist, you can gain insight into the efficiency of space utilization within your tablespaces and identify any performance bottlenecks related to disk space management. The pg_stat_database_blks_free view provides information about the freelist for each database in a PostgreSQL cluster.

This query returns a row for each database in your PostgreSQL cluster, with information about the freelist for each database. The columns returned by this query include:

  • datname: The name of the database.
  • blks_free: The number of free pages in the freelist.
  • blks_total: The total number of pages in the freelist.
  • percent_free: The percentage of pages in the freelist that are free, calculated as blks_free / blks_total.

By using this query, you can monitor the size of the freelist for each database in your PostgreSQL cluster and gain insight into the efficiency of space utilization within your tablespaces. You can use this information to identify any performance bottlenecks related to disk space management and take appropriate action to resolve the issue.

How to reduce Table Contention in PostgreSQL with multiple freelists?

Table Contention in PostgreSQL can be reduced by using multiple freelists. This can be accomplished by dividing a large table into smaller, separate tablespaces, each with its own freelist.

The freelist is a data structure used by PostgreSQL to manage free space within a tablespace, and it is used to keep track of free space that is available for reuse. By using multiple freelists, you can reduce the contention for the same freelist and improve the performance of your database.

Here are the steps to reduce Table Contention in PostgreSQL with multiple freelists:

  1. Create multiple tablespaces: Create multiple tablespaces in your PostgreSQL cluster and assign each tablespace to a different disk or set of disks.
  2. Divide your table: Divide your large table into smaller tables and assign each table to a different tablespace.
  3. Create indexes: Create indexes on the smaller tables to improve query performance.
  4. Monitor performance: Monitor the performance of your database to see if the multiple freelists are reducing table contention.
  5. Adjust as needed: If necessary, adjust the size of the tablespaces or the number of tablespaces to find the optimal configuration for your database.

By using multiple freelists, you can reduce the contention for the same freelist, which can help to improve the performance of your database and reduce table contention. It is important to monitor the performance of your database to ensure that the multiple freelists are providing the desired performance benefits.

About MinervaDB Corporation 36 Articles
A boutique private-label enterprise-class MySQL, MariaDB, MyRocks, PostgreSQL and ClickHouse consulting, 24*7 consultative support and remote DBA services company with core expertise in performance, scalability and high availability. Our consultants have several years of experience in architecting and building web-scale database infrastructure operations for internet properties from diversified verticals like CDN, Mobile Advertising Networks, E-Commerce, Social Media Applications, SaaS, Gaming and Digital Payment Solutions. Our globally distributed team working on multiple timezones guarantee 24*7 Consulting, Support and Remote DBA Services delivery for MySQL, MariaDB, MyRocks, PostgreSQL and ClickHouse.