BUG #16519: SET SESSION ROLE in plpgsql requires string literal.

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: mbwiese(at)gmail(dot)com
Subject: BUG #16519: SET SESSION ROLE in plpgsql requires string literal.
Date: 2020-06-30 16:20:03
Message-ID: 16519-9ef04828d058a319@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 16519
Logged by: Marianne B. Wiese
Email address: mbwiese(at)gmail(dot)com
PostgreSQL version: 10.12
Operating system: Ubuntu 18.04
Description:

The documentation says:
This command sets the current user identifier of the current SQL session to
be role_name. The role name can be written as either an identifier or a
string literal. After SET ROLE, permissions checking for SQL commands is
carried out as though the named role were the one that had logged in
originally.

However, the loop in below function gives me an error:
[22023] ERROR: role "inuser" does not exist Where: SQL statement "SET
SESSION ROLE TO InUser" PL/pgSQL function change_user(name) line 8 at SQL
statement.

I can only make it work with string literals, as in the commented out
section.

CREATE OR REPLACE FUNCTION public.change_user(InUser name) RETURNS void AS
$$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT role FROM public.editors
LOOP
IF ( InUser = r.role ) THEN
SET SESSION ROLE TO InUser;
end if;
END LOOP;
/* IF ( InUser = 'mbw(at)geus(dot)dk' ) THEN
SET SESSION ROLE TO 'mbw(at)geus(dot)dk';
ELSEIF ( InUser = 'joth(at)geus(dot)dk') THEN
SET SESSION ROLE TO 'joth(at)geus(dot)dk';
ELSE raise invalid_role_specification using message = 'authenticator
can not be || ' + InUser;
END IF;
*/
END
$$ LANGUAGE plpgsql;

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Meskes 2020-06-30 16:38:08 Re: [BUG][PATCH] ecpg crash with bytea type and cursors
Previous Message PG Bug reporting form 2020-06-30 16:09:58 BUG #16518: DBA on several PostgreSQL versions