Another thought...
Alex Paulusberger <alexp@meta-bit.com> writes:
> The whole process loops 4,500 times.
> For every loop
> - a temp table is cleared
How exactly are you clearing the temp table? DELETE FROM isn't a good
plan because you'll still have dead tuples in there. You could do a
DELETE FROM and then VACUUM, but I'd suggest TRUNCATE instead.
regards, tom lane