storage size of "bit" data type..

Lists: pgsql-general
From: Alex Mayrhofer <axelm(at)nona(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: storage size of "bit" data type..
Date: 2007-12-05 19:19:39
Message-ID: 4756F9CB.1090704@nona.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi,

i'm trying to find out the storage size for bit(n) data. My initial
assumption would be that for any 8 bits, one byte of storage is required.

Is this assumption correct? I didn't find that information in the online
docs.

thanks,

Alex


From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Alex Mayrhofer <axelm(at)nona(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: storage size of "bit" data type..
Date: 2007-12-06 01:23:16
Message-ID: E464B928-C672-4F6A-9311-63644EA63E36@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Dec 5, 2007, at 14:19 , Alex Mayrhofer wrote:

> Hi,
>
> i'm trying to find out the storage size for bit(n) data. My initial
> assumption would be that for any 8 bits, one byte of storage is
> required.

select pg_column_size(B'1') as "1bit",
pg_column_size(B'1111') as "4bits",
pg_column_size(B'11111111') as "1byte",
pg_column_size(B'111111111111') as "12bits",
pg_column_size(B'1111111111111111') as "2bytes",
pg_column_size(B'11111111111111111') as "17bits",
pg_column_size(B'111111111111111111111111') as "3bytes";
1bit | 4bits | 1byte | 12bits | 2bytes | 17bits | 3bytes
------+-------+-------+--------+--------+--------+--------
9 | 9 | 9 | 10 | 10 | 11 | 11
(1 row)

Looks like there's 8 bytes of overhead as well, probably because a
bit string is a varlena type.

Michael Glaesemann
grzm seespotcode net


From: Decibel! <decibel(at)decibel(dot)org>
To: Michael Glaesemann <grzm(at)seespotcode(dot)net>
Cc: Alex Mayrhofer <axelm(at)nona(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: storage size of "bit" data type..
Date: 2007-12-06 22:17:01
Message-ID: 067676FA-77B3-4EDF-B412-5AE93BD9820E@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Dec 5, 2007, at 7:23 PM, Michael Glaesemann wrote:
> On Dec 5, 2007, at 14:19 , Alex Mayrhofer wrote:
>> i'm trying to find out the storage size for bit(n) data. My
>> initial assumption would be that for any 8 bits, one byte of
>> storage is required.
>
> select pg_column_size(B'1') as "1bit",
> pg_column_size(B'1111') as "4bits",
> pg_column_size(B'11111111') as "1byte",
> pg_column_size(B'111111111111') as "12bits",
> pg_column_size(B'1111111111111111') as "2bytes",
> pg_column_size(B'11111111111111111') as "17bits",
> pg_column_size(B'111111111111111111111111') as "3bytes";
> 1bit | 4bits | 1byte | 12bits | 2bytes | 17bits | 3bytes
> ------+-------+-------+--------+--------+--------+--------
> 9 | 9 | 9 | 10 | 10 | 11 | 11
> (1 row)
>
> Looks like there's 8 bytes of overhead as well, probably because a
> bit string is a varlena type.

Wow, that's screwed up... that's a lot more than varlena overhead:

select pg_column_size('a'::text), pg_column_size(1::numeric),
pg_column_size(3111234::numeric);
pg_column_size | pg_column_size | pg_column_size
----------------+----------------+----------------
5 | 10 | 12

Apparently it's something related to numeric.
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Decibel!" <decibel(at)decibel(dot)org>
Cc: "Michael Glaesemann" <grzm(at)seespotcode(dot)net>, "Alex Mayrhofer" <axelm(at)nona(dot)net>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: storage size of "bit" data type..
Date: 2007-12-06 23:10:33
Message-ID: 87ir3bpggm.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

"Decibel!" <decibel(at)decibel(dot)org> writes:

> On Dec 5, 2007, at 7:23 PM, Michael Glaesemann wrote:
>> On Dec 5, 2007, at 14:19 , Alex Mayrhofer wrote:
>>> i'm trying to find out the storage size for bit(n) data. My initial
>>> assumption would be that for any 8 bits, one byte of storage is required.
>>
>> select pg_column_size(B'1') as "1bit",
>> pg_column_size(B'1111') as "4bits",
>> pg_column_size(B'11111111') as "1byte",
>> pg_column_size(B'111111111111') as "12bits",
>> pg_column_size(B'1111111111111111') as "2bytes",
>> pg_column_size(B'11111111111111111') as "17bits",
>> pg_column_size(B'111111111111111111111111') as "3bytes";
>> 1bit | 4bits | 1byte | 12bits | 2bytes | 17bits | 3bytes
>> ------+-------+-------+--------+--------+--------+--------
>> 9 | 9 | 9 | 10 | 10 | 11 | 11
>> (1 row)
>>
>> Looks like there's 8 bytes of overhead as well, probably because a bit
>> string is a varlena type.
>
> Wow, that's screwed up... that's a lot more than varlena overhead:

It needs to store the number of bits present as well. Otherwise it wouldn't be
able to tell apart B'1' and B'01' ... B'00000001'

> select pg_column_size('a'::text), pg_column_size(1::numeric),
> pg_column_size(3111234::numeric);
> pg_column_size | pg_column_size | pg_column_size
> ----------------+----------------+----------------
> 5 | 10 | 12
>
> Apparently it's something related to numeric.

Only in the sense that numeric also has to store some meta data as well like
the weight and display precision.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's On-Demand Production Tuning


From: "Vyacheslav Kalinin" <vka(at)mgcp(dot)com>
To: Decibel! <decibel(at)decibel(dot)org>
Cc: "Michael Glaesemann" <grzm(at)seespotcode(dot)net>, "Alex Mayrhofer" <axelm(at)nona(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: storage size of "bit" data type..
Date: 2007-12-06 23:19:46
Message-ID: 9b1af80e0712061519t20f39c4bye1c55f0c7deb49cc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> It needs to store the number of bits present as well

Couldn't that be reduced to 1 byte that'd say how many bits count in the
last byte?

> Only in the sense that numeric also has to store some meta data as well
like
the weight and display precision.

Is it really necessary to store display precision when it can be taken from
the table column definition?


From: Decibel! <decibel(at)decibel(dot)org>
To: "Vyacheslav Kalinin" <vka(at)mgcp(dot)com>
Cc: "Michael Glaesemann" <grzm(at)seespotcode(dot)net>, "Alex Mayrhofer" <axelm(at)nona(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: storage size of "bit" data type..
Date: 2007-12-06 23:34:25
Message-ID: 6D69367B-D029-4E58-AF8F-13EF7B59F427@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Dec 6, 2007, at 5:19 PM, Vyacheslav Kalinin wrote:
> > It needs to store the number of bits present as well
>
> Couldn't that be reduced to 1 byte that'd say how many bits count
> in the last byte?
>
> > Only in the sense that numeric also has to store some meta data
> as well like
> the weight and display precision.
>
> Is it really necessary to store display precision when it can be
> taken from the table column definition?

Two problems...

1) CREATE TABLE n(n numeric);

2) The knowledge of extra type information (ie: the numbers in char()
or numeric()) don't extend deeply enough into the code. This is part
of why char() uses the exact same storage mechanism as varchar().
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Decibel! <decibel(at)decibel(dot)org>
To: Gregory Stark <stark(at)enterprisedb(dot)com>
Cc: "Michael Glaesemann" <grzm(at)seespotcode(dot)net>, "Alex Mayrhofer" <axelm(at)nona(dot)net>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: storage size of "bit" data type..
Date: 2007-12-06 23:35:13
Message-ID: 89C7452B-E92D-4FAF-8268-DD8FE55F0E4B@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Dec 6, 2007, at 5:10 PM, Gregory Stark wrote:
> It needs to store the number of bits present as well. Otherwise it
> wouldn't be
> able to tell apart B'1' and B'01' ... B'00000001'
...
> Only in the sense that numeric also has to store some meta data as
> well like
> the weight and display precision.

Hrm... perhaps that's another worthwhile target for the varvarlena
technique...
--
Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
Give your computer some brain candy! www.distributed.net Team #1828


From: Bruce Momjian <bruce(at)momjian(dot)us>
To: "Decibel!" <decibel(at)decibel(dot)org>
Cc: Michael Glaesemann <grzm(at)seespotcode(dot)net>, Alex Mayrhofer <axelm(at)nona(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: storage size of "bit" data type..
Date: 2008-03-17 23:49:40
Message-ID: 200803172349.m2HNnee29872@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Added to TODO:

* Reduce BIT data type overhead using short varlena headers

http://archives.postgresql.org/pgsql-general/2007-12/msg00273.php

---------------------------------------------------------------------------

Decibel! wrote:
> On Dec 5, 2007, at 7:23 PM, Michael Glaesemann wrote:
> > On Dec 5, 2007, at 14:19 , Alex Mayrhofer wrote:
> >> i'm trying to find out the storage size for bit(n) data. My
> >> initial assumption would be that for any 8 bits, one byte of
> >> storage is required.
> >
> > select pg_column_size(B'1') as "1bit",
> > pg_column_size(B'1111') as "4bits",
> > pg_column_size(B'11111111') as "1byte",
> > pg_column_size(B'111111111111') as "12bits",
> > pg_column_size(B'1111111111111111') as "2bytes",
> > pg_column_size(B'11111111111111111') as "17bits",
> > pg_column_size(B'111111111111111111111111') as "3bytes";
> > 1bit | 4bits | 1byte | 12bits | 2bytes | 17bits | 3bytes
> > ------+-------+-------+--------+--------+--------+--------
> > 9 | 9 | 9 | 10 | 10 | 11 | 11
> > (1 row)
> >
> > Looks like there's 8 bytes of overhead as well, probably because a
> > bit string is a varlena type.
>
> Wow, that's screwed up... that's a lot more than varlena overhead:
>
> select pg_column_size('a'::text), pg_column_size(1::numeric),
> pg_column_size(3111234::numeric);
> pg_column_size | pg_column_size | pg_column_size
> ----------------+----------------+----------------
> 5 | 10 | 12
>
> Apparently it's something related to numeric.
> --
> Decibel!, aka Jim C. Nasby, Database Architect decibel(at)decibel(dot)org
> Give your computer some brain candy! www.distributed.net Team #1828
>
>

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://postgres.enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +