pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Nalbat <alexey@price.ru>
To: pgsql-interfaces@postgresql.org
Subject: Re: can external C-function get multiple rows?
Date: Fri, 27 Apr 2001 16:50:21 +0400
Message-ID: <01042718092204.12343@workshop.price.ru> (raw)
In-Reply-To: <16102.988296940@sss.pgh.pa.us>
References: <01042420581800.12729@workshop.price.ru>
	<16102.988296940@sss.pgh.pa.us>

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



view thread (6+ messages)  latest in thread

Message-ID: <01042718092204.12343@workshop.price.ru>
Permalink:  ../01042718092204.12343@workshop.price.ru/
Also on:    postgresql.org/message-id/01042718092204.12343@workshop.price.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: alexey@price.ru
  Subject: Re: can external C-function get multiple rows?
  In-Reply-To: <01042718092204.12343@workshop.price.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox