X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id F04C6D1D953 for ; Mon, 24 Nov 2003 15:13:08 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 09022-04 for ; Mon, 24 Nov 2003 11:12:40 -0400 (AST) Received: from ll.mit.edu (LLMAIL.LL.MIT.EDU [129.55.12.40]) by svr1.postgresql.org (Postfix) with ESMTP id E2323D1D999 for ; Mon, 24 Nov 2003 11:12:35 -0400 (AST) Received: (from smtp@localhost) by ll.mit.edu (8.12.10/8.8.8) id hAOFCcUZ013431 for ; Mon, 24 Nov 2003 10:12:38 -0500 (EST) Received: from sty.llan.ll.mit.edu( ), claiming to be "sty.llan" via SMTP by llpost, id smtpdAAAFOaa9z; Mon Nov 24 10:12:33 2003 Date: Mon, 24 Nov 2003 10:12:33 -0500 From: george young To: pgsql-sql@postgresql.org Subject: Re: increment int value in subset of rows? Message-Id: <20031124101233.454030d8.gry@ll.mit.edu> In-Reply-To: <001a01c3b26c$633e1150$8e8bbd3e@rwanet.co.uk> References: <20031123205914.45e19925.gry@ll.mit.edu> <001a01c3b26c$633e1150$8e8bbd3e@rwanet.co.uk> Reply-To: gry@ll.mit.edu Organization: MIT Lincoln Laboratory X-Mailer: Sylpheed version 0.9.7 (GTK+ 1.2.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/276 X-Sequence-Number: 16091 On Mon, 24 Nov 2003 09:21:39 -0000 "Matthew Lunnon" threw this fish to the penguins: > You could write a function to do it. > > Matthew That would save me the external interaction, but still amount to ~1000 sql queries -- I'm hoping to find something O(0), i.e. a few queries regardless of the number of rows... > ----- Original Message ----- > From: george young > To: pgsql-sql@postgresql.org > Sent: Monday, November 24, 2003 1:59 AM > Subject: [SQL] increment int value in subset of rows? > > > [postgresql 7.4, SuSE x86 linux] > I have a table "rtest" with primary key (run,seq) and other data. For a given value > of "run", seq is a sequential run of integers, 1,2,3,4.. Now I want to > insert a row into this "sequence", say run='foo', seq=2, adjusting the seq up for > all subsequent foo rows. My first thought > was just: > update rtest set seq=seq+1 where run='foo' and seq>1; > which gets: > ERROR: Cannot insert a duplicate key into unique index rtest_pkey > no surprise :-(. > > This doesn't work, since the *order* of execution of these updates > is not guaranteed, and I actually would need to start with the highest > value of seq and work down. There may be a thousand or so rows for 'foo' > run, so an external loop of queries would be very expensive. > How can I increment all the seq values for foo columns where seq > something? > > create table rtest(run text,seq int,data int,primary key (run,seq)); > insert into rtest values('foo',1,11); > insert into rtest values('foo',2,22); > insert into rtest values('foo',3,33); > insert into rtest values('foo',4,44); > insert into rtest values('bar',1,99); > > I want to shift all foo rows and insert a new one so that: > select * from rtest where run='foo' order by seq; > would get: > > run | seq | data > -----+-----+------ > foo | 1 | 11 > foo | 2 | 999 > foo | 3 | 22 > foo | 4 | 33 > foo | 5 | 44 -- I cannot think why the whole bed of the ocean is not one solid mass of oysters, so prolific they seem. Ah, I am wandering! Strange how the brain controls the brain! -- Sherlock Holmes in "The Dying Detective"