<vivek(at)staff(dot)ownmail(dot)com> writes: > i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0], tupdesc, 1, &isnull)); I think this is your problem: you're trying to use the wrong tupdesc to retrieve from the SELECT count(*) result. SPI_tuptable->tupdesc would work better there. regards, tom lane