By digoal
Multi-tenant mode has a difficult problem to solve. When DDL scripts need to be executed in multiple tenants, how can we execute in batches?
Let’s take multi-tenant schema technology as an example. Each tenant has one schema, and the structure of each schema is the same. When you need to add a table, delete a table, modify the table structure, and add an index, how can you operate in batches in all schemas?
Write a UDF function:
create or replace function ddl(
xs name [], -- 排除的schema
sql text -- 要执行的sql
) returns boolean as $$
declare
nsp name;
vs text;
begin
for nsp in SELECT n.nspname FROM pg_catalog.pg_namespace n
WHERE n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'
loop
if not xs @> array[nsp] then
vs := format('set search_path=%I,public;', nsp);
raise notice '%', vs;
execute vs;
execute sql;
end if;
end loop;
reset search_path;
return true;
exception when others then
reset search_path;
return false;
end;
$$ language plpgsql strict;
The xs parameter specifies the schema not involved in the execution, such as schemas unrelated to the tenant. Other tenant-related schemas are involved in the execution.
SQL parameters indicate the SQL statements that need to be executed in all schemas of the tenant.
Please visit this link to check whether the DDL scripts are consistent.
PostgreSQL Multi-Tenant Usage – Performance Impact Test of set search_path
General Usage of PostgreSQL hll in Retention and UV Statistics
ApsaraDB - September 27, 2021
Apache Flink Community China - July 28, 2020
Apache Flink Community China - November 6, 2020
ApsaraDB - July 26, 2024
Alibaba Cloud New Products - August 20, 2020
ApsaraDB - October 14, 2021
Alibaba Cloud PolarDB for PostgreSQL is an in-house relational database service 100% compatible with PostgreSQL and highly compatible with the Oracle syntax.
Learn MoreAn online MPP warehousing service based on the Greenplum Database open source program
Learn MoreResource management and task scheduling for large-scale batch processing
Learn MoreAn on-demand database hosting service for SQL Server with automated monitoring, backup and disaster recovery capabilities
Learn MoreMore Posts by digoal