Global configuration of native Postgres functions at function creation time

Viewed 48

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 ?

1 Answers

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.

Related