Views

Views

Views are persistent "fake" tables that have rows from other tables. These can be exact rows as they exists on another table, or a computed value.

They can be referenced in any place where you would normally reference a table.

circle-exclamation
triangle-exclamation

Creating

CREATE VIEW view_name AS (
    -- In here, any kind of SELECT query
    SELECT * FROM somewhere
);

Updating

CREATE OR REPLACE VIEW view_name AS (
    -- In here, any kind of SELECT query
    SELECT * FROM somewhere
);

Deleting

Materialized Views

With Materialized Views, the query gets executed only at specific times, but the results are saved and can be referenced without rerunning the query.

circle-info

The ideia is that it will not garantee data integrity, since it can hold old values, until its data is refreshed.

circle-info

Materialized Views work as regular tables, so you can add indexes, primary keys, and etc.

triangle-exclamation

Creating

WITH DATA will load data into the materialized view at the creation time. (Will lock the source tables for as long as it takes to fully populate the view)

Refreshing data

triangle-exclamation
triangle-exclamation

concurrently

CONCURRENTLY allows for the update of the materialized view without locking it. (Postgres creates a temporary updated version of the materialized view, compares the two versions, and performs INSERT and UPDATE only on the differences)

triangle-exclamation

Deleting

Last updated