tags:

views:

29

answers:

1

Hi

We are running PostGres 8.3.3 and the documentation (http://www.postgresql.org/docs/8.3/static/datatype-character.html) leaves me with a few questions about how storage is allocated.

For the purposes of this discussion I'd like to stick to assuming a character = 1 byte.

I understand that character(n) blank pads so you always have n bytes stored.

If your char(n) > 126 characters long you have an overhead of 4 bytes (presumably for storing the length), otherwise an overhead of 1 byte.

I'm interested in what happens with varchar(n).

If I use varchar(255) and store a 4 character string in it do I have 4 bytes for the string & 4 bytes overhead or do I have just one byte overhead until I hit the 126 character limit?

I've googled and can't find this documented anywhere.

Any thoughts?

+1  A: 

It's the length of the data you store that controls the overhead, not the maximum length of the column. So you have the one byte overhead until you hit 126.

Magnus Hagander
Thanks Magnus. That answers my question ... icing on the cake would be whether or not this is documented anywhere ...
azp74
@nos - the question was about varchar, not char.
Magnus Hagander
@apz74 - that page actually has it. Not all that easy to derive from it, of course...
Magnus Hagander