Skip site navigation (1) Skip section navigation (2)

C Function returning a tuple with a float4 array as column

From: Tim <tim(dot)child(at)comcast(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: C Function returning a tuple with a float4 array as column
Date: 2010-12-05 17:52:14
Message-ID: 4CFBD14E.9090808@comcast.net (view raw or flat)
Thread:
Lists: pgsql-novice
HI,

Anyone have any ideas what wrong  with my C code to return a tuple with 
1 column that is a float4 array. I have tested  functions returning  a 
float4 array and a row and both  work independently. When I combine the 
  code  I get  the SQL error

ERROR:  cache lookup failed for type 0

********** Error **********

ERROR: cache lookup failed for type 0
SQL state: XX000

from  the SQL  query

select  pg_backend_pid(),  getrecordarray();

The function is  defined

create or replace  function GetRecordArray() returns table (
     Param_1  real[]
)
       AS  .....
      LANGUAGE C STRICT;

The C code is

  PG_FUNCTION_INFO_V1(GetRecordAray);
  /**
     GetRecordArray  Function to return a record with  1 column as a 
Float4 array
     @returns record with a float4 array
  **/
  DLLEXPORT  Datum  GetRecordArray(PG_FUNCTION_ARGS)
  {
     Datum        column[1];    /*  Columns for the record */
     bool        isNULL[1]  = { false };
     Datum        results;    /* Results tuple */
     HeapTuple    tuple;        /* Record Tuple */
     TupleDesc    tupleDesc;    /* Tuple descripton */

     float dataArray[2] = { 1.1f, 3.1e-16f };
     ArrayType * arrayType;
     Datum *datum;
     int16 typlen;
     bool typbyval;
     char typalign;

     datum = (Datum *)palloc(sizeof(Datum) * 2);

     datum[0] =  Float4GetDatum(dataArray[0]);
     datum[1] =  Float4GetDatum(dataArray[1]);

     get_typlenbyvalalign(FLOAT4OID, &typlen, &typbyval, &typalign);
     arrayType =  construct_array(datum, 2, FLOAT4OID,  typlen, 
typbyval, typalign);

     /* Create and bless the template */
     tupleDesc = CreateTemplateTupleDesc(1, false);

     TupleDescInitEntry(tupleDesc, (AttrNumber) 1, "Param_1", 
FLOAT4ARRAYOID, -1, 0);


     tupleDesc = BlessTupleDesc(tupleDesc);

     /**  Set up the column Datum for the Tuple **/
     column[0] = DatumGetPointer(arrayType);



         /**  Build the tuple **/
     tuple = heap_form_tuple(tupleDesc, column, isNULL);
     results = HeapTupleGetDatum(tuple);

     PG_RETURN_DATUM(results);

}









Responses

pgsql-novice by date

Next:From: Tom LaneDate: 2010-12-05 20:43:19
Subject: Re: C Function returning a tuple with a float4 array as column
Previous:From: Marco CraveiroDate: 2010-12-05 17:34:47
Subject: Re: Understanding the behaviour of hostname in psql

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group