F.48. pgpro_rp — resource prioritization #

pgpro_rp is a Postgres Pro Enterprise extension for resource prioritization.

On systems with limited resources or under heavy load, you may need to prioritize transaction execution, so that some transactions are executed more quickly than the others. For example, you may want to execute simple user queries as fast as possible, even if it delays less urgent tasks, such as complex OLAP queries that may be running at the same time. Postgres Pro Enterprise enables you to assign a resource prioritization plan to a particular session, which can slow it down based on the amount of CPU, I/O read, and I/O write resources this session consumes as compared to other sessions. These parameters can take weight values 1, 2, 4, and 8. The higher the value, the more resources the session can use. By default, all sessions have weight 4 for all types of resources. The resource prioritization plan contains a set of these prioritization parameters as a jsonb and can be assigned to any user or role.

The plan is selected during session creation using a login event trigger as follows:

  • First, pgpro_rp calls the plan selection function if it has been set. Otherwise, it searches for a plan assigned to the current user.

  • If no plan is assigned to the user, pgpro_rp selects the plan with the maximum sum of priorities from the plans assigned to the roles that the user is a member of.

  • Finally, the priority settings of the found plan are applied.

If the plan cannot be determined, the default priority settings are applied: all priorities have the value of 4.

Note

Only superusers can manage and update plans.

F.48.1. Installation #

The pgpro_rp extension is a built-in extension included into Postgres Pro Enterprise, but since pgpro_rp uses shared memory for caching user prioritization parameters, it has to be installed separately. Once you have pgpro_rp installed, complete the following steps to enable pgpro_rp:

  1. Add pgpro_rp to the shared_preload_libraries parameter in the postgresql.conf file:

                  shared_preload_libraries = 'pgpro_rp'
                
  2. Restart the Postgres Pro Enterprise instance for the changes to take effect.

  3. Create the pgpro_rp extension:

                  CREATE EXTENSION pgpro_rp;
                

F.48.2. Usage #

pgpro_rp extension supports the following operation modes:

F.48.2.1. Permanent Plan Mode #

This operation mode is used to set up permanent resource prioritization plans for users or roles. These plans remain unchanged regardless of the time of the day, the day of the week, etc. You don't have to create and set up plan assignment groups for users or roles. In other words, only the preinstalled default group is used implicitly.

This mode includes the following steps:

The created plans can be updated or deleted if needed.

Example:

        -- Create resource prioritization plans
        SELECT pgpro_rp_create_plan('plan_2_2_2', 2, 2, 2);
        SELECT pgpro_rp_create_plan('plan_8_8_8', '{ "session_cpu_weight":8,
        "session_ioread_weight":8, "session_iowrite_weight":8}');

        -- Assign plans to roles
        SELECT pgpro_rp_create_role_plan('users', 'plan_2_2_2');
        SELECT pgpro_rp_create_role_plan('admins', 'plan_8_8_8');
      

F.48.2.2. Changeable Plan Mode #

This operation mode is used to set up various resource prioritization plans for users or roles that are subject to changing in accordance with the time of the day, the day of the week, etc.

This mode includes the following steps:

Example:

        -- Create resource prioritization plans
        SELECT pgpro_rp_create_plan('plan_8_8_8', 8, 8, 8);
        SELECT pgpro_rp_create_plan('plan_4_4_4', 4, 4, 4);
        SELECT pgpro_rp_create_plan('plan_2_2_2', 2, 2, 2);
        SELECT pgpro_rp_create_plan('plan_1_1_1', 1, 1, 1);

        -- Create plan assignment groups to users or roles and add in plan assignment groups
        SELECT pgpro_rp_create_group('day_plan');
        SELECT pgpro_rp_create_group_role_plan('day_plan', 'top', 'plan_4_4_4');
        SELECT pgpro_rp_create_group_role_plan('day_plan', 'main', 'plan_2_2_2');
        SELECT pgpro_rp_create_group_role_plan('day_plan', 'hr', 'plan_1_1_1');

        SELECT pgpro_rp_create_group('night_plan');
        SELECT pgpro_rp_create_group_role_plan('night_plan', 'top', 'plan_8_8_8');
        SELECT pgpro_rp_create_group_role_plan('night_plan', 'main', 'plan_1_1_1');
        SELECT pgpro_rp_create_group_role_plan('night_plan', 'hr', 'plan_2_2_2');

        -- Set an active group
        CALL pgpro_rp_set_active_group('day_plan');
      

F.48.3. The pgpro_rp_roles_plans_view View #

All plan assignment groups along with the roles that are included in them, and the plans assigned to the roles are available via a view named pgpro_rp_roles_plans_view. This view contains one row for each role with an assigned plan. The columns of the view are shown in Table F.37.

Table F.37. pgpro_rp_roles_plans_view Columns

NameTypeDescription
group_nametextThe name of the group
rolnametextThe name of the user/role
plan_nametextThe plan used for the user/role

F.48.4. Functions #

F.48.4.1. Plan Management Functions #

The functions described in this section provide the ability to create, modify, or delete a resource prioritization plan.

