Re: declared cursor uses slow plan

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kevin Neufeld <kneufeld(at)refractions(dot)net>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: declared cursor uses slow plan
Date: 2004-09-16 18:07:43
Message-ID: 29148.1095358063@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Kevin Neufeld <kneufeld(at)refractions(dot)net> writes:
> I have a relatively simple query that takes about 150ms using explain
> analyze. However, when I wrap the same query in a declared cursor
> statement, the subsequent fetch statement takes almost 30seconds. For
> some reason, the planner decided to do a nested loop left join instead
> of a hash left join. Does anyone know why the planner would choose this
> course?

Plans for cursors are optimized partly for startup speed as opposed to
total time, on the assumption that you'd rather get some of the rows
sooner so you can crunch on them.

Probably there should be a knob you can fool with to adjust the strength
of the effect, but at present I think it's hard-wired.

The real problem here of course is that the total cost of the nestloop
is being underestimated so badly (the estimate is only 5x more than the
hash join where reality is 200x more). It looks like this is mainly
because the number of matching rows from csn_waterbodies is badly
underestimated, which comes from the fact that we have no useful
statistics for geometric operators :-(. I think that the PostGIS crew
is working that problem but I have no idea how far along they are...

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Daniel Ceregatti 2004-09-16 18:10:13 Re: Article about PostgreSQL and RAID in Brazil
Previous Message Josh Berkus 2004-09-16 17:50:33 Article about PostgreSQL and RAID in Brazil