0

My C++ functions for postgres use a parameter (the path to the config) for static singleton Config class, now it is set from Cmake via definitions. I want to make the path specified like an argv parameter in the main function. However, my functions are just a shared (.so) library. Is there any way to achieve such functionality ?

Now I use this vartiant:

add_compile_definitions(CONFIG_PATH="/some/path/to/config.ini)

Also I have sql script for creating postgres functions:

CREATE OR REPLACE FUNCTION some() RETURNS text
     AS 'postgres_Clibrary.so', 'someFunction'
     LANGUAGE C;

And load functions as:

psql -f create_functions.sql

And I want to change this to something like:

psql -f create_functions.sql -DPATH_TO_CONFIG="/some/path/to/config.ini"

Is there any variant to get similar functionality as I want ? How to do it without recompilation ?

4
  • I am not certain what you mean. Do you want a fixed value for a certain function argument, defined at the time the function is created in PostgreSQL? Commented Sep 21, 2022 at 8:50
  • @LaurenzAlbe If we use a regular bin file, then we write ./execute "some" "argv" "params". I want that when creating functions using psql or something like that, you can specify a path so as not to compile the library again. Commented Sep 21, 2022 at 9:26
  • The linked answer shows how to use system settings to accomplish what you want. If needed, that can be overridden on a per-connection basis Commented Sep 21, 2022 at 9:41
  • @Botje I don't think that's what is asked for, so I'll vote to reopen. I'd say that default values for a function argument are the solution. A placeholder parameter is not fixed in any way and can always change. Commented Sep 21, 2022 at 9:47

1 Answer 1

0

I think you could use a default value for a parameter here.

Define your function so that it has an additional parameter config_path. That parameter has to be the last one. Then you can define the function as

CREATE FUNCTION some(
   first_param text,
   config_path text DEFAULT '/some/path/to/config.ini'
) RETURNS text
AS 'postgres_Clibrary.so', 'someFunction'
LANGUAGE C;

Now you can simply call the function with

SELECT some('first_arg');

and the default value will be substituted for the second argument.

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

4 Comments

So, it should work, but my config is a static sigleton class, so I don't want to read or create class object every time
I thought you were talking about a function, not a class... Can you clarify?
The function body uses the singleton class, which must be initialized in advance. Something like static Config& getConfig(), which in turn uses configPath. So, I can't init it every time, I should do this only one time
Well, you can check if it is already initialized, can't you? And only initialize it the first time.

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.