Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id CE608633D46 for ; Tue, 11 Aug 2009 14:23:07 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 03188-01 for ; Tue, 11 Aug 2009 17:23:00 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-gx0-f217.google.com (mail-gx0-f217.google.com [209.85.217.217]) by mail.postgresql.org (Postfix) with ESMTP id 3133F63385A for ; Tue, 11 Aug 2009 14:22:56 -0300 (ADT) Received: by gxk17 with SMTP id 17so4639983gxk.19 for ; Tue, 11 Aug 2009 10:22:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=in6nnBVV9BLobtvAqO8zRLq0cRG1CDXb2QnrwaxrUbc=; b=nP+obt5YW9aTVgjj1EnDvFSWSk3ZzZNsp9UIwfPJmm2zTCRbxeskpkHCy2XAqgDhkL Q2mvHnZucQyyI6nT5Cc7pNfALXlKEMFWlQoK2WKIB7ARr2HN0AV9lDUVp66+ML1M8YQF Bx6/6hrxRmdLR5zIC4vmULoceKnvVQee2nreI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=GfZmmnsBxRkb7A8Lk4/S3O3GyK37HdootLR5NFGzUUGqTa4c5y8zgi0QC3G0QSQBeJ 8ZVVs/RpGs8esrMoEouL/dB8vyaMB/4esfW1rg67wHl9EWByoxlgdDaSLGCY3I5rX8yw YoYBwjQi8b38DR/SPz10nOr0/cte4BKxOYkkc= MIME-Version: 1.0 Received: by 10.150.215.10 with SMTP id n10mr11039574ybg.233.1250011375484; Tue, 11 Aug 2009 10:22:55 -0700 (PDT) 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> Date: Tue, 11 Aug 2009 19:22:55 +0200 Message-ID: <162867790908111022y48e65d31kcf5a037c1b41857f@mail.gmail.com> Subject: Re: Updating a specific number of rows in pl/pgsql From: Pavel Stehule To: Peter Headland Cc: "D'Arcy J.M. Cain" , pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200908/47 X-Sequence-Number: 32956 2009/8/11 Peter Headland : >> 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) fo= r performance reasons. ctid is unique system column in every table. postgres=3D# create table x(a int); CREATE TABLE Time: 655,062 ms postgres=3D# insert into x values(10); INSERT 0 1 Time: 49,237 ms postgres=3D# insert into x values(10); INSERT 0 1 Time: 1,740 ms postgres=3D# 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 ite= rate over a cursor reasonable? I don't anticipate p_number_of_items being m= ore 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 : >> On Mon, 10 Aug 2009 17:52:36 -0700 >> "Peter Headland" wrote: >>> I can get the rows I want to update like this: >>> >>> =C2=A0 SELECT * >>> =C2=A0 =C2=A0FROM queue >>> =C2=A0 =C2=A0WHERE id =3D p_queue_id >>> =C2=A0 =C2=A0ORDER BY rank >>> =C2=A0 =C2=A0LIMIT 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: >>> >>> =C2=A0 UPDATE queue SET assigned =3D 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: >> >> =C2=A0UPDATE queue SET assigned =3D TRUE >> =C2=A0WHERE queue_id IN (SELECT queue_id >> =C2=A0 =C2=A0FROM queue >> =C2=A0 =C2=A0WHERE id =3D p_queue_id >> =C2=A0 =C2=A0ORDER BY rank >> =C2=A0 =C2=A0LIMIT 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 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0D= emocracy is three wolves >> http://www.druid.net/darcy/ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0| =C2=A0and a sheep voting on >> +1 416 425 1212 =C2=A0 =C2=A0 (DoD#0082) =C2=A0 =C2=A0(eNTP) =C2=A0 | = =C2=A0what'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 >> >