How to refer to resource files from UDFs written in C

Started by Supun Nakandalaover 8 years ago4 messages
#1Supun Nakandala
supun.nakandala@gmail.com

Hi hackers,

I am trying to extend PostgreSQL by adding UDT and UDF for a custom use
case and I am using C language extensions to do that.

However, I have a requirement of reading a text file from one of the C
functions. The compiled *.so files are placed in the "pg_config
--pkglibdir" directory and tried copying my text files there but it didn't
work. I found that, when these shared libs are loaded they are run from a
different working directory. In this case, what is the best way to refer to
my text files from the C code other than giving the absolute path which can
change from system to system.

Thank you.
Supun

#2Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Supun Nakandala (#1)
Re: How to refer to resource files from UDFs written in C

On 06/09/2017 08:56 AM, Supun Nakandala wrote:

Hi hackers,

I am trying to extend PostgreSQL by adding UDT and UDF for a custom use
case and I am using C language extensions to do that.

However, I have a requirement of reading a text file from one of the C
functions. The compiled *.so files are placed in the "pg_config
--pkglibdir" directory and tried copying my text files there but it didn't
work. I found that, when these shared libs are loaded they are run from a
different working directory. In this case, what is the best way to refer to
my text files from the C code other than giving the absolute path which can
change from system to system.

All backend processes run with the data directory as the current
directory. So you can put the files into the data directory, probably
best to have a subdirectory there to avoid confusing them with
PostgreSQL's own files. Or you could have a config option, to set an
absolute path.

- Heikki

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Supun Nakandala (#1)
Re: How to refer to resource files from UDFs written in C

Supun Nakandala <supun.nakandala@gmail.com> writes:

However, I have a requirement of reading a text file from one of the C
functions. The compiled *.so files are placed in the "pg_config
--pkglibdir" directory and tried copying my text files there but it didn't
work. I found that, when these shared libs are loaded they are run from a
different working directory. In this case, what is the best way to refer to
my text files from the C code other than giving the absolute path which can
change from system to system.

You probably want to use get_share_path, or one of its sibling functions
in src/port/path.c. Then your files go with the PG installation tree,
whereever it is. There are different functions that are appropriate for
different types of files.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Supun Nakandala
supun.nakandala@gmail.com
In reply to: Heikki Linnakangas (#2)
Re: How to refer to resource files from UDFs written in C

On Fri, Jun 9, 2017 at 3:05 AM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:

On 06/09/2017 08:56 AM, Supun Nakandala wrote:

Hi hackers,

I am trying to extend PostgreSQL by adding UDT and UDF for a custom use
case and I am using C language extensions to do that.

However, I have a requirement of reading a text file from one of the C
functions. The compiled *.so files are placed in the "pg_config
--pkglibdir" directory and tried copying my text files there but it didn't
work. I found that, when these shared libs are loaded they are run from a
different working directory. In this case, what is the best way to refer
to
my text files from the C code other than giving the absolute path which
can
change from system to system.

All backend processes run with the data directory as the current
directory. So you can put the files into the data directory, probably best
to have a subdirectory there to avoid confusing them with PostgreSQL's own
files. Or you could have a config option, to set an absolute path.

- Heikki

Thank you everyone for your quick replies. I ended up copying the files to
the data directory and referring using relative path.