Cost of initiating cursors - Mailing list pgsql-general

From Matthew Churcher
Subject Cost of initiating cursors
Date
Msg-id 799630713.60765.1365691232899.JavaMail.root@realvnc.com
Whole thread Raw
Responses Re: Cost of initiating cursors
List pgsql-general
Hi All,

I have a question in regards to the cost of initiating a cursor (for loop) over a large number of rows (100,000+) and
actuallyretrieving little or none of them.  

For example:
  FOR curr_foo
  IN
  SELECT foo FROM bar
    WHERE wibble
    ORDER BY wobble
  LOOP
     EXIT; -- always break out of loop
  END LOOP;


For some reason this is hugely expensive and slow regardless of the selected execution plan and available indexes. The
WHEREand particularly the ORDER BY clause appear to be highly significant despite having appropriate indexes in place.  

It's the combination of the following behaviours I find particular perplexing:-
  1.) Removing the WHERE and ORDER BY clauses results in a very fast query
  2.) Adding a LIMIT clause also results in a very fast query.

This is perplexing because I don't see why ORDER BY should affect the cost of opening the cursor when indexes are in
placebut since it does why would LIMIT reduce the cost of ORDER BY as PostgreSQL would still need to order all of
candidaterecords. This is all assuming the cursor isn't actually retrieving all the rows which is my understanding of
howit should work. The configuration parameter 'cursor_tuple_fraction' is having no observable effect. 

This is being seen on Postgres 9.1 (Ubuntu x64), on a server with fast disks and large amount of memory. Basic memory
tuninghas also been performed. 

Thanks in advanced, I appreciate any insights.
Kind regards,
Matthew Churcher


pgsql-general by date:

Previous
From: Joshua Berry
Date:
Subject: Re: Update
Next
From: David Greco
Date:
Subject: Streaming Replication 9.2