From: Joe Conway <mail@joeconway.com>
To: Rob Abernethy IV <abernethy@dynedge.com>
Cc: postgresql <pgsql-admin@postgresql.org>
Subject: Re: pg_group view
Date: Mon, 30 Dec 2002 14:16:35 -0800
Message-ID: <3E10C5C3.6010906@joeconway.com> (raw)
In-Reply-To: <20021230153246.M60737@dynedge.com>
References: <20021230153246.M60737@dynedge.com>
Rob Abernethy IV wrote:
> Does anyone have a good recipe for a view that will display users/gruops in a
> way that can be used with Tomcat's JDBCRelam configuration? In other words,
> instead of seeing a *list* of users in a group (grolist), I'd like to see a
> separate row for every group/user combination.
>
> group | user
> -------------
> 1 | 1
> 1 | 2
> 2 | 1
You didn't say what version of PostgreSQL you're using. If it is prior to
7.3.x, you're pretty much out of luck. Starting with 7.3, you can use
something like the following:
CREATE TYPE group_view AS (grosysid int4, groname name, usesysid int4, usename
name);
CREATE OR REPLACE FUNCTION expand_groups() RETURNS SETOF group_view AS '
DECLARE
rec record;
groview record;
low int;
high int;
BEGIN
FOR rec IN SELECT grosysid FROM pg_group LOOP
SELECT INTO low
replace(split_part(array_dims(grolist),'':'',1),''['','''')::int
FROM pg_group WHERE grosysid = rec.grosysid;
SELECT INTO high
replace(split_part(array_dims(grolist),'':'',2),'']'','''')::int
FROM pg_group WHERE grosysid = rec.grosysid;
FOR i IN low..high LOOP
SELECT INTO groview g.grosysid, g.groname, s.usesysid, s.usename
FROM pg_shadow s join pg_group g on s.usesysid = g.grolist[i];
RETURN NEXT groview;
END LOOP;
END LOOP;
RETURN;
END;
' LANGUAGE 'plpgsql';
CREATE VIEW public.pg_groupview AS SELECT * FROM expand_groups();
test=# select * from pg_groupview;
grosysid | groname | usesysid | usename
----------+---------+----------+---------
100 | g1 | 100 | user1
100 | g1 | 101 | user2
100 | g1 | 100 | user1
100 | g1 | 101 | user2
101 | g2 | 102 | user3
(5 rows)
There *might* be a builtin function or view in 7.4 to do the same thing.
HTH,
Joe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: mail@joeconway.com, abernethy@dynedge.com
Subject: Re: pg_group view
In-Reply-To: <3E10C5C3.6010906@joeconway.com>
References: <20021230153246.M60737@dynedge.com> <3E10C5C3.6010906@joeconway.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox