X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AE1703A54E6 for ; Mon, 24 Jan 2005 19:13:43 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 86710-07 for ; Mon, 24 Jan 2005 19:13:33 +0000 (GMT) Received: from gpmail.gphq.genpathpharma.com (67.109.105.227.ptr.us.xo.net [67.109.105.227]) by svr1.postgresql.org (Postfix) with ESMTP id C8F8D3A5515 for ; Mon, 24 Jan 2005 19:13:32 +0000 (GMT) x-mimeole: Produced By Microsoft Exchange V6.0.6487.1 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Subject: Value specific sequences? Date: Mon, 24 Jan 2005 14:10:51 -0500 Message-ID: <7A4ADADFC8AFF0478D47F63BEDD57CE30D0A0D@gpmail.gphq.genpathpharma.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Value specific sequences? Thread-Index: AcUCSGtZFCif4+N0SvOVoQiT6z3sGw== From: "Dmitri Bichko" To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200501/213 X-Sequence-Number: 20001 Hello everyone, I have a table of entities, each entity has a parent_id, I'd like to have an insert trigger that assigns to that entity a sequential number which gets incremented per parent_id. i.e. doing: INSERT INTO foo(id, parent_id) VALUES('a',1); INSERT INTO foo(id, parent_id) VALUES('b',1); INSERT INTO foo(id, parent_id) VALUES('c',2); Should result in 'foo' containing: id parent_id parent_index 'a' 1 0 'b' 1 1 'c' 2 0 The two ways that come to mind are quering 'foo' on every insert for the largest index for that parent_id (which seems slow) and using a separate table of counters (which seems breaky). Performance wise: the number of rows in 'foo' is not limited (ie will grow continuously over the life of the app), but each parent_id will usually only have about 10 foo's associated with it. To make things simple, nothing can be deleted from foo, and once inserted the relevant values cannot change. Is there an elegant way to do this? Your help greatly appreciated, Dmitri