Search code examples
sqlrubypostgresqlsequel

How can I find the columns returned by a postgresql query without running the query itself?


Given a messy postgres query (e.g. with lots of subqueries) is there a way to figure out what columns will be returned by the query without running the query itself?

If I understand correctly, Sequel's Dataset#columns method (Documentation) calls the query with a LIMIT 1 attached. That's fine for a simple query, but if subqueries are involved it seems that this approach still results in computing those subqueries.

(One approach might be to add a LIMIT 1 to every subquery, but I'm not exactly sure how to go about doing that.)

I'm using Postgres 9.2 with Sequel.

Thanks! (I know this question isn't as precisely posed as might be desirable -- please let me know what more information I can provide that might be helpful.)


Solution

  • You can do this with explain and add the option VERBOSE. Have a look here

    http://www.postgresql.org/docs/9.1/static/sql-explain.html