Search code examples
sqlpostgresqlmaterialized-views

How to get materialized views that refer to a table in postgresql


In postgresql it is possible to get all views that refer to a table by simple sql thanks to information_schema.views table. But I need an equivalent sql to get materialized views that refer to the table.

For views following works

SELECT * 
FROM information_schema.view_table_usages AS u
JOIN information_schema.views AS v on u.view_schema = v.table_schema AND u.view_name = v.table_name
WHERE u.table_name = 'my_table'

Solution

  • Closest I can come up with:

    SELECT * FROM pg_matviews WHERE definition ilike '%my_table%' ;
    
    

    From here:

    https://www.postgresql.org/docs/current/view-pg-matviews.html