Re: Regarding varchar max length in postgres

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Durgamahesh Manne <maheshpostgres9(at)gmail(dot)com>
Cc: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org, andreas(at)a-kretschmer(dot)de
Subject: Re: Regarding varchar max length in postgres
Date: 2018-10-15 14:24:23
Message-ID: 9907.1539613463@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Durgamahesh Manne <maheshpostgres9(at)gmail(dot)com> writes:
>>> If character varying is used without length specifier, the type
>>> accepts strings of any size
>>> but varchar does not accept more than this 10485760 value

You're confusing the size of string that can be stored with the
largest value accepted for "n" in "varchar(n)". This is documented,
in the same place that people have been pointing you to:

In any case, the longest possible character string that can be stored
is about 1 GB. (The maximum value that will be allowed for n in the
---------------------------------------------------
data type declaration is less than that. It wouldn't be useful to
----------------------------------------
change this because with multibyte character encodings the number of
characters and bytes can be quite different. If you desire to store
long strings with no specific upper limit, use text or character
varying without a length specifier, rather than making up an arbitrary
length limit.)

As you found out, the limit for "n" is ~ 10 million.

In principle, we could have allowed it to be as much as 1Gb divided by
the maximum character length of the database's encoding, but it did
not seem like a great idea for the limit to be encoding-dependent.

As the last sentence in the doc paragraph points out, the preferred
thing to do if you just want to allow very long strings is to leave
off "(n)" altogether.

The subtext here, which maybe we ought to state in a more in-your-face
way, is that if you use char(N) or varchar(N) without a concrete
application-driven reason why N has to be that particular value,
no more or less, then You're Doing It Wrong. Artificially-chosen
column width limits are a bad idea left over from the days of
punched cards. The reason the limit on N is much smaller than it
could theoretically be is that column declarations with very large
N are, without exception, violations of this principle.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2018-10-15 14:36:00 Re: FATAL: terminating connection because protocol synchronization was lost
Previous Message talk to ben 2018-10-15 13:24:36 Re: Setting up continuous archiving