6
Query execution
client
application
driver
parsing ← system catalog
rewriting ← rules
planning ← statistics
execution ← data
query
result
PostgreSQL
Query execution is complicated. First, a query is sent from a client to the
server as text. The server parses the text, analyzing its syntax (whether
letters are formed into words, and words into commands) and semantics
(whether there are tables and other objects in the database that the query
refers to by name). To do that, the server needs data on what is actually
stored in the database. This meta-data is called the system catalog and is
stored in special tables in the same database.
A query can be rewritten (transformed). For example, a view name can be
substituted with the query text. Users can implement their own
transformations using the rule system.
SQL is a declarative language: a query defines what data to get, but not how
to get it. It is at this point when the query (already parsed and presented in
the form of a tree) is passed on to the planner, which develops an execution
plan. For example, the planner can decide whether or not to use indexes to
find the data for the query. To plan the execution efficiently, the planner
needs certain information about the tables it is going to work with, such as
the size of the tables and the distribution of data within them. Together, this
information is called statistics.
When a plan is selected, the query is executed in accordance with it, and
the result is returned to the client in its entirety.
This is convenient and simple when we're talking about just a row or two,
but it can quickly become problematic with large outputs.