Re: [HACKERS] Re: [GENERAL] Update of bitmask type - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [HACKERS] Re: [GENERAL] Update of bitmask type
Date
Msg-id 6018.938185575@sss.pgh.pa.us
Whole thread Raw
In response to Re: [HACKERS] Re: [GENERAL] Update of bitmask type  (Adriaan Joubert <a.joubert@albourne.com>)
List pgsql-hackers
Adriaan Joubert <a.joubert@albourne.com> writes:
> A second problem I encountered last night is that the postgres variable
> length types only allow for the length of an array to be stored in
> bytes. This means that the number of bits will automatically always be
> rounded up to the nearest factor of 8, i.e. you want tp store 3 bits and
> you get 8. For ordering and output this is not always going to produce
> the correct output, as the bitstrings will get zero-padded. Is there
> anywhere else where one could store the exact length of a bit string?

You will need to put it right in the string, I think.  You could
dedicate the first byte of the value of a bitstring (after the required
varlena length word) to indicating how many bits in the last byte are
wasted padding (0-7).  That would leave a few spare bits in this header
byte that might or might not have any good use.

> I haven't quite understood what the variable attypmod is. In varchar.c
> it looks as if it is the length of the record, but if it is just an
> integer identifier, then I could store the exact length in there. In
> that case I could handle the difference between 3 and 5 bit strings
> correctly. My main worry was that this might be used in other routines
> to determine the length of a record.

atttypmod is a type-specific modifier: if you are developing a new data
type then you can define it any way you darn please.  However, it's not
quite as useful as it first appears, because it is only stored in
connection with a column of a table --- there is no atttypmod associated
with the result of a function, for example.  It is primarily useful if
you want to be able to coerce values into a common subformat when they
are stored into a column.  For example, fixed-length char(n) types use
atttypmod as the column width so that they can pad or truncate a
supplied string to the right length just before storing.  But a
free-standing string value does not have an atttypmod, only a length.
Similar remarks apply to NUMERIC, which uses atttypmod to store the
desired precision for a column, but not to figure out the actual
precision of a value in memory.  In short, your datatype representation
needs to be self-identifying without help from atttypmod.
        regards, tom lane


pgsql-hackers by date:

Previous
From: Thomas Lockhart
Date:
Subject: Re: PostgreSQL Upgrade Procedure
Next
From: Tom Lane
Date:
Subject: Re: [HACKERS] int8 and index