Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com May 12 '13 at 0:04

This question came from our site for professional and enthusiast programmers.

1  
Just use explain or explain analyze on the main query. Views are merged/melted into the main query plan. –  wildplasser May 11 '13 at 17:17
    
I mean I want to see SQL, not the query plan. –  jl6 May 11 '13 at 17:18
    
I don't think you can do that. –  wildplasser May 11 '13 at 17:20
add comment

1 Answer 1

up vote 3 down vote accepted

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/or explain analyze to observe what the query does.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.