0

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply and advice on the use of C++. The problem is solved. And it makes sense to use the user's home directory, for example: ~/pg_files_storage/[database name]. However, the base is located on the external drive, so it is advisable to use the path of the database, it is possible relative to the storage (../[pgdata]/pg_files_storage).
0

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.

1 Comment

I write a library in C++, using a special API. It would be good to get the settings using function call. How to use the SQL query in the library? And, I promise :), I will not modify the database files, I need to create a subdirectory for binary files. In fact, PostgreSQL will act as a file server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.