If I have a view on a view on a view (etc.), is there a way to show the full query that this expands/flattens to?
Postgresql – How to view the full, flattened query underlying a Postgresql view
postgresql
Related Question
- Postgresql – Full Join Query
- PostgreSQL – Get Underlying Foreign Key Constraints from a View
- Sql-server – Query Speed CTE Or View
- Postgresql – View the final query executed by PostgreSQL Function
- PostgreSQL Materialized View not refreshing
- Postgresql – Does CREATE/REFRESH MATERIALIZED VIEW in PostgreSQL use the parallel planner
- Postgresql – Improving the queries in a view to query custom types
- PostgreSQL – Expanding View Multiple Times Instead of Executing Once
Best Answer
Not in PostgreSQL at this time. PostgreSQL doesn't actually build a single giant SQL statement; instead, it uses the plan tree of the view(s) and inserts that into the top level query's plan tree. To turn that back into SQL would require a deparser, something PostgreSQL doesn't have a the moment.
You can kind-of do-it-yourself by replacing references to the view with the text of the view query, but you need to understand that the effect isn't exactly the same, particularly where there are multiple references to the view.
You're best off using
explain
and/orexplain analyze
to observe what the query does.