satinder singh
satinder singh

Reputation: 310

Serial column of datatype text in psql

I am having a two-column table trial(id serial, value text).

Then I use ALTER TABLE to change the data type of id to text, and still if I insert records like:

insert into trial(value) values('name');

Then serial is still getting incremented everytime I am adding rows into it. If I insert a row like

insert into trial values(111111111111....a very large number, 'name');

It still works and it still increments the serial counter.

My question is: why doesn't it overflow? Though the datatype of the column is text, the serial value would be still stored in a integer internally right? what is the data type of the variable that holds the serial value of a table(in psql's implementation) so that I can be sure that it would never overflow?

Upvotes: 0

Views: 279

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246033

If you insert an explicit value into a column with a DEFAULT, the default value is not used. It does not matter that the value you enter is not a valid integer - it is a valid text, that is all that matters.

If you don't enter a value for id, PostgreSQL uses the sequence and converts the result to text.

Upvotes: 1

Related Questions