Re: varchar(n) VS text - Mailing list pgsql-general

From Tom Lane
Subject Re: varchar(n) VS text
Date
Msg-id 10898.1182831377@sss.pgh.pa.us
Whole thread Raw
In response to varchar(n) VS text  ("Pierre Thibaudeau" <pierdeux@gmail.com>)
Responses Re: varchar(n) VS text  (Paul Lambert <paul.lambert@autoledgers.com.au>)
Re: varchar(n) VS text  (Kev <kevinjamesfield@gmail.com>)
List pgsql-general
"Pierre Thibaudeau" <pierdeux@gmail.com> writes:
> I am puzzling over this issue:

> 1) Is there ever ANY reason to prefer "varchar(n)" to "text" as a column type?

In words of one syllable: no.

Not unless you have an application requirement for a specific maximum
length limit (eg, your client code will crash if fed a string longer
than 256 bytes, or there's a genuine data-validity constraint that you
can enforce this way).

Or if you want to have schema-level portability to some other DB that
understands varchar(N) but not text.  (varchar(N) is SQL-standard,
while text isn't, so I'm sure there are some such out there.)

> From my reading of the dataype documentation, the ONLY reason I can
> think of for using "varchar(n)" would be in order to add an extra
> data-type constraint to the column.

That is *exactly* what it does.  No more and no less.  There's no
performance advantage, in fact you can expect to lose a few cycles
to the constraint check.

            regards, tom lane

pgsql-general by date:

Previous
From: Ben
Date:
Subject: Re: varchar(n) VS text
Next
From: Rafal Pietrak
Date:
Subject: a JOIN on same table, but 'slided over'