citusdata / citus

Distributed PostgreSQL as an extension
https://www.citusdata.com
GNU Affero General Public License v3.0
10.53k stars 667 forks source link

Add template schemas for easier distributed schema management #7356

Open double-dubs opened 11 months ago

double-dubs commented 11 months ago

Has there been any thought into how Citus could manage multiple schemas with the same table structure? There could be a master template schema and then a Citus command that would create new schemas based on this template. Citus would then monitor this template schema for table structure changes that would then be automatically replicated to all the schemas that were created using this template.

It would be similar to how the row based sharding works now where you only need to manage a single table structure and Citus replicates that to all the shard tables.

We currently are using separate databases (a few hundred) for each enterprise SAAS customer and having each customer as their own schema would be an improvement, but having an easy and reliable way to replicate schema changes would really make a compelling case.

onurctirtir commented 11 months ago

This was something we were anticipating that there could be a need for this, and we wanted to wait until hearing an explicit user ask about that, thank you for opening this issue!

To better understand your need, would it be enough to replicate only table level DDLs from template schemas to corresponding tables in actual schemas (ALTER TABLE), or would you need any other kind of DDLs to be replicated, such as CREATE INDEX, or any other type (VACUUM, TRUNCATE, something else)?

onurctirtir commented 11 months ago

I could imagine that we add a GUC that specifies which template schema is requested to be used when creating a new schema, as in:

SET citus.effective_template_schema_name = "template_schema_1";

Then the CREATE SCHEMA commands that follows this statement directly inherit table structures defined in template_schema_1 as well as following table structure changes (ALTER TABLE .., CREATE INDEX) that happens in template_schema_1. This probably also requires disallowing individual schema changes in the schemas that inherit their structure from a template one?

Is my understanding correct? DDLs other than ALTER TABLE / CREATE INDEX are needed?

double-dubs commented 10 months ago

I am not familiar with all the operations you currently support with row based, but it would make sense to have this function in a similar way for consistency sake. I would think at a minimum you would need:

Table: create, alter, drop View: create, drop Function: create, replace, drop Index: create, drop

double-dubs commented 5 months ago

Are there any plans to implement something on this front in an upcoming release? We have been building our new version on Citus and unsure at the moment how we are going to manage the hundreds of identical schemas.