Hi,

I’m defining a custom type “MyType” with additional functions and an custom 
aggregate in a C-coded extension. From a PostgreSQL perspective it is a base 
type that piggybacks on the bytea type, i.e. LIKE = BYTEA.
 
But now I need to (re)define MyType to support type modifiers (e.g. 
MyType(1,14,18)) and I got that done using CREATE TYPE’s TYPMOD_IN and 
TYPMOD_OUT parameters resulting in the correct packed value getting stored in 
pg_attribute when I define a column of that type. 

But when I pass a MyType value to a function defined in my C extension how 
would I access the type modifier value for the argument which could have been 
drawn from the catalog or the result of a cast. 

E.g. if I: 
SELECT MyFunc(‘xDEADBEEF’::MyType(1,14,18));
In the C function MyFunc calls I get a pointer to the data using 
PG_GETARG_BYTEA_P(0) macro and its length using the VARSIZE macro but I also 
need the given type modifiers (1, 14 and 18 in the example) before I can 
process the data correctly. Clearly I'd have to unpack the component values 
myself from the 16-bit atttypemod value into which the TYPMOD_OUT function has 
packed it, but where would I get access to that value?  My type is written in C 
to be as fast as possible having to go do some SPI-level lookup or involved 
logic would slow it right down again.

My searches to date only yielded results referring to the value stored for a 
table in pg_attribute with the possibility of there being a value in 
HeapTupleHeader obtained by using the PG_GETARG_HEAPTUPLEHEADER(0) macro but 
that assumes the parameter is a tuple, not the individual value it actually is. 
I struggle to imagine that the type modifier value isn't being maintained by 
whatever casts are applied and not getting passed through to the extension 
already, but where to find it? 

Can someone please point me in the right direction here, the name of the 
structure containing the raw type modifier value, the name of the value in that 
structure, the name of a macro that accesses it, even if it’s just what 
keywords to search for in the documentation and/or archives? Even if it’s just 
a pointer to the code where e.g. the numeric type (which has type modifiers) is 
implemented so I can see how that code does it. Anything, I’m getting 
desperate. Perhaps not many before me needed to do this so it's not often 
mentioned, but sure it is in there somewhere, how else would type like numeric 
and even varchar actually work (given that the VARSIZE of a varlena gives its 
actual size, not the maximum as given when the column or value was created)?

Thank you in advance,

Marthin Laubscher




Reply via email to