Re: Datatype sizes; a space and speed issue? - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: Datatype sizes; a space and speed issue?
Date
Msg-id 20040623045139.GA16202@dcc.uchile.cl
Whole thread Raw
In response to Re: Datatype sizes; a space and speed issue?  (Joel Matthew <rees@ddcom.co.jp>)
Responses Re: Datatype sizes; a space and speed issue?  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: Datatype sizes; a space and speed issue?  (Madison Kelly <linux@alteeve.com>)
Re: Datatype sizes; a space and speed issue?  (Joel Matthew <rees@ddcom.co.jp>)
List pgsql-general
On Wed, Jun 23, 2004 at 01:15:23PM +0900, Joel Matthew wrote:
> > What effect is there if I specify "TEXT" instead of
> > say "VARCHAR(255)"?
>
> Well, theoretically, in the latter case, the database will allocate 256
> (257? 259?) bytes for that field in the record itself. That is, that
> field will consume 256 bytes for each record stored.

Nope.  Actually, in both cases the length will be stored first (4 bytes)
and the actual content following it, using the indicated amount of
bytes.  There's absolutely no difference in storage.

A varchar(256) field will allow you to store a text not with 256 bytes
max, but 256 _chars_ max.  Think multibyte encodings such as utf8 -- the
varchar(256) can take anything from 4 + 1 bytes (a single byte string) to
4 + 256 * max_bytes_per_char.


The difference you cite is for char(N) fields, which are always padded
with blanks to fill the N chars.

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"There is evil in the world. There are dark, awful things. Occasionally, we get
a glimpse of them. But there are dark corners; horrors almost impossible to
imagine... even in our worst nightmares." (Van Helsing, Dracula A.D. 1972)


pgsql-general by date:

Previous
From: Joel Matthew
Date:
Subject: Re: Datatype sizes; a space and speed issue?
Next
From: Tom Lane
Date:
Subject: Re: Parameters from trigger to function.