Re: looking for a globally unique row ID

From: Karl Czajkowski <karlcz(at)isi(dot)edu>
To: Rafal Pietrak <rafal(at)ztk-rp(dot)eu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: looking for a globally unique row ID
Date: 2017-09-14 21:21:12
Message-ID: 20170914212112.GC10289@moraine.isi.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sep 14, Rafal Pietrak modulated:

> My main objective is the "guarantee". Which I've tried to indicate
> referring to "future UPDATEs".
>

With a well-behaved application, it is sufficient to define each
ID column as:

id int8 UNIQUE NOT NULL DEFAULT nextval('sharedsequence')

and ensure that you always use defaults for INSERT and never
set the column during UPDATE.

You can also use a BEFORE/FOR EACH ROW trigger to enforce these
conventions, if you are worried about accidental violations in your
SQL. In that case, leave the DEFAULT as NULL in the table definition,
but make the trigger do this for INSERT:

NEW.id := nextval('sharedsequence');

and this enforcement check for UPDATE:

IF OLD.id != NEW.id THEN RAISE EXCEPTION 'id is immutable';

If that's not a strong enough guarantee, I'm not sure I understand
your problem statement...

Karl

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gavin Flower 2017-09-14 21:52:52 Re: looking for a globally unique row ID
Previous Message Rob Sargent 2017-09-14 21:15:07 Re: looking for a globally unique row ID