Guido Niewerth <gniewerth(at)ocsgmbh(dot)com> writes:
> As you can see theres a huge runtime difference between the select query used in the trigger function and the one run from the SQL editor.
contrib/auto_explain might be useful in seeing what's going on, in
particular it would tell us whether or not a different plan is being
used for the query inside the function.
regards, tom lane