Oracle-DB: How to fix ORA-04062 / find lazy PL/SQL dependencies across DB-links

If you have invalid PL/SQL objects due to later compilation of objects they depend on, then you can recompile them easily using
DBMS_UTILITY.compile_schema(schema => USER, compile_all => FALSE);
But what happens if your PL/SQL objects like procedures or packages are depending on resources in remote DBs via DB-link?
In this case your local objects remain valid, but the DB will check the remote dependencies directly at execution.

If the remote object was changed after the last DDL of the local object then the first execution of the local object will fail with ORA-04062.
An immediate compilation of the local object is forced than by the DB, so the second and further executions of the local object will work.

There are two flavours how the DB handles this lazy compilation, depending on the setting of REMOTE_DEPENDENCIES_MODE:
  • TIMESTAMP - The last DDL timestmp of the local object (User_Objects.Last_DDL_Time) is compared with the last DDL timestamp of the remote object in the remote DB
  • SIGNATURE - The last DDL timestmp of the local object (User_Objects.Last_DDL_Time) is compared with the last specification change of the remote object (User_Objects.Timestamp in the remote DB)
In result that means:
Simple compilation of a remote object may raise errors at next execution of your depending local PL/SQL object.
To avoid such sudden failures you need to check the timestamps of local and remote objects and recompile the affected objects yourself before executing them.

The following anonymous PL/SQL detects the objects that need recompile to avoid ORA-04062:
SET SERVEROUTPUT ON;
DECLARE
  Remote_Last_DDL_Time      DATE;
  Remote_Last_Spec_Time     DATE;
  Remote_Dependencies_Mode  VARCHAR2(20);
  Needs_Recompile           BOOLEAN;
BEGIN
  SELECT Value INTO Remote_Dependencies_Mode 
  FROM   v$Parameter 
  WHERE name = 'remote_dependencies_mode';
  FOR Rec IN (SELECT d.*, o.Last_DDL_Time, TO_DATE(o.Timestamp, 'YYYY-MM-DD:HH24:MI:SS') Last_Spec_Time 
              FROM   User_Dependencies d
              JOIN   User_Objects o ON o.Object_Name = d.Name AND o.Object_Type = d.Type
              WHERE  Referenced_Link_Name IS NOT NULL
             ) LOOP
    EXECUTE IMMEDIATE 'SELECT Last_DDL_Time, TO_DATE(Timestamp, ''YYYY-MM-DD:HH24:MI:SS'') 
                       FROM   ALl_Objects@'||Rec.Referenced_Link_Name||' 
                       WHERE  Owner        = :Owner 
                       AND    Object_Name  = :Name
                       AND    Object_Type  = :Type
                      '
    INTO Remote_Last_DDL_Time, Remote_Last_Spec_Time USING Rec.Referenced_Owner, Rec.Referenced_Name, Rec.Referenced_Type;
    IF Remote_Dependencies_Mode = 'TIMESTAMP' THEN
      Needs_Recompile := Remote_Last_DDL_Time >= Rec.Last_DDL_Time;
    ELSE /* SIGNATURE */
      Needs_Recompile := Remote_Last_Spec_Time >= Rec.Last_DDL_Time;
    END IF;
    IF Needs_Recompile THEN    
      DBMS_OUTPUT.PUT_LINE(Rec.Type||' '||Rec.Name||' with Last DDL = '||TO_CHAR(Rec.Last_DDL_Time, 'YYYY-MM-DD HH24:MI:SS')||
                           ', Last Spec = '||TO_CHAR(Rec.Last_Spec_Time, 'YYYY-MM-DD HH24:MI:SS')||' depends on '||
                           Rec.Referenced_Type||' '||Rec.Referenced_Owner||'.'||Rec.Referenced_Name||'@'||Rec.Referenced_Link_Name||
                           ' with Last DDL = '||TO_CHAR(Remote_Last_DDL_Time, 'YYYY-MM-DD HH24:MI:SS')||
                           ', Last Spec = '||TO_CHAR(Remote_Last_Spec_Time, 'YYYY-MM-DD HH24:MI:SS')
                          );
    END IF;                      
  END LOOP;           
END;
/

Comments

Popular posts from this blog

Panorama: Determining candidates for storage reorganization in Oracle-DB

Oracle-DB: Cleanup Unified Audit Trail with dynamic number of rows and oldest timestamp, but limited storage size

Oracle-DB: Estimate network latency of client connections by evaluation of Active Session History