Showing posts with label invalid. Show all posts
Showing posts with label invalid. Show all posts

Tuesday, March 18, 2014

INVALID Materialized View with COMPILATION_ERROR

In this example, Andrew grants access on a table to a role then grants this role to John. John creates a materialized view on Andrew’s table. Andrew updates his table, making John’s materialized view INVALID. John then tries to compile his materialized view, which gives it a COMPILE_STATE of COMPILATION_ERROR. Andrew then grants access to his table directly to John and the problem goes away. It was tested on Oracle 11.2:
 
First, user Andrew creates a table with one row of data: 

SQL> conn andrew/reid
Connected.
SQL> create table tab1 (col1 number)
  2  /
 
Table created.
 
SQL> insert into tab1 values (1)
  2  /
 
1 row created.
 
SQL>

Then he creates a role and grants access on the table to it:
 
SQL> create role role1
  2  /
 
Role created.
 
SQL> grant select on tab1 to role1
  2  /
 
Grant succeeded.
 
SQL> 

A new user called John is set up. He has access to Andrew’s table via ROLE1 and he creates an ON DEMAND materialized view on it:
 
SQL> conn / as sysdba
Connected.
SQL> create user john identified by smith
  2  default tablespace users
  3  quota unlimited on users
  4  /
 
User created.
 
SQL> grant create session,
  2        create table,
  3        create materialized view,
  4        role1
  5        to john
  6  /
 
Grant succeeded.
 
SQL> conn john/smith
Connected.
SQL> create materialized view mv1
  2  refresh on demand
  3  as select * from andrew.tab1
  4  /
 
Materialized view created.
 
SQL> select * from mv1
  2  /
 
      COL1
----------
         1
 
SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   VALID
 
SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            VALID
 
SQL>
 
Andrew updates his table:

SQL> conn andrew/reid
Connected.
SQL> update tab1 set col1 = 2
  2  /
 
1 row updated.
 
SQL>

This makes John’s materialized view INVALID and gives it a COMPILE_STATE of NEEDS_COMPILE. I showed you this in an earlier example:
 
SQL> conn john/smith
Connected.
SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   INVALID
 
SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            NEEDS_COMPILE
 
SQL> select * from mv1
  2  /
 
      COL1
----------
         1
 
SQL>

Johns spots this and tries to compile his materialized view but this time, it remains INVALID:

SQL> alter materialized view mv1 compile
  2  /
 
Materialized view altered.
 
SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   INVALID
 
SQL>

… and it now has a COMPILE_STATE of COMPILATION_ERROR:
 
SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            COMPILATION_ERROR
 
SQL> select * from mv1
  2  /
 
      COL1
----------
         1
 
SQL>

Even refreshing the materialized view does not make the problem go away:
 
SQL> exec dbms_mview.refresh('MV1');
 
PL/SQL procedure successfully completed.
 
SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   INVALID
 
SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            COMPILATION_ERROR
 
SQL>

Although it does bring the data up to date:

SQL> select * from mv1
  2  /
 
      COL1
----------
         2
 
SQL>

Andrew grants SELECT access directly to John:
 
SQL> conn andrew/reid
Connected.
SQL> grant select on tab1 to john
  2  /
 
Grant succeeded.
 
SQL>

… and when John tries to compile the materialized view again, the problem disappears:
 
SQL> conn john/smith
Connected.
SQL> alter materialized view mv1 compile
  2  /
 
Materialized view altered.
 
SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   VALID
 
SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            VALID
 
SQL> select * from mv1
  2  /
 
      COL1
----------
         2
 
SQL>

Friday, March 14, 2014

INVALID Materialized View NEEDS_COMPILE

This example was tested on Oracle 11.2. It shows how an on demand materialized view becomes invalid following DML on the underlying table. First I created a table:

SQL> create table tab1 (col1 number)
  2  /
 
Table created.
 
SQL>

Then I inserted the value 1 into it: 

SQL> insert into tab1 values (1)
  2  /
 
1 row created.
 
SQL>

I created an on demand materialized view on the table: 

SQL> create materialized view mv1
  2  refresh on demand
  3  as select * from tab1
  4  /
 
Materialized view created.
 
SQL>

I checked that it was VALID:

SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   VALID
 
SQL>

I updated the table, making the materialized view out of date: 

SQL> update tab1 set col1 = 2
  2  /
 
1 row updated.
 
SQL> commit
  2  /
 
Commit complete.
 
SQL> select * from tab1
  2  /
 
      COL1
----------
         2
 
SQL> select * from mv1
  2  /
 
      COL1
----------
         1
 
SQL>

Although you can still query the materialized view when this happens, it shows as INVALID:

SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   INVALID
 
SQL>

... and in need of compilation:

SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            NEEDS_COMPILE
 
SQL>

This isn't a problem but, if you are not happy with it, you can refresh the materialized view:

SQL> exec dbms_mview.refresh('MV1');
 
PL/SQL procedure successfully completed.
 
SQL>

This brings it in line with the underlying table:

SQL> select * from mv1
  2  /
 
      COL1
----------
         2
 
SQL>

It is then shown as VALID again:

SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   VALID
 
SQL>

... and not in need of compilation:

SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            VALID
 
SQL>

I updated the table, making the materialized view out of date again: 

SQL> update tab1 set col1 = 3
  2  /
 
1 row updated.
 
SQL> commit
  2  /
 
Commit complete.
 
SQL> select * from tab1
  2  /
 
      COL1
----------
         3
 
SQL> select * from mv1
  2  /
 
      COL1
----------
         2

SQL>

It was also INVALID and in need of compilation as before:
 
SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   INVALID
 
SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            NEEDS_COMPILE
 
SQL>

Another way to change this, if it bothers you, is to compile it:

SQL> alter materialized view mv1 compile
  2  /
 
Materialized view altered.
 
SQL>

This way, the materialized view is still out of date: 

SQL> select * from mv1
  2  /
 
      COL1
----------
         2
 
SQL>

... but it is no longer INVALID: 

SQL> select object_type, status
  2  from user_objects
  3  where object_name = 'MV1'
  4  /
 
OBJECT_TYPE         STATUS
------------------- -------
TABLE               VALID
MATERIALIZED VIEW   VALID
 
SQL>

... and it does not need compiling either:

SQL> select mview_name, compile_state
  2  from user_mviews
  3  /
 
MVIEW_NAME                     COMPILE_STATE
------------------------------ -------------------
MV1                            VALID
 
SQL>

Thursday, January 31, 2013

Why is my Package or Procedure Invalid?

I had a problem earlier this week with a package which kept going INVALID. Once I had worked out the reason, I decided to do a worked example of it on Oracle 11.2.0.2.7. I created users called SOMEBODY, JLS and CBS. JLS has nothing to do with the pop group of the same name and CBS has nothing to do with the news network. You will notice that they have some unusual permissions. This is to save me having to connect as SYS later in the example. It has nothing to do with the problem:

conn / as sysdba
Connected.
SQL> create user somebody
  2  identified by somebody
  3  default tablespace users
  4  quota unlimited on users
  5  /

User created.
 
SQL> grant create session, create table to somebody
  2  /
 
Grant succeeded.
 
SQL> create user jls
  2  identified by jls
  3  default tablespace users
  4  quota unlimited on users
  5  /
 
User created.
 
SQL> grant
  2  create session,
  3  create procedure,
  4  update any table,
  5  select any table,
  6  select any dictionary to jls
  7  /
 
Grant succeeded.
 
SQL> create user cbs
  2  identified by cbs
  3  default tablespace users
  4  quota unlimited on users
  5  /
 
User created.
 
SQL> grant
  2  create session,
  3  create table,
  4  select any dictionary,
  5  create public synonym to cbs
  6  /
 
Grant succeeded.

SQL>

SOMEBODY created a table called SITE:
 
SQL> conn somebody/somebody
Connected.
SQL> create table site (col1 varchar2(15))
  2  /
 
Table created.

SQL>

