Re: Efficient Boolean Storage - Mailing list pgsql-general

From Ericson Smith
Subject Re: Efficient Boolean Storage
Date
Msg-id 1039017709.12735.24.camel@localhost.localdomain
Whole thread Raw
In response to Re: Efficient Boolean Storage  (Richard Huxton <dev@archonet.com>)
Responses Re: Efficient Boolean Storage  (Felipe Schnack <felipes@ritterdosreis.br>)
List 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@did-it.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@postgresql.org so that your
> message can get through to the mailing list cleanly



pgsql-general by date:

Previous
From: Scott Lamb
Date:
Subject: Re: User date_trunc function in plpgsql function
Next
From: Felipe Schnack
Date:
Subject: Re: Efficient Boolean Storage