0

I tried this but it does not work:

CREATE OR REPLACE FUNCTION answer()
 RETURNS bigint
 LANGUAGE sql AS
$$
SELECT 42
$$;
SET session.answer = select answer();
asked Dec 11, 2023 at 15:24

1 Answer 1

3

The SET command only accepts constants. The manual:

constants, identifiers, numbers, or comma-separated lists of these

It does not take parameters, nor can it evaluate subqueries. Parameter substitution only works for basic DML commands (SELECT, INSERT, UPDATE, DELETE, MERGE).

The function set_config() provides equivalent functionality;

SELECT set_config('session.answer', answer()::text, false);

Note the cast to ::text. Postgres parameters only store text.

Related:

answered Dec 11, 2023 at 16:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.