1

I'd like to set a session variable inside of a function, using the values passed into the function.

Example:

CREATE FUNCTION my_function(my_param INT)
RETURNS VOID AS $$
BEGIN
    SET SESSION "myapp.myvar" = my_param;
END 
$$
LANGUAGE plpgsql;

SELECT * FROM my_function(100);
SELECT current_setting('myapp.myvar');

What I get:

my_param

What I want:

100
0
4

Use set_config()

CREATE FUNCTION my_function(my_param INT)
RETURNS VOID AS $$
BEGIN
    perform set_config('myapp.myvar', my_param::text, true);
END 
$$
LANGUAGE plpgsql;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.