Re: ERROR: cache lookup failed for type 0

Lists: pgsql-general
From: Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: ERROR: cache lookup failed for type 0
Date: 2005-01-07 18:35:53
Message-ID: 017101c4f4e7$be262490$0b00a8c0@llord
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


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.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for type 0
Date: 2005-01-07 18:59:09
Message-ID: 28419.1105124349@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com> writes:
> 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?

I don't think it's safe to do HeapTupleGetDatum() directly on a tuple
obtained from heap_getnext. You need to copy it.

regards, tom lane


From: Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com>
To: 'Tom Lane' <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for type 0
Date: 2005-01-07 19:51:30
Message-ID: 017c01c4f4f2$4e531ff0$0b00a8c0@llord
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

It still doesn't work. btw, I am using 8rc2.
changed it to:
Datum result;
HeapTuple tupleCopy;
tupleCopy = heap_copytuple(tuple);
result = HeapTupleGetDatum(tupleCopy);
SRF_RETURN_NEXT(funcctx, result);

its probably something with the column description. the rows are
returned ok with select testgetrows();
but not with select * from testgetrows();

Regards,
tzahi.

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org
> [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Tom Lane
> Sent: Friday, January 07, 2005 8:59 PM
> To: Tzahi Fadida
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] ERROR: cache lookup failed for type 0
>
>
> Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com> writes:
> > 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?
>
> I don't think it's safe to do HeapTupleGetDatum() directly on
> a tuple obtained from heap_getnext. You need to copy it.
>
> regards, tom lane
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
> majordomo(at)postgresql(dot)org
>
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for type 0
Date: 2005-01-07 20:31:07
Message-ID: 29191.1105129867@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com> writes:
> It still doesn't work. btw, I am using 8rc2.

Um. The "clean" way to do this is to use BlessTupleDesc and then
heap_formtuple. That requires you to break down the original tuple
into fields (see heap_deformtuple). Alternatively you could poke
the datatype ID fields directly into the copied tuple.

regards, tom lane


From: Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com>
To: 'Tom Lane' <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for type 0
Date: 2005-01-07 22:01:02
Message-ID: 018501c4f504$63ef7bd0$0b00a8c0@llord
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

well, I tried the heap_deformtuple and I am getting now:
select testgetrows();
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!>

btw, as can be seen below I tried two kinds of tupledesc just in case
with the same results.
/*funcctx->tuple_desc= BlessTupleDesc(fctx->lRel->rd_att);*/
funcctx->tuple_desc=BlessTupleDesc(RelationNameGetTupleDesc("my_first_ta
ble"));

#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->tuple_desc= BlessTupleDesc(fctx->lRel->rd_att);*/

funcctx->tuple_desc=BlessTupleDesc(RelationNameGetTupleDesc("my_first_ta
ble"));

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;
Datum *values;
HeapTuple tupleCopy;
HeapTuple tupleCopy2;
char *nulls = (char *)palloc(funcctx->tuple_desc->natts *
sizeof(char));

tupleCopy = heap_copytuple(tuple);
heap_deformtuple(tuple,funcctx->tuple_desc,values,nulls);
tupleCopy2 = heap_formtuple(funcctx->tuple_desc,values,nulls);
result = HeapTupleGetDatum(tupleCopy2);

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.

> -----Original Message-----
> From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
> Sent: Friday, January 07, 2005 10:31 PM
> To: Tzahi Fadida
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] ERROR: cache lookup failed for type 0
>
>
> Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com> writes:
> > It still doesn't work. btw, I am using 8rc2.
>
> Um. The "clean" way to do this is to use BlessTupleDesc and
> then heap_formtuple. That requires you to break down the
> original tuple into fields (see heap_deformtuple).
> Alternatively you could poke the datatype ID fields directly
> into the copied tuple.
>
> regards, tom lane
>
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for type 0
Date: 2005-01-07 22:23:44
Message-ID: 421.1105136624@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com> writes:
> well, I tried the heap_deformtuple and I am getting now:
> select testgetrows();
> server closed the connection unexpectedly

You didn't palloc the values array. Any reasonable compiler would have
warned you about that BTW. If you don't have compiler warnings enabled,
learn to use them.

Also, I'd recommend using the tupledesc from the just-opened lRel;
fetching it via an independent path is just asking for trouble.

regards, tom lane


From: Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com>
To: 'Tom Lane' <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: returning a setof tuples like a subquery was(RE: ERROR: cache lookup failed for type 0 )
Date: 2005-01-07 22:56:52
Message-ID: 019c01c4f50c$30bfdef0$0b00a8c0@llord
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

yes you were right it works now.
10x, sorry for the rookie mistake I jumped the gun on the last one.
If you will i have another question on the same subject.
I want to return a setof tuples without a predetermined return type
like a subquery with a join, I don't want to specify a type.
for example, new_join_alg_func(table1,table2).
when I use SETOF RECORD I have to specify a type, same goes
for anyelement.

Regards,
tzahi.

> -----Original Message-----
> From: pgsql-general-owner(at)postgresql(dot)org
> [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Tom Lane
> Sent: Saturday, January 08, 2005 12:24 AM
> To: Tzahi Fadida
> Cc: pgsql-general(at)postgresql(dot)org
> Subject: Re: [GENERAL] ERROR: cache lookup failed for type 0
>
>
> Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com> writes:
> > well, I tried the heap_deformtuple and I am getting now: select
> > testgetrows(); server closed the connection unexpectedly
>
> You didn't palloc the values array. Any reasonable compiler
> would have warned you about that BTW. If you don't have
> compiler warnings enabled, learn to use them.
>
> Also, I'd recommend using the tupledesc from the just-opened
> lRel; fetching it via an independent path is just asking for trouble.
>
> regards, tom lane
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>
>


From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for type 0
Date: 2005-01-08 06:17:36
Message-ID: 874qhs4f7j.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:

> Tzahi Fadida <tzahi_ml(at)myrealbox(dot)com> writes:
> > well, I tried the heap_deformtuple and I am getting now:
> > select testgetrows();
> > server closed the connection unexpectedly
>
> You didn't palloc the values array. Any reasonable compiler would have
> warned you about that BTW. If you don't have compiler warnings enabled,
> learn to use them.

I think with gcc this type of warning is only enabled when you're compiling
with optimizations. Most people don't compile with optimizations enabled when
developing.

--
greg


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Greg Stark <gsstark(at)mit(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: ERROR: cache lookup failed for type 0
Date: 2005-01-08 17:56:45
Message-ID: 6841.1105207005@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Greg Stark <gsstark(at)mit(dot)edu> writes:
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:
>> You didn't palloc the values array. Any reasonable compiler would have
>> warned you about that BTW. If you don't have compiler warnings enabled,
>> learn to use them.

> I think with gcc this type of warning is only enabled when you're compiling
> with optimizations. Most people don't compile with optimizations enabled when
> developing.

Pretty much the first thing you learn when developing with gcc is to use
-O1 -Wall for compiling devel code. Gets all the warnings and doesn't
confuse gdb too badly. Once in a long while I'll recompile an
individual file with -O0 so that I can single-step through it more
easily, but 99.44% of the time I'd rather have the
uninitialized-variable warning.

regards, tom lane