Skip to main content
Tweeted twitter.com/#!/StackDBAs/status/419281772745351168
added 128 characters in body
Source Link
Neil McGuigan
  • 8.6k
  • 5
  • 40
  • 56

Let's say I have two Postgresql database groups, "authors" and "editors", and one usertwo users, "maxwell" and "ernest".

create role authors inherit;authors;

create role editors inherit;editors;

create user maxwellmaxwell;

create inherit;user ernest;

grant authors to editors; --editors can do what authors can do

grant editors to maxwell; --maxwell is an editor 

grant authors to ernest; --ernest is an author

I would like to write a performant function that returns a list of the roles (preferably their oid's) that maxwell belongs to, something like this:

create or replace function get_all_roles() returns oid[] ...

It should return the oids for maxwell, authors, and editors (but not ernest).

But I am not sure how to do it when there is inheritance.

Let's say I have two Postgresql database groups, "authors" and "editors", and one user "maxwell".

create role authors inherit;

create role editors inherit;

create user maxwell inherit;

grant authors to editors; --editors can do what authors can do

grant editors to maxwell; --maxwell is an editor

I would like to write a performant function that returns a list of the roles (preferably their oid's) that maxwell belongs to, something like this:

create or replace function get_all_roles() returns oid[] ...

But I am not sure how to do it when there is inheritance.

Let's say I have two Postgresql database groups, "authors" and "editors", and two users, "maxwell" and "ernest".

create role authors;

create role editors;

create user maxwell;

create user ernest;

grant authors to editors; --editors can do what authors can do

grant editors to maxwell; --maxwell is an editor 

grant authors to ernest; --ernest is an author

I would like to write a performant function that returns a list of the roles (preferably their oid's) that maxwell belongs to, something like this:

create or replace function get_all_roles() returns oid[] ...

It should return the oids for maxwell, authors, and editors (but not ernest).

But I am not sure how to do it when there is inheritance.

Source Link
Neil McGuigan
  • 8.6k
  • 5
  • 40
  • 56

How to get all roles that a user is a member of (including inherited roles)?

Let's say I have two Postgresql database groups, "authors" and "editors", and one user "maxwell".

create role authors inherit;

create role editors inherit;

create user maxwell inherit;

grant authors to editors; --editors can do what authors can do

grant editors to maxwell; --maxwell is an editor

I would like to write a performant function that returns a list of the roles (preferably their oid's) that maxwell belongs to, something like this:

create or replace function get_all_roles() returns oid[] ...

But I am not sure how to do it when there is inheritance.