From: Dmitri Bichko <dbichko@genpathpharma.com>
To: pgsql-sql@postgresql.org
Subject: Value specific sequences?
Date: Mon, 24 Jan 2005 14:10:51 -0500
Message-ID: <7A4ADADFC8AFF0478D47F63BEDD57CE30D0A0D@gpmail.gphq.genpathpharma.com> (raw)
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
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: dbichko@genpathpharma.com
Subject: Re: Value specific sequences?
In-Reply-To: <7A4ADADFC8AFF0478D47F63BEDD57CE30D0A0D@gpmail.gphq.genpathpharma.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