Thread: Dynamic loading of C functions

Dynamic loading of C functions

From
"Jasbinder Bali"
Date:
I've written a function in C, compiled it and trying to use the same function in one of my postgres functions like this:

CREATE FUNCTION add_one(integer) RETURNS integer
     AS '/usr/include/pgsql/server/test_func, 'add_one'
     LANGUAGE C STRICT

test_func is the name of my object file and add_one is the name of
the function i want to call from test_func.c  C file.

I get the follwing error
ERROR:  could not access file "/usr/include/pgsql/server/test_func": No such file or directory

/usr/include/pgsql/server/ is exactly the path where test_func object file resides.
Don't know why isn't postgres able to find it there.

Any kind of help would be appreciated.

~Jas

Re: Dynamic loading of C functions

From
Bill Moran
Date:
In response to "Jasbinder Bali" <jsbali@gmail.com>:

> I've written a function in C, compiled it and trying to use the same
> function in one of my postgres functions like this:
>
> CREATE FUNCTION add_one(integer) RETURNS integer
>      AS '/usr/include/pgsql/server/test_func, 'add_one'
>      LANGUAGE C STRICT
>
> test_func is the name of my object file and add_one is the name of
> the function i want to call from test_func.c  C file.
>
> I get the follwing error
> ERROR:  could not access file "/usr/include/pgsql/server/test_func": No such
> file or directory
>
> /usr/include/pgsql/server/ is exactly the path where test_func object file
> resides.
> Don't know why isn't postgres able to find it there.
>
> Any kind of help would be appreciated.

Check the permissions.  Can the Postgres user read the file?

I don't remember if it has to be marked executable or not, but that's
something to check.

--
Bill Moran
Collaborative Fusion Inc.

Re: Dynamic loading of C functions

From
Tom Lane
Date:
Bill Moran <wmoran@collaborativefusion.com> writes:
> In response to "Jasbinder Bali" <jsbali@gmail.com>:
>> I get the follwing error
>> ERROR:  could not access file "/usr/include/pgsql/server/test_func": No such
>> file or directory

> Check the permissions.  Can the Postgres user read the file?

The error is pretty clearly "file not found", not "no permissions".

One possibility is that the complaint is not about this file itself
but about some other shared library it depends on.  Try "ldd" or
local equivalent on the file to see if it shows any unresolved
references.

Also, you might try looking in the postmaster log to see if any
additional info appears there --- anything the dynamic linker spit out
to stderr is not going to appear on your terminal.

            regards, tom lane

Re: Dynamic loading of C functions

From
Bill Moran
Date:
In response to Tom Lane <tgl@sss.pgh.pa.us>:

> Bill Moran <wmoran@collaborativefusion.com> writes:
> > In response to "Jasbinder Bali" <jsbali@gmail.com>:
> >> I get the follwing error
> >> ERROR:  could not access file "/usr/include/pgsql/server/test_func": No such
> >> file or directory
>
> > Check the permissions.  Can the Postgres user read the file?
>
> The error is pretty clearly "file not found", not "no permissions".

Hmmm ... I was getting PostgreSQL confused with other software that
provides less precise errors.

My apologies.

--
Bill Moran
Collaborative Fusion Inc.

Re: Dynamic loading of C functions

From
"Jasbinder Bali"
Date:
chmod 666 filename is something i've done to give permissions to all..
still doesn't work.

 
On 6/20/06, Bill Moran <wmoran@collaborativefusion.com> wrote:
In response to "Jasbinder Bali" <jsbali@gmail.com>:

> I've written a function in C, compiled it and trying to use the same
> function in one of my postgres functions like this:
>
> CREATE FUNCTION add_one(integer) RETURNS integer
>      AS '/usr/include/pgsql/server/test_func, 'add_one'
>      LANGUAGE C STRICT
>
> test_func is the name of my object file and add_one is the name of
> the function i want to call from test_func.c  C file.
>
> I get the follwing error
> ERROR:  could not access file "/usr/include/pgsql/server/test_func": No such
> file or directory
>
> /usr/include/pgsql/server/ is exactly the path where test_func object file
> resides.
> Don't know why isn't postgres able to find it there.
>
> Any kind of help would be appreciated.

Check the permissions.  Can the Postgres user read the file?

I don't remember if it has to be marked executable or not, but that's
something to check.

--
Bill Moran
Collaborative Fusion Inc.

Re: Dynamic loading of C functions

From
elein
Date:
Do you need to specify ....test_func.so not ...test_func
or has that changed?

--elein

On Tue, Jun 20, 2006 at 05:13:32PM -0400, Jasbinder Bali wrote:
> chmod 666 filename is something i've done to give permissions to all..
> still doesn't work.
>
>
> On 6/20/06, Bill Moran <wmoran@collaborativefusion.com> wrote:
>
>     In response to "Jasbinder Bali" <jsbali@gmail.com>:
>
>     > I've written a function in C, compiled it and trying to use the same
>     > function in one of my postgres functions like this:
>     >
>     > CREATE FUNCTION add_one(integer) RETURNS integer
>     >      AS '/usr/include/pgsql/server/test_func, 'add_one'
>     >      LANGUAGE C STRICT
>     >
>     > test_func is the name of my object file and add_one is the name of
>     > the function i want to call from test_func.c  C file.
>     >
>     > I get the follwing error
>     > ERROR:  could not access file "/usr/include/pgsql/server/test_func": No
>     such
>     > file or directory
>     >
>     > /usr/include/pgsql/server/ is exactly the path where test_func object
>     file
>     > resides.
>     > Don't know why isn't postgres able to find it there.
>     >
>     > Any kind of help would be appreciated.
>
>     Check the permissions.  Can the Postgres user read the file?
>
>     I don't remember if it has to be marked executable or not, but that's
>     something to check.
>
>     --
>     Bill Moran
>     Collaborative Fusion Inc.
>
>

