declare
exp text;
begin
for exp in explain myquery
loop
raise notice '%', exp;
end loop;
end;
If you use json format output, however, the result comes back as a single json document rather than a set of lines. You still might need a loop - I haven't found another way yet of getting the output from EXPLAIN into a variable - but the loop will only have one iteration. Here is an example taken from a function I wrote the other day that lets you get the estimated number of rows from the plan:
declare
exp json;
begin
for exp in explain (format json) myquery
loop
raise notice 'rows: %', exp#>>'{0,Plan,Plan Rows}';
end loop;
end;