Hi, I am learning how to use the c functions and my function below works
when I do:
select testgetrows();
but when I do select * from testgetrows(); I am getting:
"ERROR:  cache lookup failed for type 0"
Whats's the problem?
10x.

drop function testgetrows();
CREATE OR REPLACE FUNCTION testgetrows() RETURNS SETOF my_first_table
AS 'foo6', 'testgetrows'
LANGUAGE C IMMUTABLE STRICT;

#include "postgres.h"
#include <string.h>
#include <array.h>
#include "fmgr.h"
#include "funcapi.h"
#include "access/heapam.h" 

typedef struct
{
  HeapScanDesc scan;
  Relation      lRel; 
} testgetrows_fctx;

PG_FUNCTION_INFO_V1(testgetrows);

Datum
testgetrows(PG_FUNCTION_ARGS)
{
    FuncCallContext     *funcctx;
    testgetrows_fctx *fctx; 
    if (SRF_IS_FIRSTCALL())
     {
        MemoryContext   oldcontext;
        
        funcctx = SRF_FIRSTCALL_INIT();
        oldcontext =
MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
        fctx = (testgetrows_fctx *) palloc(sizeof(testgetrows_fctx)); 
        
        fctx->lRel = heap_open(17236, AccessShareLock);
        fctx->scan = heap_beginscan(fctx->lRel, SnapshotNow, 0, NULL);
        funcctx->user_fctx = fctx; 
        MemoryContextSwitchTo(oldcontext);
    }

    funcctx = SRF_PERCALL_SETUP();
    fctx = funcctx->user_fctx;
    HeapTuple    tuple;
    tuple = heap_getnext(fctx->scan, ForwardScanDirection);
    if (HeapTupleIsValid(tuple))
    { 
      Datum        result; 
      result = HeapTupleGetDatum(tuple);
      SRF_RETURN_NEXT(funcctx, result);
    }
    else    /* do when there is no more left */
    {
      heap_endscan(fctx->scan);
      heap_close(fctx->lRel, AccessShareLock);
      SRF_RETURN_DONE(funcctx);
    }
}

Regards,
        tzahi.




---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to