Re: Dynamic loading of C functions

From
Tom Lane
Date:
"Jasbinder Bali" <jsbali@gmail.com> writes:
>>> /usr/include/pgsql/server/ is exactly the path where test_func object
>>> file resides.

Hmmm .... when you say "object file", do you mean it's really named
"test_func.o"?

If so, that's both the wrong name and the wrong type of file.  Postgres
is looking for a shared library, eg "test_func.so" (or on some platforms
".sl" or ".dylib").  There's some advice in our manual about the
compiler switches to use to create a shared library, or see your
compiler documentation.

Given AS '/usr/include/pgsql/server/test_func', Postgres will look for
both "test_func" and "test_func.so" (not sure which order, try the LOAD
reference page for details).  It won't look for "test_func.o" though.

BTW, most people would say that /usr/include is exactly where NOT to
put an executable file ... conventionally this kind of file goes under
/usr/lib.  That's not what's causing your problem, it's just a question
of keeping your filesystem tidy enough to be able to find things again.

            regards, tom lane

Re: Dynamic loading of C functions

From
"Jasbinder Bali"
Date:
yes, i've named it as .so file.

On 6/20/06, Tom Lane <tgl@sss.pgh.pa.us> wrote:
"Jasbinder Bali" <jsbali@gmail.com> writes:
>>> /usr/include/pgsql/server/ is exactly the path where test_func object
>>> file resides.

Hmmm .... when you say "object file", do you mean it's really named
"test_func.o"?

If so, that's both the wrong name and the wrong type of file.  Postgres
is looking for a shared library, eg "test_func.so" (or on some platforms
".sl" or ".dylib").  There's some advice in our manual about the
compiler switches to use to create a shared library, or see your
compiler documentation.

Given AS '/usr/include/pgsql/server/test_func', Postgres will look for
both "test_func" and "test_func.so" (not sure which order, try the LOAD
reference page for details).  It won't look for "test_func.o" though.

BTW, most people would say that /usr/include is exactly where NOT to
put an executable file ... conventionally this kind of file goes under
/usr/lib.  That's not what's causing your problem, it's just a question
of keeping your filesystem tidy enough to be able to find things again.

                       regards, tom lane

Re: Dynamic loading of C functions

From
"Erin Sheldon"
Date:
Was the object compiled on the same architecture as postgres?
Sometimes when one is compiled on a 64-bit, the other on 32-bit,
or two wildly different versions of gcc, I have seen this cryptic "No such
file or directory".

Erin

On 6/20/06, Jasbinder Bali <jsbali@gmail.com> wrote:
>
> chmod 666 filename is something i've done to give permissions to all..
> still doesn't work.
>
>
>
> On 6/20/06, Bill Moran <wmoran@collaborativefusion.com> wrote:
> > In response to "Jasbinder Bali" <jsbali@gmail.com>:
> >
> > > I've written a function in C, compiled it and trying to use the same
> > > function in one of my postgres functions like this:
> > >
> > > CREATE FUNCTION add_one(integer) RETURNS integer
> > >      AS '/usr/include/pgsql/server/test_func, 'add_one'
> > >      LANGUAGE C STRICT
> > >
> > > test_func is the name of my object file and add_one is the name of
> > > the function i want to call from test_func.c  C file.
> > >
> > > I get the follwing error
> > > ERROR:  could not access file
> "/usr/include/pgsql/server/test_func": No such
> > > file or directory
> > >
> > > /usr/include/pgsql/server/ is exactly the path where test_func object
> file
> > > resides.
> > > Don't know why isn't postgres able to find it there.
> > >
> > > Any kind of help would be appreciated.
> >
> > Check the permissions.  Can the Postgres user read the file?
> >
> > I don't remember if it has to be marked executable or not, but that's
> > something to check.
> >
> > --
> > Bill Moran
> > Collaborative Fusion Inc.
> >
>
>

Re: Dynamic loading of C functions

From
"Jasbinder Bali"
Date:

I've tried everything so that my .so file is recognized but in vein.
Don't know whats going wrong.
~Jas

On 6/20/06, Tom Lane < tgl@sss.pgh.pa.us> wrote:
Bill Moran < wmoran@collaborativefusion.com> writes:
> In response to "Jasbinder Bali" <jsbali@gmail.com>:
>> I get the follwing error
>> ERROR:  could not access file "/usr/include/pgsql/server/test_func": No such
>> file or directory

> Check the permissions.  Can the Postgres user read the file?

The error is pretty clearly "file not found", not "no permissions".

One possibility is that the complaint is not about this file itself
but about some other shared library it depends on.  Try "ldd" or
local equivalent on the file to see if it shows any unresolved
references.

Also, you might try looking in the postmaster log to see if any
additional info appears there --- anything the dynamic linker spit out
to stderr is not going to appear on your terminal.

                        regards, tom lane

Re: Dynamic loading of C functions

From
Martijn van Oosterhout
Date:
On Wed, Jun 21, 2006 at 12:03:26PM -0400, Jasbinder Bali wrote:
> I've tried everything so that my .so file is recognized but in vein.
> Don't know whats going wrong.

The easiest may be to attach strace to the backend (strace -p) and then
do a "LOAD blah" on the frontend and see what happens.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment