Thread: views to behave differently according to user and schema

views to behave differently according to user and schema

From
Samuel Thoraval
Date:
Hi,

I'd like views to behave differently according to the user and schema.

For instance, having 3 schemas corresponding to 3 users :

-- AS USER nsp1 :
CREATE TABLE nsp1.test (id integer);
CREATE VIEW nsp2.test AS SELECT * FROM nsp1.test WHERE id <= 10;
CREATE VIEW nsp3.test AS SELECT * FROM nsp1.test WHERE id <= 50;
CREATE or REPLACE VIEW public.vtest AS SELECT * FROM test WHERE id <=5;

Here, test in VIEW vtest is actually being tied up to the first
relation/view it found in the search path (nsp1,public) :
\d public.vtest;
     View "public.vtest"
 Column |  Type   | Modifiers
--------+---------+-----------
id | integer |
View definition:
 SELECT test.id
   FROM nsp1.test
  WHERE test.id <= 10;

Is it possible to change this behavior so that the VIEW will dynamically
use the search_path variable (when the schema was not specifically set) ?
In this case, when user nsp2 loggs in, the VIEW vtest would be using
VIEW nsp2.test, when user nsp3 loggs in it would use VIEW nsp3.test, for
user nsp1 TABLE nsp1.test ...

Cheers,

--
Samuel Thoraval
LIBROPHYT, Bioinformatique
Centre de Cadarache


Re: views to behave differently according to user and schema

From
Richard Huxton
Date:
Samuel Thoraval wrote:
>
> Is it possible to change this behavior so that the VIEW will dynamically
> use the search_path variable (when the schema was not specifically set) ?
> In this case, when user nsp2 loggs in, the VIEW vtest would be using
> VIEW nsp2.test, when user nsp3 loggs in it would use VIEW nsp3.test, for
> user nsp1 TABLE nsp1.test ...

I don't think so. There's no guarantee that nsp1 and nsp2 have the same
definition, which means the view could break depending on search_path.

You could create a separate view in each schema of course.

--
   Richard Huxton
   Archonet Ltd