Re: unsigned types

From: "Jim C(dot) Nasby" <jnasby(at)pervasive(dot)com>
To: Jeff Davis <jdavis-pgsql(at)empires(dot)org>
Cc: jeff sacksteder <jsacksteder(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: unsigned types
Date: 2005-10-17 17:25:18
Message-ID: 20051017172518.GZ86144@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Oct 16, 2005 at 10:08:41AM -0700, Jeff Davis wrote:
> jeff sacksteder wrote:
> >
> >The sign doesn't concern me. I am storing a value that is unsigned and 16
> >bits wide natively. I'll have to just use an int4 and waste twice the space
> >I actually need.
> >
>
> Are you sure you'd really save space with a 16 bit type? Often times
> that savings gets lost in alignment.
>
> As far as I know, the smallest type that PostgreSQL supports is 4 bytes.
> On 64-bit architectures, it may be effectively 8 bytes (although I'm not
> sure about that).

It depends on MAXALIGN, which is 4 bytes on most platforms. But, there's
more to the story than that... If you SELECT typname, typalign FROM
pg_type WHERE typname LIKE 'int%'; you'll see that int2 can actually
align on smallint (typically 2 byte) boundaries. So, if you have a bunch
of int2's all next to each other in a table, they will happily just
consume 2 bytes. The issue comes when you try and mix them with other
fields randomly, since many other fields require int alignment.

Also, your suggestion of packing could actually hurt, since it will be
forced to an 8 byte boundary on most systems (int8 requires 'double'
alignment). If you instead used 4 smallint fields, all together, you
would probably only waste 2 bytes. Of course, this is all 100% dependant
on the other fields in the table.
--
Jim C. Nasby, Sr. Engineering Consultant jnasby(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dean Gibson (DB Administrator) 2005-10-17 17:29:43 Re: Planner regression in 8.0.x: WORKAROUND
Previous Message Tom Lane 2005-10-17 17:19:27 Re: [GENERAL] Postgres logs to syslog LOCAL0