Just Code‎ > ‎

PostgreSQL - Get Execution Plan as query result

posted Mar 6, 2015, 7:00 AM by Peter Henell   [ updated Mar 6, 2015, 7:00 AM ]
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;
Comments