Two serial numbers in one table

From: "D'Arcy J(dot)M(dot) Cain" <darcy(at)druid(dot)net>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Two serial numbers in one table
Date: 2003-10-27 21:05:45
Message-ID: 200310271605.45432.darcy@druid.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Here is what I am trying to do. I have a table with two fields, both of which
are supposed to contain a serial number. The first one is the primary key
and is setup to default to a sequence in the normal way. The second one can
be one of any number of sequences. The sequence to use is calculated at run
time and the next sequence is manually included (creating the sequence if
necessary.) Sometimes the sequence is left null. In those cases it is
supposed to use the first (primary) field.

I suppose I can add a rule on the select but this is a huge and busy table
with an index and selects on the second field. I would prefer if I could
create the actual value value during insert. I did try this:

ALTER TABLE certificate
ALTER COLUMN card_id
SET DEFAULT CURRVAL('certificate_certificate_id_seq');

This mostly works but it has two problems. The first is that it seems klugey
and I am not sure if I can depend on it happening in the correct order. The
second problem is that there are edge cases (albeit none that I can imagine
happening in our application) that causes it to either fail or put a previous
value into the field.

Is there a better way to do what I am trying to do?

--
D'Arcy J.M. Cain <darcy(at){druid|vex}.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

Browse pgsql-hackers by date

  From Date Subject
Next Message Jan Wieck 2003-10-27 21:41:29 Re: Still a few flaws in configure's default CFLAGS selection
Previous Message Fernando Nasser 2003-10-27 20:59:36 Multiple database services and multiple versions on Red Hat Linux systems