Find the answer to your Linux question:
Results 1 to 10 of 10
Hai all , kranthi here i generated .so file in linux i would like to call a function named as abc() which is available in "ab.so" file from postgres function ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    8

    Accessing .so files in Database


    Hai all ,
    kranthi here

    i generated .so file in linux

    i would like to call a function named as abc() which is available in "ab.so" file from
    postgres function which look like

    CREATE FUNCTION abc(text, text, text) RETURNS TEXT
    AS '/home/downloads/ab.so', 'abc'
    LANGUAGE 'C';

    when i executed i got an error as follows

    error
    could not load library "/home/downloads/ab.so": /home/downloads/ab.so: undefined symbol: DirectFunctionCall1Coll

  2. #2
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    When you compiled your ".so" file, what was the exact compile command you used?

    If you look at the PostgreSQL documentation, section 34.9.6, it says you must use the command:
    Code:
    cc -fpic -c abc.c
    cc -shared -o ab.so abc.o
    It is important to specify the "-fpic" option when compiling the source file -- this enables "Position Independent Code" which is necessary for dynamically loading libraries. It is also important to specifiy "-shared" when creating your ".so" file, otherwise the resulting file will not be a proper "shared object".

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94
    Hi kranthikumar

    It looks like your problem is with ld.so. You have to tell it were the .so is located. There are two ways that I know of.
    1. as root, you can edit /etc/ld.so.conf then add the directory of your .so then (as root) run ldconfig
    2. or, (assuming your not root), you can augment your LD_LIBRARY_PATH variable to indicate were your .so is located.

    Usually, the second choice is preferable. If you decide on the second choice, you can set the variable and run the command on one line like this:
    Code:
    LD_LIBRARY_PATH=/path/to/library ./command-that-needs-library

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Apr 2012
    Posts
    8
    Hai ramin
    Thanks for the reply
    i tried the same thing,but again i got the same error. whether i want to install any other s/w's or else i want to do anything else to rectify this error.once again thanks

  6. #5
    Just Joined!
    Join Date
    Apr 2012
    Posts
    8
    Hai BrianMicek
    Thanks for the reply ,
    i didn't understand this "LD_LIBRARY_PATH=/path/to/library ./command-that-needs-library"
    can u make it much more clear. i saved that .so file in root path as well as in local path .
    Last edited by kranthikumar; 04-10-2012 at 07:24 AM.

  7. #6
    Just Joined!
    Join Date
    Apr 2012
    Posts
    8
    Hai,
    I added a new function inside the Postgres source code and i compiled that code and generated a .so file. That function is included in that .so file. bt when i tried to call it in database its showing no such function is found in that .so file. Can anyone help me?

  8. #7
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94
    Quote Originally Posted by kranthikumar View Post
    Hai BrianMicek
    Thanks for the reply ,
    i didn't understand this "LD_LIBRARY_PATH=/path/to/library ./command-that-needs-library"
    can u make it much more clear. i saved that .so file in root path as well as in local path .
    Hi,
    In long notation, lets assume:
    1. your using bash
    2. you were able to compile correctly
    3. your .so is located in /tmp (it can be located anywhere)
    4. your user-id is not root
    5. your compiled ELF binary is called a.out and is located in the current working directory,
    then, you can use LD_LIBRARY_PATH like this:
    Code:
    export LD_LIBRARY_PATH=/tmp
    ./a.out
    now, bash will let you shorten that notation to this:
    Code:
    LD_LIBRARY_PATH=/tmp ./a.out

  9. #8
    Just Joined!
    Join Date
    Apr 2012
    Posts
    8
    Now i am facing a new problem. I added one new function in the existing postgres source code file and i generated .so file. But its showing in database no such function existing. Can u pls help me from this

  10. #9
    Just Joined!
    Join Date
    Apr 2012
    Posts
    8
    like in windows is there any way to register this .so file in ubuntu. Since wat ever changes i made in the .so file is effecting the database.

  11. #10
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Quote Originally Posted by BrianMicek View Post
    It looks like your problem is with ld.so. You have to tell it were the .so is located. There are two ways that I know of.
    1. as root, you can edit /etc/ld.so.conf then add the directory of your .so then (as root) run ldconfig
    2. or, (assuming your not root), you can augment your LD_LIBRARY_PATH variable to indicate were your .so is located.
    @BrianMicek Hi Brian. Actually, in my education, I was told that LD_LIBRARY_PATH variable is generally not preferable, its more of a quick-fix. Otherwise, I think you are absolutely right about PosgreSQL not being able to see the library.

    Exporting the LD_LIBRARY_PATH will not help, because it will only effect applications that are launched with that environment variable set for it, e.g. applications launched by Bash on the command line. The PostgreSQL server is not launched in this way.

    @kranthikumar In order to tell the PostgreSQL server where it should load it's libraries, you need to set the the "dynamic_library_path" variable (see section 18.10 of the PostgreSQL manual), and this variable is defined in the "psotgresql.conf" configuration file (as explained in section 18.1 of the PostgreSQL manual).

    The location of "postgresql.conf" depends on how you have it installed, but usually it is in "/var/lib/pgsql/data/postgresql.conf".

    So, you need to place the compiled "abc.so" file in a directory which the PostgreSQL server has access permissions, so for example, in the "/usr/local/lib/postgresql" directory (create the directory if it is not there), then add the "/usr/local/lib/postgresql" path to the "dynamic_library_path" variable in the "/var/lib/pgsql/data/postgresql.conf" file. For example:
    Code:
    dynamic_library_path = '/usr/local/lib/postgresql:/home/my_project/lib:$libdir'
    Then restart your PostgreSQL server and execute the test query again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •