Re: Multi row sequence?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Filip Wuytack <fwuytack(at)fgscapital(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Multi row sequence?
Date: 2004-12-18 08:27:15
Message-ID: 20041218082715.GB10881@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Dec 17, 2004 at 11:10:12AM -0000, Filip Wuytack wrote:

> Is it possible to have a sequence (as a multirow prim key), where sequence
> (id) only increase per group of data (grp).
>
> E.g.
> +--------+----+---------+
> | grp | id | name |
> +--------+----+---------+
> | fish | 1 | lax |
> | mammal | 1 | dog |
> | mammal | 2 | cat |
> | mammal | 3 | whale |
> | bird | 1 | penguin |
> | bird | 2 | ostrich |
> +--------+----+---------+

PostgreSQL's sequences are simply number generators that return a
unique value. If you want to generate keys in the manner you
describe, then you could use a trigger to calculate what the next
id should be. You'd probably have to lock the table to ensure that
the operation works when multiple transactions are updating at the
same time.

Take a look at the "Triggers" chapter in the documentation, as well
as the "Trigger Procedures" section of the "PL/pgSQL - SQL Procedural
Language" chapter and the "Concurrency Control" chapter.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vincent Hikida 2004-12-18 08:40:41 Re: UNION with more restrictive DISTINCT
Previous Message Greg Stark 2004-12-18 07:25:43 Re: replacements for vacuum?