Re: Efficient Boolean Storage

From: Ericson Smith <eric(at)did-it(dot)com>
To: Postgresql General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Efficient Boolean Storage
Date: 2002-12-04 16:01:49
Message-ID: 1039017709.12735.24.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hmmm...

You can store them in an int4 type (will take up to 31)

To store your numbers:
$num = (2^$num1) + (2^$num2) ...
where $num1 and $num2 are your bit positions that you want to set to
"1",

To retrieve them...
SELECT * FROM table WHERE ((mycol & 2^1) != 0 OR (mycol & 2^3) != 0)
Here you are checking for the 1st bit position and the 3rd bit position.

- Ericson Smith
eric(at)did-it(dot)com

On Wed, 2002-12-04 at 10:11, Richard Huxton wrote:
> On Wednesday 04 Dec 2002 1:06 am, Chris White wrote:
> > Hello,
> >
> > I need to store many (e.g. 30) booleans and am wondering what is the
> > most efficient way to store them. I can think of four options.
> >
> > Option 1...Use 'bool' data type
> > No good since each value will require 1 byte rather than 1 bit.
>
> Depends what you're going to use them for. Are these 30 flags that should be
> grouped together? Will the users/app want all together or one at a time? Will
> they feature in WHERE clauses?
>
> --
> Richard Huxton
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-12-04 16:19:38 Re: Drop column and Access
Previous Message Felipe Schnack 2002-12-04 16:01:22 Re: Efficient Boolean Storage