Re: Creating a sequence - Mailing list pgsql-jdbc

From Craig Ringer
Subject Re: Creating a sequence
Date
Msg-id 480F6B43.8080407@postnewspapers.com.au
Whole thread Raw
In response to Creating a sequence  (Chris <cmattmiller@gmail.com>)
List pgsql-jdbc
Chris wrote:
> That was my problem, I didn't know where or how to create the sequence.
> Got it to work now. :D
> So, should the standard practice be to create a sequence for every table?

That leads in to the synthetic vs natural primary key debate, which
isn't worth getting into here.

If you have a synthetic key on a table (a "meaningless" integer
identifier generated by the application or database) then a sequence is
an extremely good way of generating such identifiers. The use of a
sequence permits multiple concurrent inserts without risk of conflict
and without the need for locking.

You *WILL* get gaps in your primary key values, though, where
transactions have rolled back. Your application should be designed not
to care about this.

By the way, I pointed out the stupid way of setting a sequence start
value. What I should've written was:

CREATE SEQUENCE employee_emp_uid_seq;
SELECT setval('employee_emp_uid_seq',
    (SELECT max(emp_uid) FROM employee));

--
Craig Ringer

pgsql-jdbc by date:

Previous
From: Tom Lane
Date:
Subject: Re: Query parameters limit in postgres jdbc driver?
Next
From: Chris
Date:
Subject: Login Roles