Hope this helps:
CREATE OR REPLACE FUNCTION explain(in_sql text) RETURNS TABLE(explain_line
text) AS $explain$
BEGIN
RETURN QUERY EXECUTE 'EXPLAIN '||in_sql;
END;
$explain$ LANGUAGE plpgsql;
SELECT * FROM explain('SELECT * FROM pg_locks');
--
Victor Y. Yegorov