BUG #16519: SET SESSION ROLE in plpgsql requires string literal. - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #16519: SET SESSION ROLE in plpgsql requires string literal.
Date
Msg-id 16519-9ef04828d058a319@postgresql.org
Whole thread Raw
Responses Re: BUG #16519: SET SESSION ROLE in plpgsql requires string literal.  ("David G. Johnston" <david.g.johnston@gmail.com>)
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      16519
Logged by:          Marianne B. Wiese
Email address:      mbwiese@gmail.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@geus.dk' ) THEN
            SET SESSION ROLE TO 'mbw@geus.dk';
        ELSEIF ( InUser = 'joth@geus.dk') THEN
            SET SESSION ROLE TO 'joth@geus.dk';
        ELSE raise invalid_role_specification using message = 'authenticator
can not be || ' + InUser;
        END IF;
*/
    END
$$ LANGUAGE plpgsql;


pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #16518: DBA on several PostgreSQL versions
Next
From: Michael Meskes
Date:
Subject: Re: [BUG][PATCH] ecpg crash with bytea type and cursors