Re: [GENERAL] looking for a globally unique row ID - Mailing list pgsql-general

From Karl Czajkowski
Subject Re: [GENERAL] looking for a globally unique row ID
Date
Msg-id 20170914212112.GC10289@moraine.isi.edu
Whole thread Raw
In response to Re: [GENERAL] looking for a globally unique row ID  (Rafal Pietrak <rafal@ztk-rp.eu>)
List 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


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

pgsql-general by date:

Previous
From: Rob Sargent
Date:
Subject: Re: [GENERAL] looking for a globally unique row ID
Next
From: Gavin Flower
Date:
Subject: Re: [GENERAL] looking for a globally unique row ID