Reputation: 9
OK, maybe doing wrong. But want to teach PostgreSQL to distribute the files with extension functions in C++. The project works in the local network and may by about ten connections. I do not want to have FTP or other external solutions for storing images. I do not want to store images in the database. I would like so: fs_select_file(id)
, fs_insert_file(id, escaped_bytea)
, fs_delete_file(id)
, e.g. SELECT id, name, fs_select_file(id) as escaped_bytea FROM ...
. But can't find how to determine the path of the current database to use the [PGDATA]/files
.
Upvotes: 0
Views: 337
Reputation: 324395
Don't mess with the datadir.
Even if you think it's safe to create your own contents within it, it probably isn't. Tools like pg_basebackup
won't expect to see it.
Store your own content outside the PostgreSQL data directory. If you're working with a C extension that's easy enough, just let the user configure a storage location with a custom string GUC (see DefineCustomStringVariable
).
Anyway, you can find the definition of the data_directory
GUC in src/backend/utils/misc/guc.c
. There you'll see that it's mapped to a global char * data_directory
. A quick:
$ git grep data_directory src/include
src/include/utils/guc.h:extern char *data_directory;
shows that the extern for it is in guc.h
. So you can access it with:
#include "utils/guc.h"
then using the data_directory
global. But you shouldn't. Don't do this. Define your own custom GUC specifying a separate image store location.
While we're at it. C++.
PostgreSQL is a C application that uses longjmp
based error handling. This goes with C++ exception handling about as well as a cigarette in an oxygen tent. C++ exceptions that cross PG_TRY
boundaries will completely mangle the error stack. PostgreSQL error traps that cross a C++ stack subject to unwinding (any stack objects with destructors, try/catch blocks, etc) will completely mangle the C++ state.
It's possible, but hard, to combine PostgreSQL and C++. You have to isolate each piece via an interface of pure C code, and be paranoid about catching and translating error conditions at all boundaries.
If at all possible, you should instead just use C, or at least encapsulate your C++ code into a separate library that never includes PostgreSQL headers and never calls back into PostgreSQL code. The documentation discusses this in a little more detail.
Upvotes: 3
Reputation: 17837
SELECT current_setting('data_directory');
This will show you the fully qualified path to the data directory. I'm not clear why you need this, because mucking around in the data files is usually a good way to corrupt your database. Anyway, I think this is the answer you're looking for.
Upvotes: 0