I've managed to determine that there's a trigger dependency tree that slows things down. When I DISABLE one of them the function takes under 30 seconds, still more than from pgAdmin but much quicker than before.
I am with David. I am not sure you are calling the same function in each case.
Are you sure you have not overloaded the function name and a difference in search_paths is not causing a different version to be run?