Updating a specific number of rows in pl/pgsql

From: "Peter Headland" <pheadland(at)actuate(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Updating a specific number of rows in pl/pgsql
Date: 2009-08-11 00:52:36
Message-ID: 71F491F5DA99604A80DE49424BF3D02B0C7290CA@exchange8.actuate.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

I'm working on a queuing application. As part of this I'm trying to
write a pl/pgsql function that updates a specific number of rows in the
most efficient way possible. Multiple queues are contained within a
single table.

I can get the rows I want to update like this:

SELECT *
FROM queue
WHERE id = p_queue_id
ORDER BY rank
LIMIT p_number_of_items;

Of course, there may not be p_number_of_items available in the queue.

I want to update all the rows in the cursor in the same way:

UPDATE queue SET assigned = TRUE;

The "obvious" solution is to get a cursor on the query and attempt to
MOVE through that cursor in a loop, using the row count from the SELECT
to tell me when I am done. I can then use UPDATE ... WHERE CURRENT OF
... to do the updates. This seems cumbersome and inefficient to me. Is
there a better way?

Ideally, I'd like to do something like:

UPDATE (SELECT ... ) ...

--
Peter Headland
Architect
Actuate Corporation

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ow Mun Heng 2009-08-11 02:15:48 xlog flus not satisfied
Previous Message Shoaib Mir 2009-08-10 23:41:27 Re: NOTICE: there is no transaction in progress

Browse pgsql-sql by date

  From Date Subject
Next Message Premila Devi 2009-08-11 03:54:32 Import (.CVS File) to postgreSql
Previous Message John 2009-08-07 13:28:28 Re: two records per row from query