pgpro_rp_create_plan(a_plan_name text, a_plan_options jsonb) returns bigint
pgpro_rp_create_plan(a_plan_name text, a_cpu_weight int, a_ioread_weight int, a_iowrite_weight int) returns bigint #

Creates a plan with the given name and options or prioritization parameters and returns the ID of the created plan.

pgpro_rp_rename_plan(a_plan_name_old text, a_plan_name_new text) returns void #

Renames the specified plan.

pgpro_rp_update_plan(a_plan_name text, a_plan_options jsonb) returns void #

Modifies the options of the specified plan.

pgpro_rp_delete_plan(a_plan_name text) returns void #

Deletes the specified plan.

pgpro_rp_get_plan_id_by_name(a_plan_name text) returns bigint #

Returns the ID of the created plan by the plan name.

F.48.4.2. Plan Assignment Group Functions #

The functions described in this section provide the ability to manage plan assignment groups for users or roles.

pgpro_rp_create_group(a_group_name text) returns bigint #

Creates a plan assignment group and returns the ID of the created group.

pgpro_rp_rename_group(a_group_name text, a_new_group_name text) returns void #

Renames the specified group.

pgpro_rp_delete_group(a_group_name text) returns void #

Deletes the specified group.

F.48.4.3. Plan Assignment Functions #

The functions described in this section provide the ability to manage plan assignment for roles.

The following functions work with an active plan assignment group specified in the pgpro_rp_options table:

pgpro_rp_create_role_plan(a_role_name text, a_plan_name text) returns void #

Assigns the default plan to the specified role.

pgpro_rp_update_role_plan(a_role_name text, a_plan_name text) returns void #

Changes the default plan for the specified role.

pgpro_rp_delete_role_plan(a_role_name text) returns void #

Deletes the plan assigned to the specified role.

Functions to add/delete in plan assignment groups for users or roles:

pgpro_rp_create_group_role_plan(a_group_name text, a_role_name text, a_plan_name text) returns void #

Adds the specified plan to the specified group.

pgpro_rp_delete_group_role_plan(a_group_name text, a_role_name text) returns void #

Deletes the specified plan from the specified group.

Other functions:

pgpro_rp_cleanup_roles_plans() returns void #

Deletes the plans assigned to the non-existent roles from the pgpro_rp_roles_plans table.

pgpro_rp_set_active_group(a_group_name text) returns void #

Makes the specified plan assignment group active and clears the prioritization parameter cache.

pgpro_rp_get_active_group() returns text #

Returns the ID of the active group.

F.48.4.4. Plan Selection Functions #

You can declare a custom function to select resource prioritization plans, which takes no arguments and returns the plan ID, to be called at the session start using the functions described below.

pgpro_rp_set_plan_selection_function(a_func_name text) returns void #

Sets the plan selection function. If NULL is passed, the current function is reset.

pgpro_rp_get_plan_selection_function() returns text #

Returns the current plan selection function.

F.48.4.5. Backend Plan Function #

pgpro_rp_backend_set_plan(a_pid int, a_plan_name text) returns void #

Searches for a plan with the given name and applies its settings to the backend with the specified PID.

F.48.4.6. Cache Functions #

When creating any session, the prioritization parameter values from the assigned plan are applied to this session. However, searching for a plan requires certain resources (you need to read data from several tables), so the values of user prioritization parameters are cached in shared memory. If you create another session, its user will be searched in the cache, and if the user is found, the values of the prioritization parameters stored in the cache will be applied to the newly created session. Since the values in the cache may expire, the following function is provided to clear the cache:

pgpro_rp_invalidate_cache() returns void #

Clears the cache of prioritization parameters in shared memory.

This function should be called in cases where user prioritization settings may change (assigning roles to users, changing the parameters of the active group/plans in use, etc.).

F.48.5. Configuration Parameters #

If necessary, you can also manage priorities manually:

  • Configure the time interval for collecting usage statistics for all active backends by setting the usage_tracking_interval parameter in the postgresql.conf. Avoid setting usage_tracking_interval to small values as frequent statistics collection can cause overhead.

  • Depending on the resources you need to control, modify one or more of the following parameters for the sessions you would like to prioritize:

    Sessions with the same weight have the same priority for resource usage, so if equal weights are assigned to all sessions, performance is not affected, regardless of the weight value.

    For all possible ways of modifying configuration for a particular session, see Section 19.1.

F.48.6. Example #

The following example demonstrates using pgpro-rp.

CREATE EXTENSION pgpro_rp;
CREATE USER test_user;
SELECT pgpro_rp_create_plan(
 'test_plan',
 '{ "session_cpu_weight":8, "session_ioread_weight":8, "session_iowrite_weight":8 }'
);
SELECT pgpro_rp_create_role_plan('test_user', 'test_plan');

The next time that the test_user user logs in, the prioritization parameters will look as follows:

SHOW session_cpu_weight;
 session_cpu_weight
--------------------
 8
(1 row)
SHOW session_ioread_weight;
 session_ioread_weight
-----------------------
 8
(1 row)
SHOW session_iowrite_weight;
 session_iowrite_weight
------------------------
 8
(1 row)