This topic describes the multi-tenant resource configuration feature of PolarDB for PostgreSQL.
Concepts
Tenant: In multi-tenancy, a tenant is a logical entity that represents a specific unit of data and resource allocation. In the hierarchy, a tenant sits below the cluster and above users and databases. The relationship is many-to-one: multiple accounts or databases map to one tenant. A tenant is not the same concept as a user or a database. Tenants fall into two categories:
System tenant: a special management entity, of which there is usually only one. The system tenant has priority access to resources and can occupy the resources of regular tenants. When a user of the system tenant connects to a database and that user has the corresponding cluster access permissions, the user can access the clusters under all tenants.
Regular tenant: the resources of each regular tenant are fully isolated, and regular tenants cannot access each other. A regular tenant must be created in the context of the system tenant.
Resource configuration: A resource configuration allocates specific resources, such as CPU and memory, to a specific tenant. Privileged accounts can use resource configurations to set resource limits for each tenant based on business requirements.
The tenant resource configuration feature limits the amount of resources that one or more processes use. In PolarDB for PostgreSQL, one session corresponds to one process, and one session allows only one user to log on to one database. Processes, users, and databases therefore form the basic elements of a process. You can map any process, user, or database to a specified tenant, but a process can belong to only one tenant, which prevents resource usage from being counted twice. The child processes of a background parallel query belong to the tenant of the process that initiated the query. Based on process attributes, resource limits are divided into three dimensions:
Process: the account connection processes (session processes) that users initiate, including the parallel query processes associated with them, but excluding system auxiliary processes.
User: all user connection processes (session processes) initiated by the same user, including the parallel query processes associated with them, but excluding system auxiliary processes.
Database: all user connection processes (session processes) that access the same database, including the parallel query processes associated with them, but excluding system auxiliary processes.
Because of the specific functions that system processes perform, their resource usage is not limited by default.
Applicable versions
The supported versions of PolarDB for PostgreSQL are as follows: PostgreSQL 14, and the minor engine version must be 2.0.14.12.24.0 or later.
You can view the minor engine version in the console or by running the SHOW polardb_version; statement. If the minor engine version does not meet the requirements, upgrade the minor engine version.
Parameters
Parameter | Description | How changes take effect |
polar_max_tenants | The maximum number of tenants. Valid values: 0 to 65536. Default value: 32. | Takes effect after a restart. |
polar_resource_manager.enable_resource_manager | Specifies whether to start the Resource Manager process to enforce memory limits and prevent out-of-memory (OOM) errors. Default value: on. | Takes effect after a restart. |
polar_resource_manager.database_name | The name of the database that stores tenant metadata. Default value: | Takes effect after a restart. |
polar_resource_manager.stat_interval | The data collection interval, in milliseconds. Valid values: 10 to 10000. Default value: 500 ms. | Takes effect after RELOAD. |
polar_resource_manager.total_mem_request_rate | The proactive eviction threshold. Valid values: 50% to 100%. Default value: 80%. | Takes effect after RELOAD. |
polar_resource_manager.total_mem_limit_rate | The forced eviction threshold. Valid values: 50% to 100%. Default value: 95%. | Takes effect after RELOAD. |
polar_resource_manager.total_mem_limit_remain_size | The reserved memory size. Valid values: 131072 to INT_MAX (the maximum value of the integer data type). Unit: KB. Default value: 256000 KB. | Takes effect after RELOAD. |
polar_resource_manager.enable_log | Specifies whether to enable logging. Valid values:
| Takes effect after RELOAD. |
Usage notes
To use multi-tenancy, in the database specified by the polar_resource_manager.database_name parameter, install the polar_resource_manager extension.
If you change the database that the polar_resource_manager.database_name parameter specifies for storing tenant metadata, the multi-tenancy information that you previously configured becomes invalid.
CREATE EXTENSION polar_resource_manager;Create a resource configuration
Use the polar_create_resource_config function to create a resource configuration.
SELECT polar_resource_manager.polar_create_resource_config('resource_config_name');The resource_config_name parameter must comply with the database object naming rules. Only names within 64 bytes are supported. Names longer than 64 bytes are automatically truncated.
Delete a resource configuration
Use the polar_drop_resource_config function to delete a resource configuration.
SELECT polar_resource_manager.polar_drop_resource_config('resource_config_name');Modify a resource configuration
Use the polar_alter_resource_config function to modify the resource settings of a resource configuration.
SELECT polar_resource_manager.polar_alter_resource_config('resource_config_name', 'config_name', value);The supported config_name types are as follows:
cpu_rate_limit: the CPU limit, in number of cores.mem_limit: the memory limit, in bytes.
Create a tenant
Use the polar_create_tenant function to create a tenant.
SELECT polar_resource_manager.polar_create_tenant('tenant_name', 'resource_config_name');Note the following:
tenant_namemust comply with the database object naming rules. Only names within 64 bytes are supported. Names longer than 64 bytes are automatically truncated.resource_config_namespecifies the resource configuration, which must be created first. Otherwise, the tenant cannot be created.
Delete a tenant
Use the polar_drop_tenant function to delete a tenant. Deleting a tenant does not delete the resource configuration.
SELECT polar_resource_manager.polar_drop_tenant('tenant_name');Modify a tenant
Use the polar_alter_tenant function to modify a tenant.
SELECT polar_resource_manager.polar_alter_tenant('tenant_name', 'config_name', 'value');The supported config_name types are as follows:
name: the tenant name.resource_config: the resource configuration.
Assign a database to a tenant
Use the polar_tenant_add_database function to assign a database to a tenant. A database can belong to only one tenant. Otherwise, the assignment fails.
SELECT polar_resource_manager.polar_tenant_add_database('tenant_name', 'database_name');View databases and tenants
Use the polar_tenants_dbs view to query databases and the tenants to which they belong.
SELECT dbsname, tenantname FROM polar_resource_manager.polar_tenants_dbs;Assign a user to a tenant
Use the polar_tenant_add_user function to assign a user to a tenant. A user can belong to only one tenant. Otherwise, the assignment fails.
SELECT polar_resource_manager.polar_tenant_add_user('tenant_name', 'user_name');View users and tenants
Use the polar_tenants_users view to query users and the tenants to which they belong.
SELECT username, tenantname FROM polar_resource_manager.polar_tenants_users;Assign a process to a tenant
Use the polar_tenant_add_process function to assign a process to a tenant. A process can belong to only one tenant. If a process is assigned to multiple tenants, the last assignment takes effect.
SELECT polar_resource_manager.polar_tenant_add_process('tenant_name', pid);View tenant resource usage
Use the polar_all_resource_configs_detail view to query the resource usage of tenants.
SELECT * FROM polar_resource_manager.polar_all_resource_configs_detail;The view contains the following columns:
Column name | Column type | Description |
tenantname | NAME | The tenant name. |
resource_config_name | NAME | The resource configuration name. |
num_processes | INTEGER | The number of processes. |
num_idle_processes | INTEGER | The number of idle processes. |
num_active_processes | INTEGER | The number of active processes. |
cpu_rate_limit | DOUBLE PRESISION | The CPU resource limit of the tenant. |
per_process_cpu_rate_limit | DOUBLE PRESISION | The CPU limit of the process. |
mem_limit | DOUBLE PRESISION | The memory limit. |
mem_usage | DOUBLE PRESISION | The memory usage. |
idle_processes_mem_usage | DOUBLE PRESISION | The memory usage of idle processes. |
active_processes_mem_usage | DOUBLE PRESISION | The memory usage of active processes. |
cpu_usage_rate | DOUBLE PRESISION | The CPU usage. |
CPU resource management
After you add processes, users, or databases to the corresponding tenant, you can set the CPU-related limit values in the resource configuration of the tenant. If backend child processes are allocatable, they are added to the tenant of the session connection that issues the parallel query. The backend child processes and the session process then share the resources limited by the tenant. When the parallel query of the session finishes, the backend child processes are removed from the tenant. Resource limits are therefore added and removed dynamically.
CPU usage limit
Limits the maximum CPU usage of the objects in a tenant. When the CPU usage of an object in a tenant is too high, the cluster throttles it and reduces the CPU usage to below the limit.
Use polar_resource_manager.polar_alter_resource_config to set cpu_rate_limit. The parameter value is of the float type.
The parameter value represents the CPU usage, which is the proportion of CPU time used over a period. For example, 0.3 indicates 30% of one core, and 2 indicates the capacity of two cores. Only the maximum usage is limited, so the sum of the cpu_limit values of all tenant resource configurations can exceed the specification quota T. That is, cpu_limit_A + cpu_limit_B + cpu_limit_C + ... >= T.
Memory resource management
Memory resources are rigid resources (swap off) and cannot be limited dynamically in the way that CPU can. When memory resources exceed the hard limit, resources are usually released by terminating processes. Tenant memory management works like the OOM handling strategy of an operating system, except that it operates at the tenant level rather than at the database level of the whole system.
Memory usage limit
Use polar_resource_manager.polar_alter_resource_config to set mem_limit. The parameter value is of the float type. Similar to the CPU usage limit, this setting limits the maximum memory usage. The total limit can be greater than or equal to the specification quota. The following two strategies are supported:
Proactive resource over-limit management: When the total memory usage exceeds the proactive eviction threshold (
total_mem_request_rate, 80% by default), if the memory usage of a tenant exceeds the memory limit in its resource configuration, the corresponding sessions are terminated to release resources. A memory over-limit error is returned to the user. This continues until the memory usage drops below the proactive eviction threshold.Forced resource over-limit management: To prevent excessive cluster memory usage from causing a cluster OOM error, when the total memory usage exceeds the forced eviction threshold (
total_mem_limit_rate, 95% by default), all processes are scanned. The resources of the corresponding sessions are released, and a memory over-limit error is returned to the user.
When a memory limit triggers eviction, user sessions are terminated and process resources are released by sending SIGUSR2, and the specified OOM error is returned to the user. If a background process of a parallel query exceeds its memory limit, the corresponding user process is terminated. Only user processes are limited. System background processes belong to the system tenant and are not proactively terminated to release resources.