Postgres bit1

Author: Adriaan Joubert <adriaan@albourne.com>

Standard disclaimer: use at your own risk!


This postgres type defines a 1-byte bit mask for postgres. As the
functions only use C's bit operators they should run anywhere, but if
there are any problems, please let me know. The SQL code to allow you
to use the bit12-type in btree indexes is also included. Comment it
out if you don't ever want to use the bit mask in an index. If anybody
figures out how to do hash indexes, please let me know.

Thanks are due to Thomas Lockhart, as I borrowed heavily from the int8
type in the contrib directory, as well as the complex type in the
tutorial.

The operators that are defined are

&   bitwise-AND
||  bitwise-OR   (v6.5beta would not allow me to define a | operator)
^   bitwise-XOR
~   bitwise-NOT
<<  bit shift left
>>  bit shift right
!   returns a bit mask with exactly bit 0 set, if any of the bits were set

The following conversion functions are available

bool(*)
int(*)

and, provided that the number is smaller than 255, there is also a 

bit1(int4)

conversion function.

Elements of type bit1 are output as a bit mask, i.e. as 'b00110011',
but can be read as

b1111      - a bit mask string of any length up to 8
x0F        - a hex string
15         - an integer

Two aggregate functions are also defined:

bit1all    - a bit mask with bit x set iff ALL masks in the set had bit x set
bit1any    - a bit mask with bit x set iff ANY masks in the set had bit x set

If any other operators are widely used, please let me know and I will
add them. It is relatively trivial to add a bit2/bit4 type.


Have fun,

Adriaan


INSTALLATION

The easiest way to install this is to unpack it in the contrib
directory of the postgres build tree. By default it will install the
shared library 'bit1.so' in $PGROOT/lib/contrib. This contrib
directory does not usually exist, so you will have to create it and
make it world-writable.

Type

	make

which will create the files bit1.sql and bit1.so and

	make install

which will install the shared library. If this doesn't work, you will
have to change some paths in the makefile, and make sure that the path
to the shared object in bit1.so is correct.

Once built, you need to read in the definitions

        psql <dbname> < bit1.sql

To remove the bit1 type and all related functions do

	psql <dbname> < bit1.clean.sql


A few examples are in show.sql. Note that, to use the index, you may
need to create a bigger table (insert into bit1_example select * from
bit1_example) and run vacuum analyse. 
