It sounds like you are wanting to run 'explain analyze [query]' inside a loop inside a DO block. That isn't possible as far as I know, but auto_explain and log_nested_statements should be able to let you profile the whole thing and perhaps you can pick out the part you want from the logs.
- EXPLAIN with anonymous DO block? Ron
- Re: EXPLAIN with anonymous DO block? Michael Lewis
- Re: EXPLAIN with anonymous DO block? David G. Johnston
- Re: EXPLAIN with anonymous DO block? Tom Lane
- Re: EXPLAIN with anonymous DO block? Pavel Luzanov