Moving within a cursor declared in plpgsql function - Mailing list pgsql-general

From Sven Willenberger
Subject Moving within a cursor declared in plpgsql function
Date
Msg-id 1106951339.7258.20.camel@lanshark.dmv.com
Whole thread Raw
List pgsql-general
I have searched the web/archives for a solution to this and have found a
few entries to my question but nothing definitive. In essence I have a
function that opens a cursor (refcursor). It would appear though that
one can only traverse the cursor forward-only and only one record at a
time; is there any way (or has the ability been added in 8.0) to open
the cursor, fetch into variable until NOT FOUND and then reposition the
pointer to the beginning of the cursor?

In essence what I am doing is a type of best match algorithm; a key
(firstkey) of length x  and owner y and rating (firstrating) represents
the first element. A best fit is considered the  key  (newkey) of
shortest length belonging to a different owner such that newkey and
substring(firstkey,1,length(newkey) match and newrating is better than
firstrating.

The initial pre-match table contains all the owners, keys, and ratings
and this is loaded in as a cursor in order of key desc (key is an
integer so naturally sorts from longest to shortest here) and value.
Right now, I have to reload the cursor for each iteration through the
table, offsetting by the iteration:

DECLARE
    curs CURSOR (key integer) FOR SELECT * FROM foo ORDER BY key desc,
rating offset key;

Ideally I could see saving tons of time if I could simply:
FETCH ABSOLUTE iteration FROM curs
for each iteration without having to use offsets, etc.

Any ideas on this or on a better way of trying this?

Sven


pgsql-general by date:

Previous
From: "Ed L."
Date:
Subject: rowset-returning function mismatch
Next
From: Steve Crawford
Date:
Subject: Mail list / web issues