Search Postgresql Archives

Problem with heap_form_tuple error

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I'm have a problem with heap_form_tuple error
   ERROR:  invalid memory alloc request size 1149502660

I've read through a ton of examples and read through the code and the docs and I'm sure I'm doing something stupid but I'm not seeing it. I have included a very reduced sample of the code below trying to focus on the relevant parts.

1. how I get my return tuple_desc
2. how I prepare my values and nulls arrays

I am totally stuck on this! So I really need some help to point out the error of my ways here.

Thanks,
  -Steve

    if (SRF_IS_FIRSTCALL()) {
        MemoryContext   oldcontext;

        /* create a function context for cross-call persistence */
        funcctx = SRF_FIRSTCALL_INIT();

        /* create a function context for cross-call persistence */
        oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);

        ...

if (get_call_result_type( fcinfo, NULL, &tuple_desc ) != TYPEFUNC_COMPOSITE ) {
            elog(ERROR, "function returning record called in context"
                " that cannot accept type record");
            std_free(std);
            return finish(-1);
        }
        /* since this is a defined composite type,
           I probably do not need to bless it,
           but the example in the docs said to */
        BlessTupleDesc(tuple_desc);
        /* save this for later usage also */
        funcctx->tuple_desc = tuple_desc;

        MemoryContextSwitchTo(oldcontext);

        DBG("finished first call setup");
    }

    funcctx = SRF_PERCALL_SETUP();

    call_cntr  = funcctx->call_cntr;
    max_calls  = funcctx->max_calls;
    tuple_desc = funcctx->tuple_desc;  /* the result record description */
    fctx       = (std_fctx *) funcctx->user_fctx;
    std        = fctx->std;

    ...

    if (call_cntr < max_calls) { /* do while there are more to send */
        Datum *values;
        bool *nulls;
        int id;
        int k;

        HeapTuple addr_tuple = spi_tuptable->vals[call_cntr];

        ...

        values = (Datum *) palloc(17 * sizeof(Datum));
        nulls = (bool *) palloc(17 * sizeof(bool));
        for (k=0; k<17; k++) {
            values[k] = (Datum) 0;
            nulls[k] = true;
        }
        values[0] = Int32GetDatum(id);
        nulls[0] = false;
        if (stdaddr->building) {
            DBG("building %d", strlen(stdaddr->building));
            values[1] = CStringGetDatum(pstrdup(stdaddr->building));
            nulls[1] = false;
        }
        ... more if blocks for values[2..15] ...
        if (stdaddr->unit) {
            DBG("unit %d", strlen(stdaddr->unit));
            values[16] = CStringGetDatum(pstrdup(stdaddr->unit));
            nulls[16] = false;
        }
        HeapTuple tuple = heap_form_tuple(tuple_desc, values, nulls);
        result = HeapTupleGetDatum(tuple);
        SRF_RETURN_NEXT(funcctx, result);
    }
    ...

when this runs DBG logs the following:

NOTICE:  city='NORTH CHELMSFORD', state='MASSACHUSETTS', postcode='01863'
NOTICE:  setup values and nulls array for natts=17
NOTICE:  house_num 2
NOTICE:  name 8
NOTICE:  suftype 4
NOTICE:  city 16
NOTICE:  state 13
NOTICE:  postcode 5
NOTICE:  calling heap_form_tuple
ERROR:  invalid memory alloc request size 1149502660

the stdaddr fields all have reasonable null terminated CStrings in them.


--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux