Re: Using set role inside a function question

From: "Vianello, Daniel A" <Daniel(dot)Vianello(at)charter(dot)com>
To: Garry Chen <gc92(at)cornell(dot)edu>, "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Using set role inside a function question
Date: 2017-11-07 19:57:04
Message-ID: 02ffddf979874dbcba78a4d90f8fc96d@KSTLMEXGP001.CORP.CHARTERCOM.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi Gary,

p_runasid is a variable, not a role name, so you will need to run this as dynamic sql. That can be accomplished with the next line of code below. You may want to take a look at the manual for setting functions as “SECURITY DEFINER” (https://www.postgresql.org/docs/current/static/sql-createfunction.html ) and read up on SQL injection in general. The syntax here can be found on this page https://www.postgresql.org/docs/current/static/plpgsql-statements.html

execute format('set role= %I;', p_runas_id);

Dan

From: pgsql-novice-owner(at)postgresql(dot)org [mailto:pgsql-novice-owner(at)postgresql(dot)org] On Behalf Of Garry Chen
Sent: Tuesday, November 07, 2017 1:43 PM
To: pgsql-novice(at)postgresql(dot)org
Subject: [NOVICE] Using set role inside a function question

Hi All,
I have a function pass in a user login id (p_runas_id) and the it will insert a row into a table after that set role to that pass in id. This function complied fine but getting ERROR: role "p_runas_id" does not exist CONTEXT: SQL statement "SET ROLE p_runas_id" when execute. Any suggestion?

Thank you very much,
Garry

CREATE OR REPLACE FUNCTION set_netid_context(p_runas_id varchar(30))
RETURNS void
AS $$
Declare
v_dm_name varchar(30);
v_runid varchar(30);
v_ora_id numeric;

BEGIN
INSERT INTO obiee_context_audit(context_date, ora_sid, db_user, runas_id)
VALUES (now(), pg_backend_pid(), SESSION_USER, p_runas_id);
SET ROLE p_runas_id;
END; $$
LANGUAGE plpgsql;

E-MAIL CONFIDENTIALITY NOTICE:
The contents of this e-mail message and any attachments are intended solely for the addressee(s) and may contain confidential and/or legally privileged information. If you are not the intended recipient of this message or if this message has been addressed to you in error, please immediately alert the sender by reply e-mail and then delete this message and any attachments. If you are not the intended recipient, you are notified that any use, dissemination, distribution, copying, or storage of this message or any attachment is strictly prohibited.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message john snow 2017-11-09 02:20:21 what does t(x) in select x from generate_series(1, 10) as t(x) stand for?
Previous Message Garry Chen 2017-11-07 19:42:41 Using set role inside a function question