Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the limit of GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY in PostgreSQL?

There are smallserial, serial and bigserial numeric data types in PostgreSQL, which have obvious limits to 32767, 2147483647 and 9223372036854775807 respectively.

But what about GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY, does it have any restrictions? Or maybe they are computed according to the provided data type (SMALLINT, INT, BIGINT)?

like image 708
etimesoy Avatar asked Dec 15 '21 13:12

etimesoy


People also ask

What is generated by default as identity?

The GENERATED BY DEFAULT generates sequential integers for the identity column. However, if you provide a value for insert or update, the database system will use that value for insert instead of using the auto-generated value.

What is identity in PostgreSQL?

In PostgreSQL, the GENERATED AS IDENTITY constraint is used to create a PostgreSQL identity column. It allows users to automatically assign a unique value to a column. The GENERATED AS IDENTITY constraint is the SQL standard-conforming variant of the PostgreSQL's SERIAL column.

Does Postgres automatically create ID?

By simply setting our id column as SERIAL with PRIMARY KEY attached, Postgres will handle all the complicated behind-the-scenes work and automatically increment our id column with a unique, primary key value for every INSERT .

What is serial data type in PostgreSQL?

PostgreSQL has a special kind of database object generator called SERIAL. It is used to generate a sequence of integers which are often used as the Primary key of a table. Syntax: variable_name SERIAL. When creating a table, this sequence of integers can be created as follows: CREATE TABLE table_name( id SERIAL );

What does generated always mean in PostgreSQL?

The type can be SMALLINT, INT, or BIGINT. The GENERATED ALWAYS instructs PostgreSQL to always generate a value for the identity column. If you attempt to insert (or update) a value into the GENERATED ALWAYS AS IDENTITY column, PostgreSQL will issue an error.

What is generated as identity in PostgreSQL?

The GENERATED AS IDENTITY constraint is the SQL standard-conforming variant of the PostgreSQL’s SERIAL column. Let’s analyze the above syntax. The type can be SMALLINT, INT, or BIGINT. The GENERATED ALWAYS instructs PostgreSQL to always generate a value for the identity column.

How to override the options of a PostgreSQL sequence?

The Postgres documentation CREATE TABLE says for GENERATED BY DEFAULT AS IDENTITY (sequence_options) that: The optional sequence_options clause can be used to override the options of the sequence. See CREATE SEQUENCE for details. The documentation CREATE SEQUENCE no longer calls them as options but parameters.

Does PostgreSQL use system-generated values for insert and update?

However, if you provide a value for insert or update, PostgreSQL will use that value to insert into the identity column instead of using the system-generated value. Now let’s look into some examples.


2 Answers

Yes, it is dependent on column's data type and could be validated using COLUMNS metadata:

CREATE TABLE t1(id SMALLINT GENERATED ALWAYS AS IDENTITY);
CREATE TABLE t2(id INT GENERATED ALWAYS AS IDENTITY);
CREATE TABLE t3(id BIGINT GENERATED ALWAYS AS IDENTITY);

SELECT table_name, column_name, data_type,
       is_identity, identity_minimum, identity_maximum, *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN('t1','t2','t3');

db<>fiddle demo

Output:

enter image description here

like image 133
Lukasz Szozda Avatar answered Sep 23 '22 16:09

Lukasz Szozda


There are smallserial, serial and bigserial numeric data types in PostgreSQL, ...

Those are not actual data types to begin with. The manual:

The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns

The actual data type used is smallint, int and bigint, respectively.

See:

  • How to convert primary key from integer to serial?
  • Safely rename tables using serial primary key columns

All serial types draw numbers from an owned SEQUENCE, which is based on bigint. The manual:

Sequences are based on bigint arithmetic, so the range cannot exceed the range of an eight-byte integer (-9223372036854775808 to 9223372036854775807).

IDENTITY columns do the same, only the SEQUENCE is exclusively bound to the owning column, which avoids some oddities that serial "types" exhibit.

See:

  • Auto increment table column
like image 22
Erwin Brandstetter Avatar answered Sep 22 '22 16:09

Erwin Brandstetter