0

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?

8
  • What is the CREATE TABLE statement? Commented Sep 15, 2020 at 10:50
  • create table trial( id serial, value text); Commented Sep 15, 2020 at 10:52
  • Then you cannot store ids over 2147483647. Commented Sep 15, 2020 at 10:53
  • but explicitly providing a large id works Commented Sep 15, 2020 at 10:55
  • I don't believe that, if you really used serial. Commented Sep 15, 2020 at 11:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Will 2147483647 be the max value of the sequence?
Depends on how it is defined (\ds+ triall_id_seq).
Ok, thank you very much sir for investing so much of your precious time into my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.