On Mon, Aug 20, 2018 at 4:51 PM Tom Lane <tgl@xxxxxxxxxxxxx> wrote: > > Luca Ferrari <fluca1978@xxxxxxxxx> writes: > > I'm trying to define a custom data type that would represent a number > > of bytes in a lossy human way. > > You did not show us the SQL definition of the type. I don't see anything > obviously wrong in what you showed (other than hfsize_add not setting the > result's scaling), so the problem is somewhere else. Given this C > declaration, the type probably needs to be size 16, double alignment, > pass-by-reference; maybe you messed up part of that? > Shame on me: when I issued a create type I didn't realize that I was miswriting the length attribute from 'internallength' to 'internalsize', and while an error was reported, the type was created. Fixing the type creation into: CREATE TYPE hfsize ( internallength = 16, input = hfsize_input_function, output = hfsize_output_function ); solved the problem, so it was a length mismatch. > > HFSize *sum = new_HFSize(); > > What is new_HFSize? An helper function to allocate a new object (and that was why scaling did not get referenced in the add function): HFSize* new_HFSize() { HFSize *size = (HFSize*) palloc( sizeof( HFSize ) ); size->scaling = 0; size->size = 0.0f; return size; } Thanks, Luca