pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pl/pgsql, cursors and C function
4+ messages / 2 participants
[nested] [flat]

* pl/pgsql, cursors and C function
@ 2003-09-23 11:41  Tomasz Myrta <jasiek@klaster.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Tomasz Myrta @ 2003-09-23 11:41 UTC (permalink / raw)
  To: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: pl/pgsql, cursors and C function
@ 2003-09-23 15:15  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tomasz Myrta <jasiek@klaster.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2003-09-23 15:15 UTC (permalink / raw)
  To: Tomasz Myrta <jasiek@klaster.net>; +Cc: pgsql-sql

Tomasz Myrta <jasiek@klaster.net> writes:
> I don't understand ERROR message at all:
>   ERROR:  SPI_prepare() failed on "SELECT   $1 "

This is a bug.  Or two bugs, actually: one of yours and one of PG's.

I have repaired the PG bug with the attached patch.  The bug in your
code is that your C function needs to call SPI_connect and SPI_finish
if it's going to use any SPI operations.

			regards, tom lane

inline patch 1: spi.c (+2/-1 lines) download inline-1.patchIndex: spi.c =================================================================== RCS file: /cvsroot/pgsql-server/src/backend/executor/spi.c,v retrieving revision 1.75.2.3 diff -c -r1.75.2.3 spi.c *** spi.c 14 Feb 2003 21:12:54 -0000 1.75.2.3 --- spi.c 23 Sep 2003 15:09:39 -0000 *************** *** 1387,1393 **** elog(ERROR, "invalid portal in SPI cursor operation"); /* Push the SPI stack */ ! _SPI_begin_call(true); /* Reset the SPI result */ SPI_processed = 0; --- 1387,1394 ---- elog(ERROR, "invalid portal in SPI cursor operation"); /* Push the SPI stack */ ! if (_SPI_begin_call(true) < 0) ! elog(ERROR, "SPI cursor operation called while not connected"); /* Reset the SPI result */ SPI_processed = 0;
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: pl/pgsql, cursors and C function
@ 2003-09-23 16:43  Tomasz Myrta <jasiek@klaster.net>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Tomasz Myrta @ 2003-09-23 16:43 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-sql

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

>  > This is a bug.  Or two bugs, actually: one of yours and one of PG's.
> I have repaired the PG bug with the attached patch.  The bug in your
> code is that your C function needs to call SPI_connect and SPI_finish
> if it's going to use any SPI operations.
> 
> 			regards, tom lane

Thanks a lot. The first answer helped me, the second one - the others.

Do you know anything about good source of C functions documentation and 
examples? Currently I'm walking over .h headers to find some useful 
functions and macros.


Regads,
Tomasz Myrta




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: pl/pgsql, cursors and C function
@ 2003-09-23 17:18  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Tomasz Myrta <jasiek@klaster.net>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2003-09-23 17:18 UTC (permalink / raw)
  To: Tomasz Myrta <jasiek@klaster.net>; +Cc: pgsql-sql

Tomasz Myrta <jasiek@klaster.net> writes:
> Do you know anything about good source of C functions documentation and 
> examples?

Look in the main sources (backend/utils/adt/, mostly) and/or contrib
modules for functions that do something like what you need.  The only
difference between a builtin function and a dynamically loaded one is
you have to add the PG_FUNCTION_INFO macro.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2003-09-23 17:18 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-09-23 11:41 pl/pgsql, cursors and C function Tomasz Myrta <jasiek@klaster.net>
2003-09-23 15:15 ` Tom Lane <tgl@sss.pgh.pa.us>
2003-09-23 16:43   ` Tomasz Myrta <jasiek@klaster.net>
2003-09-23 17:18     ` Tom Lane <tgl@sss.pgh.pa.us>

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