Re: can external C-function get multiple rows?

Alexey Nalbat <alexey@price.ru>

From: Alexey Nalbat <alexey@price.ru>
To: pgsql-interfaces@postgresql.org
Date: 2001-04-27T12:50:21Z
Lists: pgsql-bugs
Tom, thank you very much.

I was succeeded in constructing such a test function. And now have another question.
I wrote function myarr(foo) which returns exactly 10 rows of random values in the range [0,foo).

But I also want this function to work correctly, when used in a query with limit clause, like
"select myarr(100) limit 6;". After a bit of experiments I supposed that while executing
this query postgres called myarr() seven times (not six!). And may be at the seven call
fcinfo has some_flag set to "stop_return", after checking which myarr() should do the same
as when returning 11's row with PG_RETURN_NULL and setting "isDone" to "ExprEndResult"
and resetting variables. Is it so? What are some_flag and "stop_return"?

Thanks in advance.

P.S.:

Now myarr() does not reset variables when "interrupted by limit", and because of this returns:

+++
+++

pl=# select myarr(100) limit 6;
 ?column? 
----------
       87
       42
       35
       38
        4
       16
(6 rows)

pl=# select myarr(100) limit 6;
 ?column? 
----------
       69
        9
       40
(3 rows)

+++
+++

Here is myarr() code:

+++
+++

#include <stdlib.h>
#include "postgres.h"
#include "fmgr.h"
#include "nodes/execnodes.h"

#define N 10

int     a_c[N];
int     n_c=0;
int     i_c=0;

PG_FUNCTION_INFO_V1(myarr);

Datum
myarr(PG_FUNCTION_ARGS)
{
        int n=PG_GETARG_INT32(0);

        if ( n_c!=n )
        {
                int j;

                n_c=n;
                i_c=0;

                for ( j=0 ; j<N ; j++ )
                {
                        a_c[j]=n_c*rand()/RAND_MAX;
                }
        }

        if ( i_c<N )
        {
                i_c++;

                ((ReturnSetInfo*)fcinfo->resultinfo)->isDone=ExprMultipleResult;
                PG_RETURN_INT32(a_c[i_c-1]);
        }
        else
        {
                n_c=0;
                i_c=0;

                ((ReturnSetInfo*)fcinfo->resultinfo)->isDone=ExprEndResult;
                PG_RETURN_NULL();
        }
}

+++
+++

-- 

WBR, Alexey Nalbat