0

Anyone can help give a C-language function example that use a custom type as one of the parameters?

Today, I tried to follow the documentation, but I was not able to get the correct values of my custom type.

here is the example: http://www.postgresql.org/docs/9.3/static/xfunc-c.html#XFUNC-C-BASETYPE

#include "postgres.h"
#include "executor/executor.h"  /* for GetAttributeByName() */

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

PG_FUNCTION_INFO_V1(c_overpaid);

Datum
c_overpaid(PG_FUNCTION_ARGS)
{
    HeapTupleHeader  t = PG_GETARG_HEAPTUPLEHEADER(0);
    bool isnull;
    Datum salary;

    salary = GetAttributeByName(t, "salary", &isnull);
    if (isnull)
        PG_RETURN_BOOL(false);

    PG_RETURN_FLOAT4(DatumGetFLOAT4(salary) );
}

the answer is always "0".

you can use "CREATE TYPE V AS(salary float);" as the custom type. thx.

jotik
  • 17,044
  • 13
  • 58
  • 123
Songjs
  • 13
  • 2

1 Answers1

0

The above problem has been solved.

It is the problem of the numbers of bytes.

I introduced the double type, but use the FLOAT4 to get the value. I should use the FLOAT8 to get the right value.

2016-03-05

Songjs
  • 13
  • 2