【问题标题】:Is it safe to use inheritance for meta-data migration?使用继承进行元数据迁移是否安全?
【发布时间】:2019-12-05 14:48:58
【问题描述】:

为元数据创建模板模式以便其他包含数据的模式可以继承它是否安全?

优势:多租户场景的迁移将是无缝的。

缺点:?

例子:

hello=# CREATE SCHEMA template;

hello=# CREATE TABLE template.cities (
name       text,
population real,
altitude   int;

hello=# CREATE SCHEMA us;

hello=# CREATE TABLE us.cities () INHERITS (template.cities);

hello=# \d us.cities;
                   Table "us.cities"
   Column   |     Type     | Collation | Nullable | Default 
------------+--------------+-----------+----------+---------
 name       | text         |           |          | 
 population | real         |           |          | 
 altitude   | integer      |           |          | 
Inherits: template.cities

hello=# CREATE SCHEMA eu;

hello=# CREATE TABLE eu.cities () INHERITS (template.cities);

hello=# \d eu.cities;
                   Table "eu.cities"
   Column   |     Type     | Collation | Nullable | Default 
------------+--------------+-----------+----------+---------
 name       | text         |           |          | 
 population | real         |           |          | 
 altitude   | integer      |           |          | 
Inherits: template.cities

hello=# ALTER TABLE cities ADD COLUMN state varchar(30);

hello=# \d us.cities;
                          Table "us.cities"
   Column   |         Type          | Collation | Nullable | Default 
------------+-----------------------+-----------+----------+---------
 name       | text                  |           |          | 
 population | real                  |           |          | 
 altitude   | integer               |           |          | 
 state      | character varying(30) |           |          | 
Inherits: template.cities


【问题讨论】:

    标签: postgresql inheritance database-migration multi-tenant


    【解决方案1】:

    我认为继承是解决这个问题的好方法。

    我可以想到两个缺点:

    • 可以为继承子级创建附加列。如果你控制了 DDL,你可能会阻止它。

    • 您仍然需要单独创建和修改所有继承子级的索引。

    如果您使用的是 PostgreSQL v11 或更高版本,则可以通过使用分区来防止这两个问题。然后,各个表将成为“模板”表的分区。这样,您可以通过在模板表上创建分区索引来集中创建索引。缺点(可能使此解决方案无法实现)是您需要表中的分区键列。

    【讨论】:

    • 感谢您的回复。正如您所说的那样,它仅适用于非索引列。我不确定分区是否适用于拥有大量租户的应用程序。 Postgres 如何在内部处理此类情况,即将父模式更改传播给子模式?我相信这可以减少拥有大量租户的应用程序所需的应用程序停机时间。
    • 我认为继承可能是一个很好的解决方案。我只是想像你问的那样提出缺点。
    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 2021-12-05
    • 2016-11-19
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2011-11-04
    相关资源
    最近更新 更多