Virtually all your time in that query is spent on the left join. Which leads me to two questions.
1) are you creating a a index on runId, trialId, criterion, invocation;
2) why are you using group by, its been a little bit since i had to write an sql statement but since you are creating a table im inclear why you have groupby in there.
@freemo@mastodon.acm.org