help defining a basic type operator

From: Luca Ferrari <fluca1978(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: help defining a basic type operator
Date: 2018-08-20 14:40:21
Message-ID: CAKoxK+6huo++-LL50d-5qvST94cFKpNR4_qZnEqDYZfbvbXBPQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,
I'm trying to define a custom data type that would represent a number
of bytes in a lossy human way.
The type is defined as:

typedef struct HFSize
{
double size;
int scaling;
} HFSize;

and the operator function is defined as:

Datum
hfsize_add(PG_FUNCTION_ARGS)
{

HFSize *first = (HFSize *) PG_GETARG_POINTER(0);
HFSize *second = (HFSize *) PG_GETARG_POINTER(1);
HFSize *sum = new_HFSize();

to_bytes( first );
to_bytes( second );

elog( DEBUG1, "sum %s + %s ", to_string( first ), to_string( second ) );
sum->size = first->size + second->size;

elog( DEBUG1, "Final sum %s ", to_string( sum ) );
PG_RETURN_POINTER( sum );
}

The problem is that, even if the last elog shows a correct result, the
final value returned via PG_RETURN_POINTER is something totally
different with the double value set to zero and an apparently random
'scaling':

# set client_min_messages to debug;
SET
testdb=# SELECT '1030'::hfsize + '2030'::hfsize;
DEBUG: Converting to human text format 1030.00-bytes
LINE 1: SELECT '1030'::hfsize + '2030'::hfsize;
^
DEBUG: Converting to human text format 2030.00-bytes
LINE 1: SELECT '1030'::hfsize + '2030'::hfsize;
^
DEBUG: sum 1030.00-bytes + 2030.00-bytes
DEBUG: Final sum 3060.00-bytes
?column?
----------
0.00-64

I've tried also to return one of the two operands from the add
function, so something like:
Datum
hfsize_add(PG_FUNCTION_ARGS)
{

HFSize *first = (HFSize *) PG_GETARG_POINTER(0);
HFSize *second = (HFSize *) PG_GETARG_POINTER(1);
PG_RETURN_POINTER( first );
}

but again the result has a zero value and a random scaling, and most
notably is not the first operand. Also memory addresses (used with %x)
are different from inside and outside the add function.
Is there something I'm missing?

Thanks,
Luca

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2018-08-20 14:51:45 Re: help defining a basic type operator
Previous Message TalGloz 2018-08-20 13:56:05 Re: Linker errors while creating a PostgreSQL C extension function.