Database query processing 2026-11-05
Database query processing is the sequence that turns a database query into returned results. In a typical SQL system, it includes parsing the text, checking names and types, rewriting the query when appropriate, database query optimization, and executing the chosen database query plan. Planning chooses operations; execution actually performs storage accesses, relational joins, filtering and aggregation.[ref]
For a request for each customer's total order value, the engine might scan orders, group their database tuples by customer identifier, sum the values and return one result per customer. An alternative plan can first restrict the orders using a database index. The result specification alone does not prescribe which access method performs the work.
A query against a stored database view illustrates why processing has more than a text-to-loop step. PostgreSQL can rewrite the reference into a query on the underlying tables before planning. The executor then walks the resulting plan and obtains rows from its operations. The distinction is useful when diagnosing a slow query: excessive planning work, a poor plan, and unexpectedly expensive execution are different causes even if the same statement triggered them.[ref]
Database view 2026-11-05
A database view is a named database query that can be used much like a database relation. An ordinary view stores the query definition rather than a separate persistent copy of its result. Referring to the view evaluates that definition as part of the surrounding database query; changes in the underlying data can therefore change the view's result.[ref]
For example, an orders table might contain identifiers, totals and payment flags. The definition CREATE VIEW unpaid_orders AS SELECT id, total FROM orders WHERE paid = false; exposes the unpaid orders without requiring every application query to repeat the condition. If order 17 is marked paid, the next query of the ordinary view no longer includes that order, subject to the query's database transaction snapshot. Creating the view did not create another independent copy of order 17.
A view can hide storage and schema details behind a stable interface, supporting data independence. A materialized view instead stores results that require a refresh policy. Some ordinary views permit updates, but a view containing aggregation or certain joins may not identify a unique underlying row to change. Update behavior and access control are separate specifications; the word view alone does not guarantee that an arbitrary definition is writable or supplies a security boundary.[ref]
Materialized view 2026-11-05
A materialized view stores the result of a defining database query, allowing later queries to read the stored result. An ordinary database view retains the definition and evaluates it against the relevant underlying data when queried. A materialized view trades storage and maintenance work for potentially cheaper repeated reads; its freshness depends on when and how the result is maintained.[ref]
Suppose an orders table contains totals 10 and 20 for one customer. A materialized view defined by SELECT customer_id, SUM(total) AS total FROM orders GROUP BY customer_id stores 30 for that customer. Inserting a third order worth 7 changes the underlying sum to 37. In PostgreSQL, the previously stored 30 remains until a refresh replaces the result, for example with REFRESH MATERIALIZED VIEW customer_totals;. The stored answer can therefore be internally consistent yet out of date.
A dashboard that tolerates nightly updates can benefit from this arrangement, especially when the defining database query combines many rows. An application requiring the latest committed totals needs a suitable maintenance policy and database transaction semantics. The term materialized view alone does not promise automatic updates or an always faster query.[ref]