Failure analysis and solution of running batch stored procedures or functions in Oracle production

 

Failure analysis and solutions of running batch stored procedures or functions in Oracle production:

Error message:

Cause Analysis:

1. When we compile a stored procedure or function, all Oracle objects referenced by the procedure or function will be recorded in the data dictionary.

The process relies on these stored objects. We can see that a subroutine marked as illegal has a compile error in the data dictionary.

Likewise, a stored subroutine would be illegal if a DDL operation was performed on its associated object. When an object changes, its related objects become illegal objects.

If all objects are in the same database, the related objects will enter an invalid state at the same time as the underlying object changes. Since the data dictionary is constantly tracking the correlation between objects, such changes can be quickly reflected.

2. Why does the process look different under the remote call?

The answer is that the data dictionary does not keep track of remotely related objects. In fact, since the remote objects may be in different databases, it is practically impossible to invalidate all related remote objects (the data dictionary may not be able to access the remote objects if they are in the invalid period). Unlike the above, the validity of the remote object is checked at runtime.

error scene

1. The object referenced by the process is invalid, for example: table structure change

2. There is a problem with dblink during operation

 

View stored procedure status:

# select t1.owner,t1.object_name,t1.object_type,t1.status,t1.created,t1.last_ddl_time from all_objects t1 where t1.owner = 'XIAOGAOKUI' and t1.object_type = 'PROCEDURE' and t1.status = 'INVALID'

View objects referenced by a procedure

# select t2.owner,t2.name,t2.type,t2.referenced_owner,t2.referenced_name from all_dependencies t2 where t2.owner = 'XIAOGAOKUI' order by 2;

View error messages during compilation

#select * from all_errors;

 

Compile the INVALID procedure

method one:

in Oracle sqlplus

# spool compile_invalid_porc.sql --record recompile statement

# select 'ALTER PROCEDURE' || t1.object_name || ' COMPILE;' from all_objects t1 where t1.status = 'INVALID' and t1.object_type = 'PROCEDURE' and t1.owner = 'XIAOGAOKUI'

# spool off

# @compile_invalid_porc.sql

 

Method two:

create or replace procedure compite_invalid_procedures(

p_owner varchar2 -- owner name, ie schema

)

as

--Compile the invalid process under the user

v_sql_statement varchar2(2000);

begin

for invalid_proc in (select t1.object_name as object_name from all_objects t1 where t1.owner = upper(p_owner) and t1.object_type = 'PROCEDURE' and t1.status = 'INVALIED')

loop

v_sql_statement := 'ALTER PROCEDURE' || invalid_proc.object_name || 'COMPILE';

begin

execute immediate v_sql_statement

exception

when others then

dbms_output.put_line(sqlcode || sqlerrm);

end;

end loop;

end;

/

 

Solutions in production:

Before calling the stored procedure, add a compile statement to the procedure:

method one:

# EXECUTE IMMEDIATE 'ALTER PROCEDURE COMPILE_INVALID_PROCEDURES COMPILE';

Method Two:

Create a stored procedure and execute it when needed, or create a timed task exec dbms_job.submit(:job_id,'timer_auto_recompile_objs;',sysdate,'sysdate+1/24'); timed execution.

create or replace procedure timer_auto_recompile_objs

as cursor objects_list is select object_name,object_type from user_objects where status='INVALID';

begin for v_object in objects_list loop

if v_object.object_type='PROCEDURE'

then execute immediate 'alter procedure '||v_object.object_name||' compile';

elseif v_object.object_type='FUNCTION'

then execute immediate 'alter function '||v_object.object_name||' compile';

elsif v_object.object_type='VIEW'

then execute immediate 'alter view '||v_object.object_name||' compile';

elsif v_object.object_type='MATERIALIZED VIEW'

then execute immediate 'alter materialized view '||v_object.object_name||' compile';

end if;

end loop;

end;

 

appendix:

Compilation process:

alter procedure New_procedure compile ;

To be able to execute this command, you need to own the procedure, or have the alter any procedure system privilege.

 

Compile function:

alter function New_function compile ;

To be able to execute, you need to have this function, or have the alter any procedure system privilege.

 

Compile the package:

alter package [user.]package_name compile  [package|body];

To be able to execute, you need to have this package, or have the alter any procedure system privilege.

 

replace:

Procedures, functions and packages can be replaced using the respective Create or replace commands.

With the or replace clause, the permissions that these objects have been given are preserved.

 

delete:

Drop procedure: drop procedure New_procedure;

Delete function: drop function New_function;

To delete a package: drop package New_package;

Delete package body: drop package body New_package;

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325558203&siteId=291194637