Sub topic 9
Checking State with the `pg_matviews` View
This chapter assumes that you know SELECT, schemas, indexes, and REFRESH MATERIALIZED VIEW. You should also know that a materialized view stores query results and can become stale until it is refreshed.
PostgreSQL provides the pg_matviews catalog view for inspecting materialized views in the current database. A catalog view is a system-provided view that reports database objects and their properties. It helps you answer questions such as:
- Does a materialized view exist in this database?
- Has it been populated?
- Does it have any indexes?
- Who owns it?
- What query defines it?
Inspecting the available materialized views
Start by listing materialized views visible in the current database:
SELECT schemaname,
matviewname,
matviewowner,
tablespace,
hasindexes,
ispopulated
FROM pg_matviews
ORDER BY schemaname, matviewname;The important columns are:
| Column | Meaning |
|---|---|
| `schemaname` | The schema containing the materialized view |
| `matviewname` | The materialized view name |
| `matviewowner` | The role that owns it |
| `tablespace` | The tablespace storing it, or `NULL` for the database default |
| `hasindexes` | `true` when at least one index exists |
| `ispopulated` | `true` when the materialized view has populated data |
| `definition` | The SQL query used to define the materialized view |
Schema qualification matters. Two schemas can contain materialized views with the same name, so use both schemaname and matviewname when identifying an object.
SELECT schemaname,
matviewname,
ispopulated,
hasindexes
FROM pg_matviews
WHERE schemaname = 'public'
AND matviewname = 'sales_daily';If this query returns no rows, the named materialized view is not visible under that schema and name in the current database. Check that you connected to the intended database and used the correct schema.
Checking whether a view is populated
A materialized view created with WITH NO DATA has a definition but no stored result rows. Its ispopulated value is false.
CREATE MATERIALIZED VIEW customer_totals AS
SELECT customer_id,
sum(total) AS lifetime_value
FROM orders
GROUP BY customer_id
WITH NO DATA;
SELECT matviewname,
ispopulated
FROM pg_matviews
WHERE matviewname = 'customer_totals';The result should show false. A query against an unpopulated materialized view fails because PostgreSQL has no stored contents to read. Populate it before using it:
REFRESH MATERIALIZED VIEW customer_totals;
SELECT matviewname,
ispopulated
FROM pg_matviews
WHERE matviewname = 'customer_totals';After the refresh succeeds, ispopulated becomes true.
Checking indexes and the definition
The hasindexes column gives a quick check for at least one index. It does not tell you the index names, columns, uniqueness, or sort order.
SELECT matviewname,
hasindexes
FROM pg_matviews
WHERE schemaname = 'public';To inspect the actual indexes, use pg_indexes:
SELECT indexname,
indexdef
FROM pg_indexes
WHERE schemaname = 'public'
AND tablename = 'sales_daily';The definition column shows the query that PostgreSQL stores for the materialized view:
SELECT matviewname,
definition
FROM pg_matviews
WHERE schemaname = 'public'
AND matviewname = 'sales_daily';This is useful when a view was created by another team or deployment. It also helps confirm which joins, filters, and grouping rules the refresh operation uses.
Understanding what `pg_matviews` cannot tell you
pg_matviews does not include a built-in “last refreshed at” timestamp. It also does not compare the stored rows with the current base tables. Therefore, it cannot directly tell you how stale a materialized view is.
Track refresh history separately if freshness matters. For example, a scheduled job can record the start time, finish time, success status, and row counts in an operational table. Then combine that record with ispopulated and the materialized view definition when monitoring the object.
flowchart TD
A[Query pg_matviews] --> B{ispopulated?}
B -->|No| C[Run initial refresh]
B -->|Yes| D{Freshness record available?}
D -->|No| E[Cannot measure staleness from pg_matviews alone]
D -->|Yes| F[Compare refresh time with freshness target]
F --> G{hasindexes?}
G -->|No| H[Review read performance and concurrent refresh needs]
G -->|Yes| I[Inspect index definitions]Exercise
A materialized view has ispopulated = true, but its dashboard shows yesterday's totals. Does pg_matviews indicate that the view is broken?
Show answer
No. ispopulated = true only means that stored contents exist. The view may be stale because its base tables changed after the last refresh. Track refresh times separately to measure freshness.