1.Standard Views and Their Benefits/Limitations
A view is a stored, named SELECT query — querying the view re-runs the underlying query every time, always returning current data. It stores no data of its own, only the query definition, similar to a CTE that persists across statements rather than being scoped to one.
Key features
- A view stores only its defining query, not any data — every SELECT against it re-runs that query
- A view always reflects the current state of its underlying tables, with no staleness
- A simple view (one table, no aggregation) can often be inserted/updated/deleted through directly, subject to PostgreSQL's updatable-view rules
- A view built on an expensive query (heavy joins, aggregation) pays that full cost on every read, since nothing is cached
- A view can hide implementation complexity behind a simple name, but can also hide that complexity from someone estimating a query's cost
View vs the query it wraps
- SELECT * FROM active_customers — a query against the view
- leads to View definition substituted
- View definition substituted — stored SELECT swapped in
- leads to Full query executed
- Full query executed — against the real, current data
Command
CREATE VIEW active_customers AS
SELECT id, name FROM customers WHERE deleted_at IS NULL;
SELECT * FROM active_customers WHERE name = 'Ada';A view stores only its defining query, not any data — it always reflects current data with no refresh needed, but pays the full cost of its underlying query on every single read.