-1

I am executing a DDL from inside a stored proc :

v_sql_stmt := 'ALTER INDEX PK_TEST REBUILD ONLINE';
EXECUTE IMMEDIATE (v_sql_stmt);

All objects - the index, the table (on which the index is built), and the proc (with the above 2 lines) belong to the same schema. Also, while executing the stored proc, I am logging into that same schema and executing the proc as the owner.

However, every time I run it, I am getting the error : ORA-01031: insufficient privileges for this statement

I have had the DBA grant CREATE ANY TABLE and CREATE ANY INDEX privileges explicitly to the user, because the PL/SQL may not be able to resolve the grants given via roles.

But am still getting the error in this line after the grants. The other parts of the proc is executing OK, as I can see from dbms_output tracing at various points.

Can anyone please help with ideas ?

Jay
  • 47
  • 11
  • Try without brackets: `EXECUTE IMMEDIATE v_sql_stmt;` – Wernfried Domscheit May 27 '20 at 14:56
  • Is index `PK_TEST` in your own schema? – Wernfried Domscheit May 27 '20 at 14:57
  • @WernfriedDomscheit yes all objects are in my schema. I found out that removing the ONLINE clause resolves the issue. The EXECUTE IMMEDIATE runs perfectly without error for 'ALTER INDEX PK_TEST REBUILD' but throws error for 'ALTER INDEX PK_TEST REBUILD ONLINE'. So this "ONLINE" clause is the issue. I dont know why. – Jay May 27 '20 at 15:57
  • Does the same thing happen without the procedure i.e. if you execute the same thing on the command line? (I don't have an answer, just curious.) – William Robertson May 29 '20 at 15:51
  • @WilliamRobertson no error when executed from the command line. The sql executes fine and the index is rebuilt. – Jay May 31 '20 at 05:14

3 Answers3

1

You must grant ALTER ANY INDEX or ALTER INDEX on the specific index.

But if the procedure is created by default by the owner of the index and the procedure is also run by the same user there is no need to grant any additional privilege.

Here a small test case with Oracle 12.1 EE:

SQL> --
SQL> select banner from v$version where rownum=1;

BANNER
--------------------------------------------------------------------------------
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production

SQL> create user myuser identified by "myuser" quota unlimited on users;

User created.

SQL> grant create session, create table, create procedure to myuser;

Grant succeeded.

SQL> connect myuser/myuser
Connected.

Session altered.

SQL> show user
USER is "MYUSER"
SQL> create table mytable as select * from all_objects where object_id < 1000;

Table created.

SQL> create index myindex on mytable(object_name);

Index created.

SQL> create procedure myproc
  2  as
  3  begin
  4   execute immediate 'alter index myindex rebuild online';
  5  end;
  6  /

Procedure created.

SQL> show errors
No errors.
SQL> exec myproc;

PL/SQL procedure successfully completed.

SQL> show errors
No errors.
SQL> 
pifor
  • 7,419
  • 2
  • 8
  • 16
  • Sorry I missed saying that. I have had the DBA grant ALTER ANY INDEX as well. Did not work :( – Jay May 27 '20 at 14:07
  • I've updated my answer. What is the Oracle 4 digits version ? Is this Enterprise Edition/ Standard Edition ? Do yo use any special security feature like Data Vault ? – pifor May 27 '20 at 14:15
  • SELECT BANNER FROM V$VERSION; Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production PL/SQL Release 12.1.0.2.0 - Production "CORE 12.1.0.2.0 Production" TNS for Linux: Version 12.1.0.2.0 - Production NLSRTL Version 12.1.0.2.0 - Production – Jay May 27 '20 at 14:39
  • Your sample procedure failed at my end. But it worked, when I removed the ONLINE clause. Dont understand why !!! But happy its working :( – Jay May 27 '20 at 15:25
  • If all what you say is true, your database is broken. If you care don't waste your time and contact Oracle support. – pifor May 27 '20 at 15:26
0

The only conclusion I can see is that at least one of the facts you've stated is not actually true. Let me rephrase the stated facts:

A) The index, table, and procedure are all in the same schema

B) You are connecting to that schema to execute the procedure

If A and B are both true, you should not need any specific granted privileges to execute the rebuild command

C) The DBA has granted CREATE ANY TABLE, CREATE ANY INDEX, and ALTER ANY INDEX (per comment on other answer) to this user

These privileges should only be relevant if A and B are not both true. If they're not both true, and at least two schemas are involved, which one did the DBA grant the privs to

D) It is the ALTER INDEX command that is throwing the error

If A, B, and C are true, then I have to suspect that D is not

To eliminate possibilities, it would be good to show hard evidence of these facts -- i.e. query results that demonstrate they are true.

I'd also try executing the ALTER as a standalone command, and in an anonymous block. This will at least clarify whether executing it via procedure is part of the problem.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
  • A,B,C are true. For D, if I run that same ALTER INDEX sql from sql plus, it executes without any error. It is from within the stored proc that I getting the error. – Jay May 27 '20 at 14:31
  • 1
    In this case you have find a bug in Oracle and you can only contact Oracle support. – pifor May 27 '20 at 14:32
  • Here is my privileges granted to the user : select privilege from user_SYS_PRIVS; CREATE PROCEDURE UNLIMITED TABLESPACE CREATE VIEW CREATE ANY INDEX CREATE MATERIALIZED VIEW CREATE SYNONYM ALTER ANY INDEX FLASHBACK ARCHIVE ADMINISTER – Jay May 27 '20 at 14:35
  • I've added a test case in my answer that shows you only need CREATE SESSION, CREATE TABLE, CREATE PROCEDURE and some quota on related tablespace to rebuild your own indexes with a stored procedure for Oracle 12.1.0.2 EE. – pifor May 27 '20 at 14:52
-1

When I removed the ONLINE clause from the ALTER INDEX statement, it is working without error. Dont understand why !!! But happy its working :(

Jay
  • 47
  • 11
  • If all what you say is true, your database is broken. If you care don't waste your time and contact Oracle support. – pifor May 27 '20 at 15:30
  • 1
    Why won't what I am saying be true !!! I gave the privilege list, banner etc. Am trying to find on the internet if the "ONLINE" clause has any specific issue with it regarding running from inside PL/SQL. Without the ONLINE clause, the index rebuild is working. With ONLINE clause it is not. – Jay May 27 '20 at 15:52
  • Because I gave a proof i.e. a test case that anyone can reproduce on a 12.1.0.2 EE database. If this does not work, you have a very special configuration and you did not tell us or something does not work as expected. As long as no one can reproduce your test case I cannot trust what you say. – pifor May 27 '20 at 15:56
  • @pifor Peace !!! You dont have to trust or distrust me. But I appreciate your help and time. My curiosity is now what is the issue with having ONLINE clause !!! – Jay May 27 '20 at 15:59
  • Online index rebuild is an Entreprise Edition (EE ) feature and it must be work in EE. And if you would have Standard Edition, the error is ORA-00439 and not ORA-1031. I insist: it's the job of your DBA to investigate, if your DBA does not know, he/she must escalate to Oracle support. – pifor May 27 '20 at 16:07