PostgreSQL schemas, select same table from all schemas

postgresqlschema

My app is used by about 80k users every month. I want to create separate PostgreSQL schemas for each user (a-la SaaS, for protection).

My app allows users to search other user's profiles, via:

SELECT user_id FROM user_table WHERE city = 'NYC';    

What is the correct way to implement the same search functionality with unique schemas in place for each user?

Will performance suffer?

Best Answer

This might be a good case for using table inheritance. Create the initial table in the master schema and then make all user tables inherit it.

The key to making this work well is to see the master table as as abstract mixin rather than a typical public class inheritance. The parent table then gives you, essentially, a catalog of all child tables' contents. This becomes problematic if you also insert into the parent table so you may want to use a trigger to avoid that.