How Many Access Paths are Available to the PostgreSQL Optimizer?

In PostgreSQL, the query optimizer has several access paths at its disposal to determine the most efficient way to access and retrieve data from tables or indexes. The choice of access path depends on factors such as table size, index availability, query complexity, and statistics. Here are some of the common access paths available to the PostgreSQL optimizer:

  1. Sequential Scan:
    • A sequential scan is the simplest access path where the optimizer reads all the rows in a table sequentially.
    • This method is efficient when retrieving a large portion of the table’s data or when an index is not available or not selective enough.
  2. Index Scan:
    • An index scan involves utilizing an index structure to locate and retrieve specific rows based on the query’s filter conditions.
    • PostgreSQL supports different types of index scans, including B-tree, Hash, GiST, GIN, and SP-GiST, each optimized for specific data types and query patterns.
    • Index scans are efficient when the query’s filter conditions match the indexed columns and provide good selectivity.
  3. Bitmap Index Scan:
    • A bitmap index scan combines multiple index scans using bitmap operations such as AND, OR, and NOT.
    • This method is useful when combining multiple conditions or when there are multiple indexes available, allowing efficient filtering and combination of results.
  4. Bitmap Heap Scan:
    • A bitmap heap scan is a combination of a bitmap index scan and a heap scan.
    • It leverages a bitmap index scan to identify the relevant rows and retrieves them from the underlying table using a heap scan.
    • This access path is particularly useful when joining multiple tables or applying complex conditions involving bitmap indexes.
  5. TID Scan:
    • A TID scan allows direct access to rows based on their physical tuple identifiers (TIDs).
    • It is used when the optimizer knows the specific TIDs of the rows to be retrieved, such as when referencing them from a subquery or an intermediate result set.
  6. Foreign Table Access:
    • PostgreSQL supports accessing data from foreign tables using foreign data wrappers (FDWs).
    • FDWs provide access to remote databases or external data sources, allowing queries to include data from multiple sources.

The PostgreSQL optimizer evaluates various factors such as data distribution, query complexity, available indexes, and statistics to select the most efficient access path for a given query. It aims to minimize I/O operations, CPU utilization, and overall query execution time. Additionally, the optimizer may consider combining multiple access paths or applying join methods to optimize complex queries.

It’s important to note that the optimizer’s choice of access path is based on statistics and cost estimation, and it may vary depending on the specific scenario. Regularly updating statistics and monitoring query performance can help identify opportunities for optimization and fine-tuning of access paths in PostgreSQL.

About Shiv Iyer 455 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.