Sub topic 13
Finding and inspecting views in the catalog
This chapter assumes you can write basic SELECT statements, joins, and filters. It also assumes you understand that database users receive privileges on database objects.
A view is a named query. The database records information about each view in metadata tables called a system catalog. Catalog queries help you answer questions such as:
- Which views exist in a schema?
- What columns does a view expose?
- What query defines the view?
- Can users insert into or update the view?
- Which tables or views does it depend on?
Listing views with `information_schema`
information_schema is a set of standard views that expose database metadata in a portable form. The exact columns and supported features vary by database system, but common queries work across many systems.
SELECT table_schema,
table_name,
check_option,
is_updatable,
is_insertable_into
FROM information_schema.views
WHERE table_schema = 'public'
ORDER BY table_name;The result describes views in the public schema. is_updatable indicates whether the database considers the view capable of accepting updates. is_insertable_into indicates whether inserts are possible through the view. These values describe database rules, not whether a particular user has permission to write.
The check_option column reports whether the view uses WITH CHECK OPTION.
Inspecting a view's columns
The information_schema.columns view lists columns for tables and views. Query it when you need to confirm a view's public shape.
SELECT ordinal_position,
column_name,
data_type,
is_nullable
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'employee_directory'
ORDER BY ordinal_position;ordinal_position shows the column's order. This matters when a view is used by applications that expect a stable result shape. Check the column names and types after changing a view definition.
The following query lists all views and their definitions:
SELECT table_schema,
table_name,
view_definition
FROM information_schema.views
WHERE table_schema NOT IN ('information_schema', 'pg_catalog')
ORDER BY table_schema, table_name;The definition may be normalized by the database. For example, the database might add parentheses, qualify names with schemas, or change capitalization. Compare the meaning of the query rather than relying on its exact formatting.
Database-specific catalogs and dependencies
Standard metadata is useful for portability, but database-specific catalogs usually provide more detail. In PostgreSQL, pg_catalog.pg_views contains view definitions:
SELECT schemaname,
viewname,
definition
FROM pg_catalog.pg_views
WHERE schemaname = 'public'
ORDER BY viewname;PostgreSQL stores regular views and materialized views as different relation types. This query lists both:
SELECT n.nspname AS schema_name,
c.relname AS object_name,
CASE c.relkind
WHEN 'v' THEN 'view'
WHEN 'm' THEN 'materialized view'
END AS object_type
FROM pg_catalog.pg_class AS c
JOIN pg_catalog.pg_namespace AS n
ON n.oid = c.relnamespace
WHERE c.relkind IN ('v', 'm')
ORDER BY n.nspname, c.relname;View dependencies matter before you rename or drop an underlying table. PostgreSQL records these relationships in pg_depend:
SELECT v.oid::regclass AS view_name,
d.refobjid::regclass AS referenced_object
FROM pg_catalog.pg_depend AS d
JOIN pg_catalog.pg_rewrite AS r
ON r.oid = d.objid
JOIN pg_catalog.pg_class AS v
ON v.oid = r.ev_class
WHERE v.relkind = 'v'
AND d.refclassid = 'pg_catalog.pg_class'::regclass
AND d.deptype = 'n'
ORDER BY view_name, referenced_object;This query is PostgreSQL-specific. Other systems provide different catalog tables or dependency commands.
A practical inspection workflow
Use this sequence before modifying a view:
- Find the view and confirm its schema.
- Record its exposed columns and data types.
- Read its definition.
- Inspect dependent objects and referenced tables.
- Check privileges and application usage.
- Test the change in a development database.
Exercise
Which catalog query lists the columns and their order for the view public.employee_directory?
Show answer
Query informationschema.columns, filter tableschema = 'public' and tablename = 'employeedirectory', select ordinalposition and columnname, and order by ordinal_position.