5

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)?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
etimesoy
  • 53
  • 5
  • 2
    "*Or maybe they are computed according to the provided data type*" - yes. And the "serial" types work exactly the same way (they aren't real data types to begin with) –  Dec 15 '21 at 14:08

2 Answers2

4

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

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
3

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:

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:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228