Re: Postgres delays function returning large set of data

From: flippo00110001 <ddevec(at)umich(dot)edu>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Postgres delays function returning large set of data
Date: 2009-06-05 17:03:57
Message-ID: 23891972.post@talk.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Pavel Stehule wrote:
>
> Hello
>
> can you send source code? There are two types of C SRF functions. One
> returns row ro by row, second store returned rows and returns block.
> What did you use?
>

I had originally used the style that returns row by row, but after reading
the page i created a new function which returns a block. This version runs
slightly faster (12.5 seconds to run my test case) but it is still far
slower than expected.

A few notes on the code:
getdata function returns an array with length 2*size, the first size
elements are one colum the other size elements are the next column.
I have timed the call getdata and determined it consumes on average around
30ms of my test case's run time.

<code>
PG_FUNCTION_INFO_V1(getTableFastHDF5);

Datum
getTableFastHDF5(PG_FUNCTION_ARGS)
{
/*{{{*/
/* Locals */
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
FuncCallContext *fcc;
TupleDesc tupdesc;
Tuplestorestate *tupstore;
MemoryContext per_query_ctx;
MemoryContext oldcontext;
AttInMetadata *attinmeta;
int *data;
int size;
int i;

if (!connections)
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("No
connections open, use openHDF5 to open a file first")));

data = getdata(textToStr(PG_GETARG_TEXT_P(0)), PG_GETARG_INT32(1),
PG_GETARG_INT32(2), &size, TEMPORARY_CONTEXT);

per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
oldcontext = MemoryContextSwitchTo(per_query_ctx);

tupdesc = rsinfo->expectedDesc;
tupstore = tuplestore_begin_heap(true, false, SortMem);

if (data == NULL)
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Could not
find specified data in file")));

for (i = 0; i < size; i++)
{
/*{{{*/
Datum val[2];
bool nulls[2];
HeapTuple tup;

MemoryContextSwitchTo(oldcontext);

MemSet(nulls, false, 2 * sizeof(bool));

/* fill strings to be turned into tuple */
val[0] = Int32GetDatum(data[i]);
val[1] = Int32GetDatum(data[i + size]);

/* make tuple */
tup = heap_form_tuple(tupdesc, val, nulls);

/* make tuple to datum so it can be returned */
MemoryContextSwitchTo(per_query_ctx);
tuplestore_puttuple(tupstore, tup);

/* return, but there is more to send */
/*}}}*/
}
/* return and finish sending */

tuplestore_donestoring(tupstore);

MemoryContextSwitchTo(oldcontext);

rsinfo->returnMode = SFRM_Materialize;
rsinfo->setResult = tupstore;
rsinfo->setDesc = tupdesc;

return (Datum) 0;

/*}}}*/
}
</code>

--
View this message in context: http://www.nabble.com/Postgres-delays-function-returning-large-set-of-data-tp23853886p23891972.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Fetter 2009-06-05 17:11:53 movavg
Previous Message Bruce Momjian 2009-06-05 16:44:11 pg_migrator mention in documentation