Search

View

What Is a View?

A view gives a name to a query so that other queries can use it like a table. An ordinary PostgreSQL view stores the definition, not a separate copy of the result.

Example

Assume orders(id, customer_id, status, total). A view can give several callers the same definition of a paid order.
CREATE VIEW paid_orders AS SELECT id, customer_id, total FROM orders WHERE status = 'PAID'; SELECT customer_id, SUM(total) FROM paid_orders GROUP BY customer_id;
Plain Text
복사

Why Use It?

•
Reuse a query without repeating its filters in every caller.
•
Expose a stable set of columns while keeping callers away from unnecessary implementation details.
•
Make reporting queries easier to read by naming an intermediate result.

Things to Check

A view is not automatically a performance improvement: the database still executes the underlying query. A materialized view stores results, but its refresh policy determines how current those results are.
Some simple views are updatable; complex joins and aggregates may not be. A view is also not a complete security policy by itself. Review privileges, row-level security, and the database’s view security rules before using it to restrict access.

Reference