Trying to make functions in 'C'

Started by fabian baenaalmost 27 years ago7 messagesgeneral
Jump to latest
#1fabian baena
fabbaena@hotmail.com

I'm tying to learn how to make function in 'C'.
I'm trying to compile a program I made in 'C'.
The command I gave were:

% gcc -fPIC -c addone.c -I/usr/local/pgsql/include
% ld -G -Bdynamic -o addone.so addone.o

and created the function in postgres like this:

CREATE FUNCTION add_one(int4) RETURNS int4
AS './addone.so' LANGUAGE 'c';

The function I made looks like this:

#include "postgres.h"

int
add_one(int arg)
{
return arg + 1;
}

But when I try to test the function in postgres I recieve this error:

ERROR: stat failed on file ./addone.so
ERROR: stat failed on file ./addone.so
Can anyone tell where did I make a mistake?
I have a linux on a PC.

Thank you.

Fabian.

______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

#2Gene Selkov, Jr.
selkovjr@mcs.anl.gov
In reply to: fabian baena (#1)
Re: [GENERAL] Trying to make functions in 'C'

I'm tying to learn how to make function in 'C'.
I'm trying to compile a program I made in 'C'.
The command I gave were:

% gcc -fPIC -c addone.c -I/usr/local/pgsql/include
% ld -G -Bdynamic -o addone.so addone.o

and created the function in postgres like this:

CREATE FUNCTION add_one(int4) RETURNS int4
AS './addone.so' LANGUAGE 'c';

^^^^^^^^^^^^^
one must provide an absolute path here

--Gene

#3Noname
tolik@icomm.ru
In reply to: fabian baena (#1)
Re: [GENERAL] Trying to make functions in 'C'

"fb" == fabian baena <fabbaena@hotmail.com> writes:

fb> I'm tying to learn how to make function in 'C'.
fb> I'm trying to compile a program I made in 'C'.
fb> The command I gave were:

fb> % gcc -fPIC -c addone.c -I/usr/local/pgsql/include
fb> % ld -G -Bdynamic -o addone.so addone.o

fb> and created the function in postgres like this:

fb> CREATE FUNCTION add_one(int4) RETURNS int4
fb> AS './addone.so' LANGUAGE 'c';

fb> The function I made looks like this:

fb> #include "postgres.h"

fb> int
fb> add_one(int arg)
fb> {
fb> return arg + 1;
fb> }

fb> But when I try to test the function in postgres I recieve this error:

fb> ERROR: stat failed on file ./addone.so
fb> ERROR: stat failed on file ./addone.so
fb> Can anyone tell where did I make a mistake?
fb> I have a linux on a PC.

Pehaps not 'int', but 'int4' in argument and return type? Except this
I think your example is right.

--
Anatoly K. Lasareff Email: tolik@icomm.ru
Senior programmer

#4Noname
selkovjr.mcs.anl.gov@mcs.anl.gov
In reply to: Noname (#3)
Re: [GENERAL] Trying to make functions in 'C'

"fb" == fabian baena <fabbaena@hotmail.com> writes:

fb> I'm tying to learn how to make function in 'C'.
fb> I'm trying to compile a program I made in 'C'.
fb> The function I made looks like this:

fb> #include "postgres.h"

fb> int
fb> add_one(int arg)
fb> {
fb> return arg + 1;
fb> }

Pehaps not 'int', but 'int4' in argument and return type? Except this
I think your example is right.

As far as return types, make sure you get what you want by
checking how types are typedef'ed in postgres.h

The safest way to add your own code would be what Fabian is trying to
accomplish -- by using the prototypes from tutorial.

--Gene

#5Jonathan davis
haj@idianet.net
In reply to: Gene Selkov, Jr. (#2)
problem with LOAD

hello every body

i have some problem with LOAD command

postgres=> LOAD '/usr/local/pgsql/complex.so';
pqReadData() -- backend closed the channel unexpectedly.
This probably means the backend terminated abnormally before or while
processing the request.
We have lost the connection to the backend, so further processing is
impossible. Terminating.
$>>

I build complex.so by:

gcc -I../include -I../backend -O2 -m486 -pipe -Wall

-Wmissing-prototypes -I../interfaces/libpq -I../../include -c
complex.c -o complex.o

ld -x -r -o complex.o.obj complex.o
ranlib complex.so.pic
ld -x -Bshareable -o complex.so complex.so.pic

I use FreeBSD-3.1 with PostgreSQL 6.4.2

thanks

#6Jonathan davis
haj@idianet.net
In reply to: fabian baena (#1)
Re: [GENERAL] Trying to make functions in 'C'

hello

CREATE SEQUENCE seq

INCREMENT 1
MINVALUE 1
START 1
;

CREATE TABLE T (
index int primary key UNIQUE DEFAULT nextval('seq'),
f1 char(5),
f2 char(5)

);

CREATE TABLE T1 (
f3 char(5),
f4 char(5),
f5 char(5)

) INHERITS (T);

CREATE TABLE T2(
cp1 char(5),
cp2 char(5)
) INHERITS (T);

INSERT INTO T1 VALUES ( nextval('seq'), 'joe', 'joe', 'joe', 'joe', 'joe');
INSERT INTO T1 VALUES ( nextval('seq'), 'davis', 'davis', 'davis', 'davis',
'davis');
INSERT INTO T1 VALUES ( nextval('seq'), 'ba', 'ba', 'ba', 'ba', 'ba');

INSERT INTO T2 VALUES ( nextval('seq'), 'aw', 'aw', 'aw', 'aw');
INSERT INTO T2 VALUES ( nextval('seq'), 'es', 'es', 'es', 'es');

SELECT * FROM T*;

this request give me only the fields in table T but I want to give me all
fiels in T1 and T2
how ?

thanks

#7Jonathan davis
haj@idianet.net
In reply to: fabian baena (#1)
pb with select

Jonathan Davis wrote:

Show quoted text

hello

CREATE SEQUENCE seq

INCREMENT 1
MINVALUE 1
START 1
;

CREATE TABLE T (
index int primary key UNIQUE DEFAULT nextval('seq'),
f1 char(5),
f2 char(5)

);

CREATE TABLE T1 (
f3 char(5),
f4 char(5),
f5 char(5)

) INHERITS (T);

CREATE TABLE T2(
cp1 char(5),
cp2 char(5)
) INHERITS (T);

INSERT INTO T1 VALUES ( nextval('seq'), 'joe', 'joe', 'joe', 'joe', 'joe');
INSERT INTO T1 VALUES ( nextval('seq'), 'davis', 'davis', 'davis', 'davis',
'davis');
INSERT INTO T1 VALUES ( nextval('seq'), 'ba', 'ba', 'ba', 'ba', 'ba');

INSERT INTO T2 VALUES ( nextval('seq'), 'aw', 'aw', 'aw', 'aw');
INSERT INTO T2 VALUES ( nextval('seq'), 'es', 'es', 'es', 'es');

SELECT * FROM T*;

this request give me only the fields in table T but I want to give me all
fiels in T1 and T2
how ?

thanks