Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-schema Postgres on Heroku

I'm extending an existing Rails app, and I have to add multi-tenant support to it. I've done some reading, and seeing how this app is going to be hosted on Heroku, I thought I could take advantage of Postgres' multi-schema functionality.

I've read that there seems to be some performance issues with backups when multiple schemas are in use. This information I felt was a bit outdated. Does anyone know if this is still the case?

Also, are there any other performance issues, or caveats I should take into consideration?

I've already thought about adding a field to every table so I can use a single schema, and have that field reference to the tenants table, but given the time windows multiple schemas seem the best solution.

like image 989
Fabian Silva Avatar asked Nov 05 '13 22:11

Fabian Silva


People also ask

Can you join across schemas Postgres?

Yes, you just prepend the schema.

Can Postgres have multiple databases?

PostgreSQL can have multiple databases in each instance. Each database can have multiple schemas. Each schema can have multiple tables.

Can you host PostgreSQL on Heroku?

Heroku Postgres is a managed SQL database service provided directly by Heroku. You can access a Heroku Postgres database from any language with a PostgreSQL driver, including all languages officially supported by Heroku.


1 Answers

I use postgres schemas for a multi-tenancy site based on some work by Ryan Bigg and the Apartment gem.


https://leanpub.com/multi-tenancy-rails

https://github.com/influitive/apartment


I find that having seperate schemas for each client an elegant solution which provides a higher degree of data segregation. Personally I find the performance improves because Postgres can simply return all results from a table without have to filter to an 'owner_id'.

I also think it makes for simpler migrations and allows you to adjust individual customer data without making global changes. For example you can add columns to specific customers schemas and use feature flags to enable custom features.

My main argument relating to performance would be that backup is a periodic process, whereas customer table scoping would be on every access. On that basis, I would take any performance hit on backup over slowing down the customer experience.

like image 84
muttonlamb Avatar answered Oct 10 '22 09:10

muttonlamb