pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomasz Myrta <jasiek@klaster.net>
To: Lista dyskusyjna pgsql-sql <pgsql-sql@postgresql.org>
Subject: pl/pgsql, cursors and C function
Date: Tue, 23 Sep 2003 13:41:00 +0200
Message-ID: <3F70314C.30507@klaster.net> (raw)

Hi
I'm making my first steps in C functions. I want to avoid doing all the 
SQL job in them, pl/pgsql looks a better choice. I tried to do this by 
passing opened cursor from pl/pgsql function to C function.

Here is simple C function:
#include <server/postgres.h>
#include <server/executor/spi.h>
PG_FUNCTION_INFO_V1(test2);
Datum test2(PG_FUNCTION_ARGS)
{
   Portal p;
   int n;
   p=SPI_cursor_find("xxx");
   if(!p)
     elog(ERROR,"Cursor error");
   SPI_cursor_fetch(p,true,1);
   n=SPI_processed;
   PG_RETURN_INT32(n);
}

And pl/pgsql one:
CREATE OR REPLACE FUNCTION test() returns integer AS '
DECLARE
   _m CURSOR FOR select id from some_table limit 1;
   n       integer;
BEGIN
   _m=''xxx'';
   open _m;
   n=test2();
   close _m;
   return n;
END;
' language 'plpgsql';

select test();
I don't understand ERROR message at all:
  ERROR:  SPI_prepare() failed on "SELECT   $1 "

This error is raised when trying to execute SPI_cursor_fetch. What does 
it mean? What does the SPI_prepare have to already opened cursor?
Where can I find better SPI documentation than "Postgresql Server 
Programming" ?

Regards,
Tomasz Myrta




view thread (4+ messages)  latest in thread

Message-ID: <3F70314C.30507@klaster.net>
Permalink:  ../3F70314C.30507@klaster.net/
Also on:    postgresql.org/message-id/3F70314C.30507@klaster.net

 · 

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-sql@postgresql.org
  Cc: jasiek@klaster.net
  Subject: Re: pl/pgsql, cursors and C function
  In-Reply-To: <3F70314C.30507@klaster.net>

* 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