Friday, December 19, 2008

Solution to PostgreSQL Serial Limitations

In PostgreSQL, an automatic incrementing column is called "serial". This is often used as a primary key of a table. The default serial column is of type INT4 which has a maximum value of 2,147,483,647. To increase this maximum number, create an INT8 data type for your serial column, as follows:

>CREATE SEQUENCE person_id_seq;
>CREATE TABLE person( id INT8 NOT NULL DEFAULT nextval('person_id_seq'), name TEXT);
>CREATE UNIQUE INDEX person_id_key on person(id);

This will increase the capacity of the column to 9,223,372,036,854,775,807. I don't know if there is an INT16 column type. If there is, theoretically we can increase further the capacity of the column by a factor of 2 since PostgreSQL does not support unsigned integers as far as I know.

No comments: