PostgreSQL Backend Internals Overview

This section gives an overview of the internal structure of the backend of PostgreSQL. It shows the general control and data flow within the backend from the time the backend receives a query to the time it sends the results.

The Path of a Query

A query passes through the following stages:

  1. An application program establishes a connection to the PostgreSQL server, transmits a query to the server, and receives the results sent back.

  2. The parser stage checks the query transmitted by the application program (client) for correct syntax and creates a query tree.

  3. The rewrite system takes the query tree created by the parser stage, looks for any rules (stored in the system catalogs) to apply to the query tree, and performs the transformations given in the rule bodies. The rewrite system is used to realize such features as views. Whenever a query is made against a view (that is, a virtual table), the rewrite system rewrites the user's query to a query that accesses the base tables given in the view definition instead.

  4. The planner/optimizer takes the (rewritten) query tree and creates a query plan that will be the input to the executor. This is done by first creating all possible paths leading to the same result. For example if there is an index on a relation to be scanned, there are two paths for the scan: one possibility is a simple sequential scan; the other possibility is to use the index. The cost for the execution of each plan is estimated and the cheapest plan is chosen and handed back to the executor.

  5. The executor recursively steps through the plan tree and retrieves tuples in the way represented by the plan. The executor makes use of the storage system while scanning relations, performs sorts and joins, evaluates qualifications and finally hands back the tuples derived.

The following sections cover the above steps in more detail to give a better understanding of the PostgreSQL internal control and data structures.