From: Pavel Stehule <pavel.stehule@gmail.com>
To: Peter Headland <pheadland@actuate.com>
Cc: D'Arcy J.M. Cain <darcy@druid.net>
Cc: pgsql-sql@postgresql.org
Subject: Re: Updating a specific number of rows in pl/pgsql
Date: Tue, 11 Aug 2009 19:22:55 +0200
Message-ID: <162867790908111022y48e65d31kcf5a037c1b41857f@mail.gmail.com> (raw)
In-Reply-To: <71F491F5DA99604A80DE49424BF3D02B0C7292EF@exchange8.actuate.com>
References: <71F491F5DA99604A80DE49424BF3D02B0C7290CA@exchange8.actuate.com>
<20090811062503.86993bf6.darcy@druid.net>
<162867790908110355g3b62cc56u1c74092dd01bbd8e@mail.gmail.com>
<71F491F5DA99604A80DE49424BF3D02B0C7292EF@exchange8.actuate.com>
2009/8/11 Peter Headland <pheadland@actuate.com>:
>> there are one fast trick
>> http://www.postgres.cz/index.php/PostgreSQL_SQL_Tricks#Fast_first_n_rows_removing
>
> Thanks - that's a very useful page!
>
> Unfortunately, there is no single column that provides a unique id, and I am reluctant to add one (for example, using a sequence and a new index) for performance reasons.
ctid is unique system column in every table.
postgres=# create table x(a int);
CREATE TABLE
Time: 655,062 ms
postgres=# insert into x values(10);
INSERT 0 1
Time: 49,237 ms
postgres=# insert into x values(10);
INSERT 0 1
Time: 1,740 ms
postgres=# select ctid, a from x;
ctid | a
-------+----
(0,1) | 10
(0,2) | 10
(2 rows)
>
> Given that additional constraint, is my original plan using a loop to iterate over a cursor reasonable? I don't anticipate p_number_of_items being more than 20.
why not? for small number of iteration is loop over cursor good solution.
Pavel Stehule
>
> --
> Peter Headland
> Architect
> Actuate Corporation
>
>
> -----Original Message-----
> From: Pavel Stehule [mailto:pavel.stehule@gmail.com]
> Sent: Tuesday, August 11, 2009 03:55
> To: D'Arcy J.M. Cain
> Cc: Peter Headland; pgsql-sql@postgresql.org
> Subject: Re: [SQL] Updating a specific number of rows in pl/pgsql
>
> 2009/8/11 D'Arcy J.M. Cain <darcy@druid.net>:
>> On Mon, 10 Aug 2009 17:52:36 -0700
>> "Peter Headland" <pheadland@actuate.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@druid.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@postgresql.org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-sql
>>
>
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: pavel.stehule@gmail.com, pheadland@actuate.com, darcy@druid.net
Subject: Re: Updating a specific number of rows in pl/pgsql
In-Reply-To: <162867790908111022y48e65d31kcf5a037c1b41857f@mail.gmail.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