Re: MOVE cursor in plpgsql?

From: Tomas Vondra <tv(at)fuzzy(dot)cz>
To: Webb Sprague <webb(dot)sprague(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: MOVE cursor in plpgsql?
Date: 2007-02-11 15:44:37
Message-ID: 45CF39E5.9010509@fuzzy.cz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Hi all,
>
> Is there a way to move a cursor in plpgsql in the same way as in
> regular sql? The function below would like to move the cursor back to
> the start each time the cursor runs out of rows, creating pairs of
> integers that are randomly put together.
>
> The "motivation" for this is to randomly assign parts of a
> shakespearian play (N=25) to not enough actors (N=6), giving each
> actor several parts. (To be truly fair, I would have to weight by
> number of lines, but that is for version 2... ) If there is a more
> graceful way to solve the problem, I am interested, but I would like
> to know about the MOVE issue in any case.

Wouldn't it be easier to list the parts in a random order (simply ORDER
BY RANDOM()) and then use modulo by number of actors (but there's no
ROWNUM so a loop is needed anyway). Something like

i := 0;

FOR x IN SELECT .... parts ... LOOP
actor := mod(i,number_of_actors);
i := i + 1;
END LOOP;

This should be fair enough and does not need moving the cursors in
various areas.

t.v.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Arturo Perez 2007-02-11 15:53:39 Re: Adding TEXT columns tanks performance?
Previous Message Arturo Perez 2007-02-11 15:16:52 Re: Adding TEXT columns tanks performance?