Heavily optimized queueing and iterating tasks in postgres