From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Using a single sequence for all tables
Date: Wed, 29 Sep 2021 12:18:47 +0200
Message-ID: <dc6a27272a280b2ae4358681ca8854dc8661c540.camel@cybertec.at> (raw)
In-Reply-To: <YVQxL6YWXxhQwcft@hjp.at>
References: <YVQxL6YWXxhQwcft@hjp.at>
On Wed, 2021-09-29 at 11:26 +0200, Peter J. Holzer wrote:
> I discovered this technique back in my Oracle days but it dropped out of
> my toolbox when I switched to PostgreSQL. Recently I had reason to
> revisit it, so I thought I should share it (trivial though it is).
>
> So the solution is to use a single sequence for all the id columns.
>
> Possible drawbacks:
>
> * The ids will grow faster, and they will be large even on small
> tables. It may be a bit irritating if you have a table with just 5
> rows and the ids are 5, 6, 7, 12654, 345953.
That's why use use "bigint".
> * Bottleneck? Using a single sequence was said to be a performance
> bottleneck in Oracle. I didn't notice a performance difference then
> and I doubt it would be one in PostgreSQL, but if in doubt, measure!
That's no problem if you define the sequence with a CACHE value
above 1, so that not every "nextval" call hits the sequence.
> * Doesn't work with IDENTIY - those columns always use implicit
> sequences.
Right.
> * currval() is pretty useless with a global sequence. But I basically
> never use that anyway.
Same here.
I think identity columns are a Good Thing, particularly when CREATED ALWAYS,
and I don't see the advantage of a database-wide unique identifier.
But if it gives you a warm fuzzy feeling, go fot it :^)
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
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: laurenz.albe@cybertec.at, hjp-pgsql@hjp.at
Subject: Re: Using a single sequence for all tables
In-Reply-To: <dc6a27272a280b2ae4358681ca8854dc8661c540.camel@cybertec.at>
References: <YVQxL6YWXxhQwcft@hjp.at> <dc6a27272a280b2ae4358681ca8854dc8661c540.camel@cybertec.at>
* 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