PostgreSQL - Get Execution Plan as query result

Post date: Mar 6, 2015 3:00:29 PM

If you want to capture the execution plan of a query, for further processing or to be able to compare it to another query plan, then it is handy to have this little snippet as a helper.

(I guess everyone and their cat have created a function like this at some point, but anyway)

CREATE OR REPLACE FUNCTION get_query_plan(text) RETURNS SETOF text AS $$

BEGIN RETURN QUERY EXECUTE format('EXPLAIN ANALYZE %s', $1);END $$ LANGUAGE plpgsql;