Dynamic loading of C functions

Started by Jasbinder Balialmost 20 years ago11 messagesgeneral
Jump to latest
#1Jasbinder 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.

~Jas

#2Bill Moran
wmoran@collaborativefusion.com
In reply to: Jasbinder Bali (#1)
Re: Dynamic loading of C functions

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.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bill Moran (#2)
Re: Dynamic loading of C functions

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

#4Bill Moran
wmoran@collaborativefusion.com
In reply to: Tom Lane (#3)
Re: Dynamic loading of C functions

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.

#5Jasbinder Bali
jsbali@gmail.com
In reply to: Bill Moran (#2)
Re: Dynamic loading of C functions

chmod 666 filename is something i've done to give permissions to all..
still doesn't work.

Show quoted text

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.

#6elein
elein@varlena.com
In reply to: Jasbinder Bali (#5)
Re: Dynamic loading of C functions

Do you need to specify ....test_func.so not ...test_func
or has that changed?

--elein

Show quoted text

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.

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jasbinder Bali (#5)
Re: Dynamic loading of C functions

"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

#8Jasbinder Bali
jsbali@gmail.com
In reply to: Tom Lane (#7)
Re: Dynamic loading of C functions

yes, i've named it as .so file.

Show quoted text

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

#9Erin Sheldon
erin.sheldon@gmail.com
In reply to: Jasbinder Bali (#5)
Re: Dynamic loading of C functions

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

Show quoted text

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.

#10Jasbinder Bali
jsbali@gmail.com
In reply to: Tom Lane (#3)
Re: Dynamic loading of C functions

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

Show quoted text

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

#11Martijn van Oosterhout
kleptog@svana.org
In reply to: Jasbinder Bali (#10)
Re: Dynamic loading of C functions

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/

Show quoted text

From each according to his ability. To each according to his ability to litigate.