Re: Updating a specific number of rows in pl/pgsql

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
Cc: Peter Headland <pheadland(at)actuate(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Updating a specific number of rows in pl/pgsql
Date: 2009-08-11 10:55:05
Message-ID: 162867790908110355g3b62cc56u1c74092dd01bbd8e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

2009/8/11 D'Arcy J.M. Cain <darcy(at)druid(dot)net>:
> On Mon, 10 Aug 2009 17:52:36 -0700
> "Peter Headland" <pheadland(at)actuate(dot)com> wrote:
>> 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;
>
> Assuming that there is a unique identifier on queue, let's call it
> queue_id, you should be able to do something like this:
>
>  UPDATE queue SET assigned = TRUE
>  WHERE queue_id IN (SELECT queue_id
>    FROM queue
>    WHERE id = p_queue_id
>    ORDER BY rank
>    LIMIT p_number_of_items);
>

there are one fast trick
http://www.postgres.cz/index.php/PostgreSQL_SQL_Tricks#Fast_first_n_rows_removing

p.s. replace DELETE by UPDATE
regards
Pavel Stehule

> --
> D'Arcy J.M. Cain <darcy(at)druid(dot)net>         |  Democracy is three wolves
> http://www.druid.net/darcy/                |  and a sheep voting on
> +1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2009-08-11 15:10:06 Re: keepalive problem with libpg connection
Previous Message D'Arcy J.M. Cain 2009-08-11 10:25:03 Re: Updating a specific number of rows in pl/pgsql

Browse pgsql-sql by date

  From Date Subject
Next Message Jasen Betts 2009-08-11 11:45:17 Re: Determining logically unique entities across many partially complete rows where at least one column matches
Previous Message D'Arcy J.M. Cain 2009-08-11 10:25:03 Re: Updating a specific number of rows in pl/pgsql