Professional Documents
Culture Documents
30
C H A P T E R
In This Chapter
How resource management works How to create a resource plan The DBMS_RESOURCE_ MANAGER reference The DBMS_RESOURCE_ MANAGER_PRIVS reference Resource management and the data dictionary
esource management is a new feature in Oracle8i that allows you to apportion CPU resources amongst various users and groups of users. The potential for one user or group of users to consume an inordinate amount of resources has always existed. In prior releases of Oracle, you could attempt to address such problems through user profiles, but user profiles are limited. They can cut off a user after that user consumes a certain amount of CPU or I/O, but they cant do the same for a group of users. Also, user profiles cannot meter resources such that a user can spread the effort of executing a large query out over a larger period of time. Oracle8is resource management feature brings flexibility to the table. You can apportion CPU time to different user groups on a percentage basis, and you can identify some groups as having a higher priority than others. By doing this, you can ensure that users who are performing critical functions always have the necessary CPU resources available, regardless of what other users may be doing at the same time. In this chapter, youll learn how to create and manage resource plans in a database.
780
Understanding priority
In addition to allocating CPU by percentage, you can also prioritize those allocations into as many as eight levels. By assigning priorities, you can further subdivide CPU capacity. With respect to the example shown previously in Figure 30-1, you could subdivide the decision support users into two groups: one for vice presidents and one for other executives. Figure 30-2 illustrates this, showing that the vice presidents get most of the decision support CPU capacity. Here, the customer care group gets 80 percent of CPU allocation. This is just as before. The remaining 20 percent still goes to the decision support users, but that 20 percent has been divided between two distinct subgroups. Vice presidents get 70 percent of the 20 percent allocated to decision support, while all other executives get 30 percent of that 20 percent.
781
Resource Plan
Plan Directives
80 percent of CPU
20 percent of CPU
Resource Plan
Figure 30-2: Decision support users will get bumped by the higher-priority customer care users.
782
Nesting plans
Resource plans may also be nested. You can divide one plan into two or more subplans. As with resource priorities, you can also use nested plans to divide and subdivide CPU allocation between various groups and subgroups. However, when a system is not fully loaded, Oracle distributes extra CPU time differently depending on whether plans or priorities are involved. Figure 30-3 shows another way of dividing the CPU time among the three consumer groups shown earlier.
Resource Plan
Plan Directives
Level 1 80 percent
Level 1 20 percent
Level 1 70 percent
Level 1 30 percent
Plan Directives
At first glance, it appears that the results of using nested resource plans are the same as you get when you prioritize CPU time. A difference does exist, though, and its an important one. The key to understanding the difference lies in understanding
783
that extra CPU time is allocated to other users in the same plan based on the priority of those users. With a single plan, such as that shown in Figure 30-2, any extra CPU time is allocated to users on a priority basis. So if the vice presidents arent using all of their allotted CPU time, Oracle will reallocate that CPU time on a priority basis. Thus, it might go to the customer care users because they are priority 1 before it would go to the priority 2 users. The rules change, however, when a resource plan contains one or more subplans. As long as there is at least one user for a subplan, the subplan gets all of its CPU allotment. With respect to Figure 30-3, if there was only one decision support user, that user would be able to use all of the CPU allocated to the decision support plan. In other words, excess CPU allocation within the decision support group goes to other decision support users. Only if no decision support users are logged on at all will that 20 percent become available to the customer care users.
784
Prerequisites
To work with resource plans, you need to hold a special privilege known as ADMINISTER_RESOURCE_MANAGER. This privilege is not a standard system privilege, nor is it a role. Its a privilege that exists only within the context of resource management.
The third argument indicates whether the user getting the privilege is allowed to further grant that privilege to others. A value of TRUE allows the user to do that. A value of FALSE prevents the user from further granting the privilege. This is exactly like the WITH ADMIN OPTION of SQLs GRANT command.
Note
Granting someone the ADMINISTER_RESOURCE_MANAGER privilege allows him or her to create and manage plans, but it doesnt provide access to the system views that return information about resource plans. You need to grant SELECT access to those views as a separate operation.
The USER_RSRC_MANAGER_SYSTEM_PRIVS view returns the same information, but only for the current logged-on user. You can query from that if you just want to check whether you have the privilege yourself.
785
Note
The DBA_SYS_PRIVS view also shows who has the ADMINISTER RESOURCE MANAGER privilege. Thats rather odd, because you cant use the standard GRANT and REVOKE commands to manage it.
Everything else that you do now will be done in the pending area. When the new plan is defined as you want it to be, you can commit the pending changes.
Creating these plans really gives you nothing more than two names. For those plans to mean anything, you need to define consumer groups and then create plan directives to allocate resources among those groups. Also, the link between the two plans is defined using a plan directive. Youll see how this is done later in this chapter.
786
The next step is to link your newly created consumer groups to your resource plans.
787
allocate. The top-level plan has 100 percent of those resources. The directives for each plan must then further allocate the resources that have been assigned to that plan. In our case (refer back to Figure 30-3), the first split involves giving 80 percent of database resources to the customer care group and 20 percent to the decision support group. The code in Listing 30-2 makes this initial 80/20 split.
The first call to CREATE_PLAN_DIRECTIVE allocates 70 percent of CPU time to the customer care group under the normal plan. The second call allocates 20 percent of CPU time to the decision support group. Why a 70/20 split instead of the 80/20 split called for in our scenario? The missing 10 percent is for a special group called OTHER_GROUPS. Oracle requires that some resources be set aside for users and groups for which you have not explicitly allocated resources. To further subdivide the decision support groups 20 percent, you execute the code shown in Listing 30-3.
788
The first parameter to the CREATE_PLAN_DIRECTIVE procedure is the plan name. This time, the plan name given is DECISION. Thats because this 70/30 split applies to the decision support group. Finally, assign that 10 percent that we held back earlier to the OTHER_GROUPS group. Here is the code to do this. Notice that the 10 percent comes from the plan named NORMAL. It could come from any plan, but NORMAL is where we held back the 10 percent, so well use this 10 percent.
BEGIN DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE ( NORMAL, OTHER_GROUPS, For users not part of any group., cpu_p1 => 10 ); END; /
The OTHER_GROUPS parameter is a group name that Oracle recognizes automatically. You dont have to create that group. You just have to create a plan directive that allocates resources to it.
789
If validation fails, you will receive an error message. The following message shows the error message that you will receive if you forget to allocate resources for OTHER_GROUPS:
ERROR at line 1: ORA-29382: validation of pending area failed ORA-29377: consumer group OTHER_GROUPS is not part of top-plan NORMAL ORA-06512: at SYS.DBMS_RMIN, line 249 ORA-06512: at SYS.DBMS_RESOURCE_MANAGER, line 254 ORA-06512: at line 2
If you receive an error, you need to fix it and then retry the validation. The DBMS_ RESOURCE_MANAGER package does implement procedures for updating and deleting resource plans, consumer groups, and plan directives. These procedures are described later in this chapter.
790
If all youve done is modify existing plans and directives, you can probably stop here. If you are implementing resource management for the first time, you will now need to assign users to the new consumer groups that youve created. After that, you will need to issue an ALTER SYSTEM command to activate your new plan.
Note
The SUBMIT_PENDING_AREA procedure makes an implicit call to VALIDATE_ PENDING_AREA. Changes are always validated prior to being submitted.
You can actually grant a user switch privileges on several consumer groups, enabling that user to switch back and forth between groups at will. That can be useful if you have one user who runs two different types of applications. If youve written the applications in-house, you can program the applications to automatically switch the user to the appropriate consumer group.
791
Tip
If you dont want users changing their consumer group setting, then grant them switch privileges only on one group, and make that group their initial group. They wont be able to switch out of it.
Once logged on, users can switch themselves to other consumer groups, providing they have been granted access to those groups, by using the DBMS_RESOURCE_ MANAGER packages SWITCH_CURRENT_CONSUMER_GROUP procedure. You can also change groups for a user by using the SWITCH_CURRENT_CONSUMER_GROUP_FOR_ SESS and SWITCH_CURRENT_CONSUMER_GROUP_FOR_USER procedures. Youll read more about these procedures later in this chapter.
SYSTEM
The RESOURCE_MANAGER_PLAN parameter defines the current resource plan for an instance. The following ALTER SYSTEM command sets it to the plan created in this chapter, which is named NORMAL:
ALTER SYSTEM SET resource_manager_plan = normal;
To set the plan used when you first start the instance, you can place a line like the following in your instances initialization parameter file:
resource_manager_plan = normal
792
Using the ALTER SYSTEM command, you can change resource plans at will. You can use one plan during the day and a different plan at night. One plan might tilt the balance of resources towards online users, while the other might favor decision support users. The possibilities are endless. Oracle8is resource management feature gives you a great deal of flexibility in controlling how a database is used.
Note
Remember that you must stop and restart your database before any changes that you make in your initialization parameter file take effect.
793
Customer Care, Resource allocation for customer care group., cpu_p1 => 70 ); DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE ( NORMAL, VP DSS Users, Resource allocation for the vice presidents, cpu_p2 => 70 ); DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE ( NORMAL, Other DSS Users, Resource allocation for the other executives, cpu_p2 => 30 ); --Dont forget the special case of OTHER_GROUPS. DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE ( NORMAL, OTHER_GROUPS, For users not part of any group., cpu_p1 => 10 ); END; /
Notice that this listing contains only one plan: the Normal plan. Three consumer groups are defined the same as in the previous scenario. The major difference is in how resources are allocated to those groups. The customer care group is allocated 70 percent of CPU resources at level 1. That leaves 30 percent for the other two groups. That 30 percent is allocated between the other groups using the CPU_P2 parameter. Level 1 users take priority over any level 2 users, so if the vice presidents arent using all of their CPU allocation, the customer care users, not the other executives, will get first crack at it.
Making changes
To make changes to resource plans, consumer groups, and plan directives, you need to follow this procedure: 1. Create a pending area. 2. Make whatever changes you need to make.
794
3. Validate the pending area. 4. Commit the changes in the pending area. This procedure is not very different from the one used to create a resource plan in the first place. The key difference is that here you are making modifications. The DBMS_RESOURCE_MANAGER package contains a number of routines that can be used to modify consumer groups and plan directives, and the interface to these routines is very similar to the various create routines that youve read about so far. For example, Listing 30-5 shows two plan directives being modified. Ten percent is taken from the customer care group and given over to the special group named OTHER_GROUPS.
In the same manner, you can also update consumer groups, delete consumer groups, delete plan directives, and even delete resource plans.
795
Notice the period separating the package name from the procedure name. The use of a period for that purpose is standard within PL/SQL.
Optional parameters
Many of the procedures in the DBMS_RESOURCE_MANAGER package have optional parameters in their interfaces. An optional parameter is one that you dont have to pass when you call the procedure. The nature of these procedures is such that its quite common to omit a number of parameters, so passing parameters using named notation rather than position notation is often convenient. You use positional notation to pass parameters by position, as shown in the following example:
DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE ( my plan, my group, null, null, 80);
In this example, the first parameter that is listed in the procedure call corresponds to the first formal parameter defined for the procedure, the second parameter that is listed corresponds to the second formal parameter, and so forth. The 80 in this example corresponds to the CPU_P2 parameter. The two NULL parameters correspond to the COMMENT and CPU_P1 parameters. Nulls need to be passed in as placeholders here, to make the value of 80 correspond with the fifth formal parameter, which is CPU_P2.
796
An easier way to skip parameters is to use named notation. By naming each parameter explicitly, you relieve yourself of the burden of passing nulls for parameters that you want to skip. You can even mix positional and named location if you like. The following example uses positional notation for the first two param-eters and then switches to named notation for the third:
DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE ( my plan, my group, CPU_P2=>80);
No values at all were specified for the COMMENT and CPU_P1 parameters, which has the same effect as passing in nulls.
Note
With named notation, your parameters dont need to be passed in any particular order. With positional notation, the order determines the correspondence between the actual and formal parameters.
There are no parameters for this procedure. If the procedure detects any problems with the changes that youve made in the pending area, you will receive an error message. Fix the problem identified by the error message, and try the validation again. If you have multiple problems, you may have to iterate through this process several times.
797
There are no parameters for this procedure. Note that a call to SUBMIT_PENDING_ AREA implies a call to VALIDATE_PENDING_AREA as well. Oracle wont allow the changes to be submitted if a validation error occurs.
There are no parameters for this procedure. Clearing the pending area gets rid of it completely. If you want to make changes afterwards, you will need to issue another call to CREATE_PENDING_AREA.
Resource plans
Resource plans describe how a database is to allocate resources. The DBMS_ RESOURCE_MANAGER package contains procedures for creating, updating, and deleting these plans.
The following list describes the parameters in this syntax: plan A name for the plan that you are creating. This may be up to 30 characters long.
798
comment A comment describing the plan. This may be up to 2,000 characters long. cpu_mth The method to use in allocating CPU resources. The default is EMPHASIS. max_active_sess_target_mth The method to use in allocating the maximum number of sessions. The default is MAX_ACTIVE_SESS_ABSOLUTE. parallel_degree_limit_mth The method to use in limiting the degree of parallelism. This defaults to PARALLEL_DEGREE_LIMIT_ABSOLUTE, which is currently the only supported method.
See the CREATE_RESOURCE_PLAN procedure for descriptions of these parameters. When updating a plan, identify the plan by name and pass in only those parameters whose values you want to change. To change a plans comment, for example, identify the plan by using the PLAN parameter and pass the new comment in the COMMENT parameter.
Note
You cant change a plans name. If you dont like the name, you must delete and re-create the plan.
Pass a resource plan name, using the plan parameter, to identify the plan that you want to delete. If you want to delete not only a resource plan but also any plan directives, consumer groups, and subplans associated with the plan, you can use the following DELETE_RESOURCE_PLAN_CASCADE procedure:
DELETE_PLAN_CASCADE ( plan IN VARCHAR2);
799
Use the plan parameter to pass in the name of the plan that you want to delete. When using DELETE_PLAN_CASCADE, be sure that you really do want to delete all the consumer groups and subplans that fall under the plan that you are deleting.
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
The following list describes the parameters for this procedure: plan The name of the resource plan to which you want the directive to be attached. group_or_subplan The name of a group or another plan. This directive specifies the amount of resources to be allocated to the group or plan that you name. comment A comment about this directive.
800
cpu_p1 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 1 allocation, with 1 being the highest priority. cpu_p2 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 2 allocation. cpu_p3 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 3 allocation. cpu_p4 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 4 allocation. cpu_p5 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 5 allocation. cpu_p6 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 6 allocation. cpu_p7 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 7 allocation. cpu_p8 A value from 0100 specifying the percentage of CPU to allocate to the named group or plan. This is a priority 8 allocation. max_active_sess_target_p1 A target for the maximum number of sessions. This parameter is currently ignored. Oracle plans to add this functionality sometime in the future. parallel_degree_limit_p1 A limit on the degree of parallelism that users who fall under this particular directive can use.
801
IN IN IN IN
See the CREATE_PLAN_DIRECTIVE procedure for parameter descriptions. To update a directive, pass in the plan and group_or_subplan parameters to identify the directive. Then pass in values only for those parameters that you wish to change.
Together, the plan and group_or_subplan parameters identify the directive that you want to delete.
Consumer groups
Consumer groups represent groups of users who share a common need for resources. Rather than allocating resources to each user individually, you assign each user to an appropriate group and allocate resources to that group.
The following list describes the parameters for this procedure: consumer_group The name of the consumer group that you want to create. comment A comment describing the consumer group. cpu_mth The CPU resource allocation method to use for this consumer group. The default method is ROUND-ROBIN, which is also the only method currently supported.
802
See the CREATE_CONSUMER_GROUP procedure for the parameter descriptions. To update a consumer group, identify the consumer group by name using the consumer_group parameter, then pass in new values for the other parameters that you want to change.
Pass the consumer group name in the consumer_group parameter to identify the group that you want to delete.
The following list describes the parameters for this procedure: user The name of the user consumer_group The name of the consumer group to which you want to assign the user
803
For a user to be part of a consumer group, that user must be granted switch privileges on that group. Use the DBMS_RESOURCE_MANAGER_PRIVS package, described later in this chapter, to grant access to a consumer group.
The following list describes the parameters for this procedure: session_id The session ID number for the session that you want to switch session_serial The session serial number for the session that you want to switch consumer_group The new consumer group assignment for that session Together, the session_id and session_serial parameters identify the session that you want to switch. You can get a list of active sessions, including IDs and serial numbers, by querying the v$session view.
The following list describes the parameters for this procedure: user The user that you want to switch consumer_group The new consumer group assignment for that user When you issue a call to the SWITCH_CONSUMER_GROUP_FOR_USER procedure, all current sessions for the specified user will be switched.
Note
The SWITCH_CONSUMER_GROUP_FOR_USER procedure doesnt change the users initial setting. The next time the user connects, the user will be assigned to the group specified as his or her initial group. To change the initial group, use the SET_INITIAL_CONSUMER_GROUP procedure.
804
The procedures described in this section are all part of the DBMS_RESOURCE_ MANAGER_PRIVS package. Remember that when referencing a procedure, you must prepend the package name to the procedure name. You reference GRANT_ SYSTEM_PRIVILEGE, for example, by writing the following: DBMS_RESOURCE_MANAGER_PRIVS.GRANT_SYSTEM_PRIVILEGE
The following list describes the parameters for this procedure: grantee_name The name of the user or role to which you want to grant the privilege. privilege_name The name of the privilege to grant. Currently, the only privilege that you can grant is ADMINISTER_RESOURCE_MANAGER. admin_option A TRUE condition allows the grantee to grant this privilege to other users. A FALSE condition does not.
805
Revoking ADMINISTER_RESOURCE_MANAGER
You use the REVOKE_SYSTEM_PRIVILEGE procedure to revoke the ADMINISTER_ RESOURCE_MANAGER privilege from a user or a role, as follows:
REVOKE_SYSTEM_PRIVILIGE ( grantee_name IN VARCHAR2, privilege_name IN VARCHAR2 DEFAULT ADMINISTER_RESOURCE_MANAGER);
The following list describes the parameters for this procedure: grantee_name The name of the user or role from which you want to revoke a privilege. privilege_name The name of the privilege to revoke. Currently, this must be ADMINISTER_RESOURCE_MANAGER.
Switch privileges
Switch privileges are issued to users to allow them to switch themselves to a resource consumer group. If you assign a default resource consumer group to a user, you must at least grant the user switch privileges to that group. If you want the user to be able to choose from among several resource groups, you must grant switch privileges for each of those groups.
Note
To restrict a user to just one consumer group, grant switch privileges to the user of the group and make the group the users default consumer group.
The following list describes the parameters for this procedure: grantee_name The name of the user or role to which you want to grant the privilege. consumer_group The name of a consumer group. This is the group to which the privilege applies.
806
grant_option The grant option flag. This may be either TRUE or FALSE, and it works like the WITH GRANT OPTION of the SQL GRANT command. A TRUE condition allows the user to further grant the identical switch privilege to other users. A FALSE condition does not.
The following list describes the parameters for this procedure: grantee_name The name of the user or role from which you want to revoke the privilege consumer_group The name of a consumer group
DBA_RSRC_CONSUMER_GROUP_PRIVS
DBA_RSRC_CONSUMER_GROUPS DBA_RSRC_MANAGER_SYSTEM_PRIVS
ADMINISTER_RESOURCE_MANAGER
privilege.
DBA_RSRC_PLAN_DIRECTIVES DBA_RSRC_PLANS
Returns information about all resource plan directives. Returns information about all resource plans.
807
View Name
Description Returns information about all users in the database. The INITIAL_RSRC_ CONSUMER_GROUP column shows you the initial consumer group assignment for each user. Tells you to which consumer groups you have access. Tells you whether you have been granted the ADMINISTER_RESOURCE_MANAGER privilege. Returns information about available parallel degree limit methods. Returns information about all currently active resource consumer groups. Returns information about available CPU allocation methods for consumer groups. Returns the names of all currently active resource plans. Returns information about available resource allocation methods. Returns information about all active database sessions. The RESOURCE_ CONSUMER_GROUP column shows you the consumer group to which each session currently belongs.
DBA_USERS
USER_RSRC_CONSUMER_GROUP_PRIVS USER_RSRC_MANAGER_SYSTEM_PRIVS
As with most data dictionary views, the column names are fairly self-explanatory. In the following example, the DBA_RSRC_CONSUMER_GROUP_PRIVS view is queried to see a list of switch privileges held by the users:
SQL> SELECT * 2 FROM dba_rsrc_consumer_group_privs 3 ORDER BY grantee, granted_group; GRANTEE --------------PUBLIC PUBLIC SEAPARK SYSTEM GRANTED_GROUP ---------------------DEFAULT_CONSUMER_GROUP LOW_GROUP CUSTOMER CARE SYS_GROUP GRANT ----YES NO NO NO INI --YES NO YES YES
The INITIAL column in this view indicates the default consumer group assignment.
808
Summary
In this chapter, you learned: Resource management is a new Oracle feature that allows you to allocate CPU resources among users and groups of users. Resource management requires that users be divided into groups called consumer groups. A resource plan is then put into effect for the database. That plan contains plan directives that specify how CPU resources are to be allocated among the various consumer groups. For added versatility, subplans may also be used. Resource plan changes are always made in a pending area, which you must first create using DBMS_RESOURCE_MANAGER.CREATE_PENDING_AREA. After completing your changes, validate them by calling DBMS_RESOURCE_MANAGER. VALIDATE_PENDING_AREA. If validation succeeds, submit the changes by calling the DBMS_RESOURCE_MANAGER.SUBMIT_PENDING_AREA procedure. Submitting the changes makes them permanent. To manage resource plans and consumer groups, you need to hold the ADMINISTER_RESOURCE_MANAGER privilege. This isnt quite the same as a system privilege, and its granted using the DBMS_RESOURCE_MANAGER_ PRIVS.GRANT_SYSTEM_PRIVILEGE procedure.