Re: How to create "auto-increment" field WITHOUT a sequence object?

From: Chris Travers <chris(dot)travers(at)gmail(dot)com>
To: Ireneusz Pluta <ipluta(at)wp(dot)pl>
Cc: Dmitry Koterov <dmitry(dot)koterov(at)gmail(dot)com>, Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to create "auto-increment" field WITHOUT a sequence object?
Date: 2011-07-03 14:47:01
Message-ID: CAKt_Zfu4padtk21KWKM8cPgnnf9KU621YVP-9+Mkws0zKoKhcg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Jul 3, 2011 at 7:25 AM, Ireneusz Pluta <ipluta(at)wp(dot)pl> wrote:

> You may use dense_rank() (or even rank()) window function to map your
> sequence-with-gaps to a no-gap-id which will be used for exports.
>

The typical case where gapless numbering comes up is something like this:

In Greece, you go get invoice paper from the tax office which is
numbered in sequence and the government gets a list of the invoice
forms you have purchased. You then print the invoices on those paper
forms, and must number the invoices sequentially and without gaps. In
the case of an audit, all paper forms obtained must be accounted for
as must all gaps in numbering. You MUST be able to connect each
sequential invoice number (internally generated) to each invoice form
(numbered at the tax office).

In this case you really have no choice but to lock some records,
generate a new gapless id, and save/print it. Naturally this causes
the sorts of problems mentioned.

Best Wishes,
Chris Travers

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daron Ryan 2011-07-03 15:13:50 Select from Java Strings
Previous Message Ireneusz Pluta 2011-07-03 14:25:43 Re: How to create "auto-increment" field WITHOUT a sequence object?