pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
C Stored Function with ECPG
3+ messages / 3 participants
[nested] [flat]

* C Stored Function with ECPG
@ 2009-02-24 01:04 Bob Henkel <bob.henkel@gmail.com>
  2009-02-24 01:47 ` Re: C Stored Function with ECPG Tom Lane <tgl@sss.pgh.pa.us>
  2009-02-25 15:54 ` Re: C Stored Function with ECPG Michael Meskes <meskes@postgresql.org>
  0 siblings, 2 replies; 3+ messages in thread

From: Bob Henkel @ 2009-02-24 01:04 UTC (permalink / raw)
  To: pgsql-interfaces

Sorry if this double posts. My first attempt was stalled because I was
not a member of the list. So now I am.

I'm starting to experiment with writing some stored functions in C and
find it really handy.  I have a simple C function that takes two
integers and returns the sum.  This works fine. Now I want to create a
function that makes us of some embedded ECPG. So far I have failed at
my first attempt. Below are the steps and outputs of what I did.  I
assume it's feasible to embedd ECPG in a Postgres Stored function
written in C?

My foo1.pcg file looks like this.
#include "postgres.h"
#include  <string.h>
#include "fmgr.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif


PG_FUNCTION_INFO_V1(foo1);

Datum foo1(PG_FUNCTION_ARGS)
{
   EXEC SQL CREATE TABLE foo1 (c1 integer NOT NULL, c2 varchar(50) NOT NULL);

   PG_RETURN_INT32(0);
}

1. I run ecpg foo1.pcg and this generates my foo1.c file. I get no
warnings or errors.
[postgres@PortJackson ~]$ ecpg foo1.pgc
[postgres@PortJackson ~]$

2. I compile foo1.c and get some warnings.  Are these warnings ok?
[postgres@PortJackson ~]$  gcc -g -fpic -c foo1.c
-I/usr/home/postgres/postgresql-8.3.5/src/include/
-I/home/postgres/postgresql-8.3.5/src/include/libpq/
-I/usr/home/postgres/postgresql-8.3.5/src/interfaces/ecpg/include/
-I/usr/local/pgsql/include/
foo1.pgc: In function 'foo1':
foo1.pgc:14: warning: passing argument 5 of 'ECPGdo' makes integer
from pointer without a cast
foo1.pgc:14: warning: passing argument 7 of 'ECPGdo' makes pointer
from integer without a cast
[postgres@PortJackson ~]$

3.I link foo1. With now warnings or errors.
[postgres@PortJackson ~]$ gcc -shared -o foo1.so foo1.o
-L/usr/local/pgsql/lib -lecpg
[postgres@PortJackson ~]$



4. I create a function in psql to call my new library. And get a could
not load library error for file libecpg.so.6.  How do I fix this
error?
dev=# CREATE OR REPLACE FUNCTION foo1(integer)
 RETURNS integer AS
'/usr/home/postgres/foo1', 'foo1'
 LANGUAGE 'c' VOLATILE STRICT
 COST 1;
ERROR:  could not load library "/usr/home/postgres/foo1.so": dlopen
(/usr/home/postgres/foo1.so) failed: Shared object "libecpg.so.6" not
found, required by "foo1.so"
dev=#

I'm new to C functions and to ECPG. So mixing the two at this point
for me is probably asking for trouble.  Any help would be much
appreciated.

Thanks
Bob



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

* Re: C Stored Function with ECPG
  2009-02-24 01:04 C Stored Function with ECPG Bob Henkel <bob.henkel@gmail.com>
@ 2009-02-24 01:47 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2009-02-24 01:47 UTC (permalink / raw)
  To: Bob Henkel <bob.henkel@gmail.com>; +Cc: pgsql-interfaces

Bob Henkel <bob.henkel@gmail.com> writes:
> I assume it's feasible to embedd ECPG in a Postgres Stored function
> written in C?

No, I don't think so; or at least that's not the expected usage.  ECPG
expects to connect to an external server.  The normal way to issue
SQL from C-level backend code is to use the SPI interface:
http://www.postgresql.org/docs/8.3/static/spi.html

			regards, tom lane



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

* Re: C Stored Function with ECPG
  2009-02-24 01:04 C Stored Function with ECPG Bob Henkel <bob.henkel@gmail.com>
@ 2009-02-25 15:54 ` Michael Meskes <meskes@postgresql.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Michael Meskes @ 2009-02-25 15:54 UTC (permalink / raw)
  To: Bob Henkel <bob.henkel@gmail.com>; +Cc: pgsql-interfaces

On Mon, Feb 23, 2009 at 07:04:04PM -0600, Bob Henkel wrote:
> assume it's feasible to embedd ECPG in a Postgres Stored function
> written in C?

As Tom already pointed out it is not. I always played with the idea of
implementing an additional ecpg interface based on spi instead of libpq but
never started working on it. Sorry.

Michael
-- 
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
ICQ: 179140304, AIM/Yahoo: michaelmeskes, Jabber: meskes@jabber.org
Go VfL Borussia! Go SF 49ers! Use Debian GNU/Linux! Use PostgreSQL!



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


end of thread, other threads:[~2009-02-25 15:54 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-02-24 01:04 C Stored Function with ECPG Bob Henkel <bob.henkel@gmail.com>
2009-02-24 01:47 ` Tom Lane <tgl@sss.pgh.pa.us>
2009-02-25 15:54 ` Michael Meskes <meskes@postgresql.org>

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