Bricklen Anderson <BAnderson@xxxxxxxxxxxx> writes: > Your loops are what is causing the time spent. > eg. "actual time=0.127..17.379 rows=1154 loops=8335)" == > 8335*(17.379-0.127)/1000=>143 secs (if my math is correct). As for where the problem is, I think it's the horrid misestimate of the number of matching rows in cs_pk: >> " -> Index Scan using cs_pk on cs (cost=0.00..12.83 >> rows=2 width=144) (actual time=0.087..444.999 rows=13587 loops=1)" >> " Index Cond: (('M'::text = (cs.csypnum)::text) >> AND ((cs.csglnum)::text = ("outer".glnum)::text))" Has that table been ANALYZEd recently? How about the gl table? regards, tom lane