已索引
该文章已被其它文章引用。

同事反馈大数据平台和EDI系统在抽取数字金融平台数据库数据时报错了,工具界面上反应的现象是有几个视图是感叹号,代码上报的是 ORA-04045 和 ORA-16000。

$ oerr ora 04045
04045, 00000, "errors during recompilation/revalidation of %s.%s"
// *Cause:  This message indicates the object to which the following
//          errors apply.  The errors occurred during implicit
//          recompilation/revalidation of the object.
// *Action: Check the following errors for more information, and
//          make the necessary corrections to the object.
[oracle@QPDB0011 ~]$ oerr ora 16000
16000, 00000, "database open for read-only access"
// *Cause:  The database was opened for read-only access.  Attempts to
//          modify the database using DML or DDL statements generate this
//          error.
// *Action: In order to modify the database, it must first be shut down and
//          reopened for read/write access.

简单说来,就是在访问备库的视图时,Oracle发现视图失效了尝试自动编译,但备库只读,所以报错。

为什么视图失效?大概率是底层的表定义发生了变化(发生了DDL)。

通过 dba_source 找出视图的定义语句,找出基表,然后通过 DBA_OBJECTS.LAST_DDL_TIME 确认最后的 DDL 时间,发现其中有两张表发生了 DDL。与应用运维人员确认,的确是修改了这两表的字段类型,修改后未对视图进行编译。

总结:
当视图后端的表发生 DDL 后,视图会变为 INVALID,当通过主库访问视图时,Oracle 会自动编译。
如果需要手动编译,可以在工具上右击视图,选择编译,或者执行如下命令:
SQL> alter view VW_TEST1 compile;

Oracle 文档(Doc ID 1940956.1)对此也有解释。

-- By 许望(RHCA、OCM、VCP)
最后修改:2025 年 01 月 15 日 05 : 25 PM
如果觉得我的文章对你有用,请随意赞赏