Re: can external C-function get multiple rows?

From: Alexey Nalbat <alexey(at)price(dot)ru>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: can external C-function get multiple rows?
Date: 2001-04-27 12:50:21
Message-ID: 01042718092204.12343@workshop.price.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-interfaces

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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2001-04-27 12:57:17 Re: Optimalisation options change query results
Previous Message pgsql-bugs 2001-04-27 11:29:33 Input/Output of byte[]-Fields with 'FF' values in LargeObject with JDBC

Browse pgsql-interfaces by date

  From Date Subject
Next Message Michael Ansley (UK) 2001-04-27 15:05:19 RE: VB and ODBC
Previous Message Graham Vickrage 2001-04-27 11:17:52 VB and ODBC