JLS created a procedure to update SOMEBODY's SITE table using its fully qualified name:
 
SQL> conn jls/jls
Connected.
SQL> create or replace procedure update_site as
  2  begin
  3  update somebody.site set col1 = '10 High Street';
  4  end;
  5  /
 
Procedure created.

SQL>

DBA_DEPENDENCIES showed that JLS's UPDATE_SITE procedure referenced SOMEBODY's SITE table:
 
SQL> select referenced_owner, referenced_name
  2  from dba_dependencies
  3  where owner = 'JLS'
  4  and name = 'UPDATE_SITE'
  5  /
 
REFERENCED_OWNER     REFERENCED_NAME
-------------------- ------------------------------
SYS                  SYS_STUB_FOR_PURITY_ANALYSIS
SOMEBODY             SITE

SQL>

CBS created its own SITE table but this did not affect the validity of JLS's UPDATE_SITE procedure:
SQL> conn cbs/cbs
Connected.
SQL> create table site (col2 number)
  2  /
 
Table created.
 
SQL> select owner, object_name, object_type
  2  from dba_objects
  3  where status = 'INVALID'
  4  /
 
no rows selected
 
SQL> drop table site
  2  /
 
Table dropped.

SQL>

A public synonym called SITE was created for SOMEBODY's SITE table:
 
SQL> create or replace public synonym site for somebody.site
  2  /
 
Synonym created.

SQL>

CBS recreated its SITE table. JLS's UPDATE_SITE procedure remained VALID:
 
SQL> create table site (col2 number)
  2  /
 
Table created.
 
SQL> select owner, object_name, object_type
  2  from dba_objects
  3  where status = 'INVALID'
  4  /
 
no rows selected
 
SQL> drop table site
  2  /
 
Table dropped.

SQL>

JLS changed its UPDATE_SITE procedure to access SOMEBODY's SITE table via the public synonym:
 
SQL> conn jls/jls
Connected.
SQL> create or replace procedure update_site as
  2  begin
  3  update site set col1 = '10 High Street';
  4  end;
  5  /
 
Procedure created.

SQL>

DBA_DEPENDENCIES showed that JLS's UPDATE_SITE procedure referenced a public synonym called SITE:
 
SQL> select referenced_owner, referenced_name
  2  from dba_dependencies
  3  where owner = 'JLS'
  4  and name = 'UPDATE_SITE'
  5  /
 
REFERENCED_OWNER     REFERENCED_NAME
-------------------- ------------------------------
SYS                  SYS_STUB_FOR_PURITY_ANALYSIS
PUBLIC               SITE

SQL>

CBS checked that there were no INVALID objects in the database and recreated its SITE table. This made JLS's UPDATE_SITE procedure INVALID.
    
SQL> conn cbs/cbs
Connected.
SQL> select owner, object_name, object_type
  2  from dba_objects
  3  where status = 'INVALID'
  4  /
 
no rows selected
 
SQL> create table site (col2 number)
  2  /
 
Table created.
 
SQL> select owner, object_name, object_type
  2  from dba_objects
  3  where status = 'INVALID'
  4  /
 
OWNER      OBJECT_NAME     OBJECT_TYPE
---------- --------------- ---------------
JLS        UPDATE_SITE     PROCEDURE

SQL>

The procedure then had to be recompiled: 

SQL> conn jls/jls
Connected.
SQL> alter procedure update_site compile
  2  /
 
Procedure altered.
 
SQL>

So, there you have it. If a compiled procedure or package accesses a table via a public synonym, then another user creates a table with the same name as the public synonym, the compiled procedure or package will be invalidated.

Incidentally, when this happened for real, a package with over 3000 lines of code became INVALID. When a UNIX script then tried to run it, Oracle did not attempt to recompile the package automatically, it just failed with a tnsnames related error, possibly because the package accessed data over a database link. It was later recompiled without errors and the UNIX script ran successfully.

I have already looked at a very simple example relating to automatic procedure recompilation. I will be looking at it in more detail in the near future as it is clearly not as straightforward as I thought at first.