Re: Bit-wise foreign keys - Mailing list pgsql-general

From Steve Atkins
Subject Re: Bit-wise foreign keys
Date
Msg-id 70EF62BC-B9AD-40C2-9B70-A8F53708F4FD@blighty.com
Whole thread Raw
In response to Bit-wise foreign keys  (Alban Hertroys <dalroi@solfertje.student.utwente.nl>)
Responses Re: Bit-wise foreign keys
List pgsql-general
On Sep 20, 2010, at 10:06 AM, Alban Hertroys wrote:

> Hey all,
>
> I'm tossing an idea around again, namely using bit positions and values as foreign key references. Let's start with a
bitof background information: 
>
> I'm currently parsing a log-file that I want to apply all kinds of statistical analysis to. This file contains lines
ofrecords of data, among which are some bytes of which each bit marks a certain truth-value. As an internal data-object
that'sjust dandy, but presenting it to, for example a user, or to query it for certain masks without having to delve
intothe definition of that particular bit-field it would be great to have a textual representation of each bit. 
>
> Let's say we have this byte, with the attached meanings:
>  pos  meaning
>   0   RED    (least significant bit)
>   1   GREEN
>   2   BLUE
>   3   FIRE
>   4   WATER
>   5   EARTH
>   7   AIR    (most significant bit)
>
> Now if I see a value of 0x05, I know that this corresponds to [RED, FIRE] and if I have a value of 0x41 I know that
I'min trouble as there's a bit set that has no meaning! 
>
> Reeks of a foreign key constraint, doesn't it? An odd one though, as one value can contain multiple bits and thus
referencesmultiple foreign values... 

Or references a single foreign value, if you have a reference table with all the valid bit combinations, which'd be
prettysimple to generate programatically for small numbers of combinations. 

  insert into foo (k integer, v text[]) values (0x21, '{"EARTH","GREEN"}';

You could also apply any other set of constraints you wanted in that way (Fire is Red, Water is either Blue or Green).

Or you could use a separate table to store the (record, enum) pairs with one entry for each set bit in each record,
withone foreign key constraint to the table of records and one to a static table of valid enum value types ((1, 'red'),
(2,'green'), (4, 'blue') ...). 

Cheers,
  Steve



pgsql-general by date:

Previous
From: Brad Nicholson
Date:
Subject: Re: Binary Replication and Slony
Next
From: Aram Fingal
Date:
Subject: Installing Contrib Modules with a Precompiled Binary