Last active
July 27, 2018 14:34
-
-
Save luben/4ab60b0dbda66ecf4b6601b88c852272 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION set_foo(foo_val varchar) RETURNS void AS $$ | |
BEGIN | |
EXECUTE format('SET SESSION my.foo TO %I', foo_val) ; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION get_foo() RETURNS varchar AS $$ | |
DECLARE | |
foo_val varchar; | |
BEGIN | |
SHOW my.foo INTO foo_val; | |
RETURN foo_val; | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE POLICY account_managers on accounts USING ( manager = get_foo()); | |
DEMO: | |
luben=> SELECT set_foo('bar'); | |
set_foo | |
--------- | |
(1 row) | |
luben=> SELECT * FROM accounts ; | |
manager | company | contact_email | |
---------+---------+--------------- | |
(0 rows) | |
luben=> SELECT set_foo('foo'); | |
set_foo | |
--------- | |
(1 row) | |
luben=> SELECT * FROM accounts ; | |
manager | company | contact_email | |
---------+---------+--------------- | |
foo | co | foo@bar.co |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment