Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
Thread:
Lists: pgsql-bugspgsql-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

pgsql-bugs by date

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

pgsql-interfaces by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group