speeding up a deletion in postgresql
The easiest optimization would probably be to let the planner use a hash anti join, by rewriting the query as:

 delete from taskflag where not exists
  (select 1 from idtemp where documentid=taskflag.documentid);


also you may need to ANALYZE the temp table immediately after populating it.