我们在编译invalid的同义词(synonym)的时候,可能在某些版本会遇到一些特殊权限问题,具体来说是遇到ora-01031错误。下面构造这样一个例子:
如下所示,scott用户下面创建了一个公共同义词emp(下面测试环境为oracle 10.2.0.5)
sql> show user;
user is "scott"
sql> create public synonym emp for scott.emp;
synonym created.
sql>
假设我们需要重新编译同义词,在system用户下去编译同义词时,就会遇到ora-01031
sql> show user;
user is "system"
sql> alter public synonym emp compile;
alter public synonym emp compile
*
error at line 1:
ora-01031: insufficient privileges
另外一种情况也会遇到这个错误,usera 去编译userb下的同义词的话,也会遇到这个错误。按照我的理解,如果用户拥有create any synonym和drop any synonym权限的话,应该是可以编译其它schema下的同义词。而且system用户被授予了dba角色,dba角色拥有create any synonym 或drop any synonym的权限。
sql> select * from dba_sys_privs where privilege in('create any synonym','drop any synonym') and grantee='dba';
grantee privilege adm
------------------------------ ---------------------------------------- ---
dba drop any synonym yes
dba create any synonym yes
sql> select * from dba_role_privs where grantee='system';
grantee granted_role adm def
------------------------------ ------------------------------ --- ---
system aq_administrator_role yes yes
system dba yes yes
system mgmt_user no yes
但是metalink上的文档doc id 1299001.1提示:同义词的编译时,要么是sys用户或者同义词的owner才能编译,也就是说,你要编译同义词emp,必须是scott用户或sys用户。
sql> show user
user is "sys"
sql> alter public synonym emp compile;
synonym altered
所以在oracle 10g下要编译其它schema下的同义词,必须是sys用户。ora-01031 when compiling a synonym (doc id 1299001.1)的介绍如下:
cause
this is the expected behaviour. the command “alter synonym <name> compile” is not documented and as such it is not supported. although it works in some circumstances it is not meant to be used by the users directly.
solution
this is the expected behaviour. if all the synonyms have to compiled then run the recompilation script as sys.
文档ora-01031 when executing alter synonym compile for other schema (doc id 1435898.1)中也有相关简单阐述
在oracle 11.2.0.4.0中测试也是如此。但是后续官方文档中将这个现象视为一个bug,官方文档4189542 – only sys can “alter synonym… compile” for another users’s synonym (doc id 4189542.8)中有简单介绍,并且在后面的一些版本中修复了这个bug,doc id 4189542.8的具体信息如下如下:
only sys can “alter synonym… compile” for another users’s synonym.
other users get ora-1031 attempting to compile another users synonym.
with this fix a user with both create any synonym and drop any synonym
privilege is allowed to issue alter synonym .. compile for another users synonym.
相同的现象在不同文档中有不同阐述,确实有点让人迷惑,如果获取的信息不全或比较片面的话,可能对这个问题和现象有不同的理解。
参考资料:
ora-01031 when compiling a synonym (doc id 1299001.1)
ora-01031 when executing alter synonym compile for other schema (doc id 1435898.1)
bug 4189542 – only sys can “alter synonym… compile” for another users’s synonym (doc id 4189542.8)