pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vik Fearing <vik@postgresfriends.org>
To: Søren Frisk <soeren.frisk@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: Invoice increment
Date: Wed, 26 Feb 2020 10:27:21 +0100
Message-ID: <dba07b2f-7c81-a522-f03f-9f46e6127b78@postgresfriends.org> (raw)
In-Reply-To: <CACe3Kr6jhD4BKv74bs7f3K6FDq7KM_QvCy3s35Dm8gfi5By9Xg@mail.gmail.com>
References: <CACe3Kr6jhD4BKv74bs7f3K6FDq7KM_QvCy3s35Dm8gfi5By9Xg@mail.gmail.com>

On 26/02/2020 09:38, Søren Frisk wrote:
> Hi all
> 
> I'm trying to find a way to increment an invoice number. And i think it
> would be pretty straight forward just using a SERIAL int. But as we're
> selling across multiple countries, i need the invoice increment to be reset
> by a country_id. any good way to achieve this in a dynamic way?

A serial column is absolutely not a good fit for invoice numbers because
in some (most? all?) countries, the numbers are not allowed to have gaps.

The way I would do it is have an invoice_numbers table like this:

CREATE TABLE invoice_numbers (
    country text PRIMARY KEY REFERENCES countries,
    invoice_number bigint NOT NULL
);

And then you can get the next number with something like this:

WITH
u (invoice_number) AS (
    UPDATE invoice_numbers
    SET invoice_number = invoice_number + 1
    WHERE country = $1
    RETURNING invoice_number
),
i (invoice_number) AS (
    INSERT INTO invoice_numbers (country, invoice_number)
    SELECT $1, 1
    WHERE NOT EXISTS (TABLE u)
    ON CONFLICT (country) DO
        UPDATE SET invoice_number = invoice_numbers.invoice_number + 1
    RETURNING invoice_number
)
TABLE u UNION ALL TABLE i;

You can just put that in an SQL function for convenience.

> Hope this is the right place to ask.

Yes, it is.
-- 
Vik Fearing





view thread (6+ messages)  latest in thread

Message-ID: <dba07b2f-7c81-a522-f03f-9f46e6127b78@postgresfriends.org>
Permalink:  ../dba07b2f-7c81-a522-f03f-9f46e6127b78@postgresfriends.org/
Also on:    postgresql.org/message-id/dba07b2f-7c81-a522-f03f-9f46e6127b78@postgresfriends.org

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: vik@postgresfriends.org, soeren.frisk@gmail.com
  Subject: Re: Invoice increment
  In-Reply-To: <dba07b2f-7c81-a522-f03f-9f46e6127b78@postgresfriends.org>
  References: <CACe3Kr6jhD4BKv74bs7f3K6FDq7KM_QvCy3s35Dm8gfi5By9Xg@mail.gmail.com> <dba07b2f-7c81-a522-f03f-9f46e6127b78@postgresfriends.org>

* 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