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();

ceving
  • 379
  • 3
  • 13

1 Answers1

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:

Erwin Brandstetter
  • 185,527
  • 28
  • 463
  • 633