Re: [PERFORM] Auto generate number in Postgres-9.1. - Mailing list pgsql-performance

From Andreas Karlsson
Subject Re: [PERFORM] Auto generate number in Postgres-9.1.
Date
Msg-id efd88f08-1a8e-c883-9842-38fe63588c18@proxel.se
Whole thread Raw
In response to Re: [PERFORM] Auto generate number in Postgres-9.1.  (Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>)
List pgsql-performance
On 03/20/2017 03:08 PM, Dinesh Chandra 12108 wrote:
> But on deletion it's not automatically re-adjusting the id's.
>
> Do I need to create trigger for this??

It is possible to do but I advice against adjusting the IDs on DELETE
due to to do so safely would require locking the entire table in the
trigger.

Note that serial columns will also get holes on ROLLBACK. In general I
think the right thing to do is accept that your ID columns can get a bit
ugly.

For example:

CREATE TABLE t (id serial);

INSERT INTO t DEFAULT VALUES;

BEGIN;

INSERT INTO t DEFAULT VALUES;

ROLLBACK;

INSERT INTO t DEFAULT VALUES;

Gives us the following data in the table:

  id
----
   1
   3
(2 rows)

Andreas


pgsql-performance by date:

Previous
From: Dinesh Chandra 12108
Date:
Subject: Re: [PERFORM] Auto generate number in Postgres-9.1.
Next
From: James Parks
Date:
Subject: [PERFORM] Optimizing around retained tuples