Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure all Postgres queries have WHERE clause?

Tags:

postgresql

I am building a multi tenant system in which many clients data will be in the same database.

I am paranoid about some developer forgetting to put the appropriate "WHERE clientid = " onto every query.

Is there a way to, at the database level, ensure that every query has the correct WHERE = clause, thereby ensuring that no query will ever be executed without also specifying which client the query is for?

I was wondering if maybe the query rewrite rules could do this but it's not clear to me if they can do so.

thanks

like image 857
Duke Dougal Avatar asked Oct 19 '25 15:10

Duke Dougal


1 Answers

Deny permissions on the table t for all users. Then give them permission on a function f that returns the table and accepts the parameter client_id:

create or replace function f(_client_id integer)
returns setof t as
$$
    select *
    from t
    where client_id = _client_id
$$ language sql
;

select * from f(1);
 client_id | v 
-----------+---
         1 | 2
like image 171
Clodoaldo Neto Avatar answered Oct 21 '25 10:10

Clodoaldo Neto