pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Headland <pheadland@actuate.com>
To: pgsql-sql@postgresql.org
Subject: Updating a specific number of rows in pl/pgsql
Date: Mon, 10 Aug 2009 17:52:36 -0700
Message-ID: <71F491F5DA99604A80DE49424BF3D02B0C7290CA@exchange8.actuate.com> (raw)

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

view thread (8+ messages)  latest in thread

Message-ID: <71F491F5DA99604A80DE49424BF3D02B0C7290CA@exchange8.actuate.com>
Permalink:  ../71F491F5DA99604A80DE49424BF3D02B0C7290CA@exchange8.actuate.com/
Also on:    postgresql.org/message-id/71F491F5DA99604A80DE49424BF3D02B0C7290CA@exchange8.actuate.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: pheadland@actuate.com
  Subject: Re: Updating a specific number of rows in pl/pgsql
  In-Reply-To: <71F491F5DA99604A80DE49424BF3D02B0C7290CA@exchange8.actuate.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox