1

案例诊断:oracle anydata 数据类型存储的自定义类型丢失后ora-利来国际app

anbob 2021-11-09
1430

oracle anydata数据类型几年前在做expdp迁移时接触过,类型xml,lob导出速度是慢之非常, 今天又一个客户在expdp时一个interval partition表时,部分分区导出报错ora-21700: object does not exist or is marked for delete错误,依赖的对象不存在,查看表定义存在anydata列,也就是可能anydata这种任何数据类型都可以存储(但还是建议用lob类型代替),但是存储的自定义类型又不存在了,这种情况很危险,下面演示一下这种情况。

```

create or replace type anbob.t_stu as object (
  stu_num varchar2(10),
  stu_name varchar2(10)
);
/
create table anbob.test_anydata(id int,msg anydata);
insert into anbob.test_anydata (id, msg) values (1, sys.anydata.convertvarchar2('this is varchar2'));
insert into anbob.test_anydata (id, msg) values (2, sys.anydata.convertnumber(999));
insert into anbob.test_anydata (id, msg) values (3, sys.anydata.convertnumber(1));
insert into anbob.test_anydata (id, msg) values (4, sys.anydata.convertdate(sysdate));
commit;
declare
  l_obj     anbob.t_stu := anbob.t_stu('1','anbob');
  l_anydata sys.anydata;
begin
  -- convert object to anydata and back.
  l_anydata := sys.anydata.convertobject(l_obj);
  insert into anbob.test_anydata (id, msg) values (5, l_anydata);
  if l_anydata.getobject(l_obj) = dbms_types.success
  then
    dbms_output.put_line('t_my_type : ' || l_obj.stu_num || ' : ' || l_obj.stu_name);
  end if;
end;
 15  /
t_my_type : 1 : anbob
commit;
sql> select * from anbob.test_anydata;
        id msg()
---------- ------------------------------
         1 anydata()
         2 anydata()
         3 anydata()
         4 anydata()
         5 anydata()
sql> @dep  anbob test_anydata % %
owner            dependent_name                 dependent_ty ref_owner        ref_name                       ref_type     dep_
---------------- ------------------------------ ------------ ---------------- ------------------------------ ------------ ----
anbob            test_anydata                   table        sys              standard                       package      hard
anbob            test_anydata                   table        sys              anydata                        type         hard
sql>  select   sys.anydata.gettypename(msg) type_name,count(*) from anbob.test_anydata group by sys.anydata.gettypename(msg)
type_name                        count(*)
------------------------------ ----------
sys.number                              2
anbob.t_stu                             1
sys.varchar2                            1
sys.date                                1


```

tip:
anydata.gettypename 可以取到anydata数据类型的实际datatype, 但是在查看依赖对象是并不会提示表依赖我们开始创建的自定义对象, 这种情况如果删除了自定义对象就糟糕了,下面继续。

sql> drop type anbob.t_stu ;
type dropped.
sql> select   sys.anydata.gettypename(msg) type_name,count(*) from anbob.test_anydata group by sys.anydata.gettypename(msg);
select   sys.anydata.gettypename(msg) type_name,count(*) from anbob.test_anydata group by sys.anydata.gettypename(msg)
                                                                                          *
error at line 1:
ora-21700: object does not exist or is marked for delete
ora-06512: at "sys.anydata", line 174
sql> select * from anbob.test_anydata;
error:
ora-21700: object does not exist or is marked for delete
no rows selected
[[email protected] ~]$ expdp system/[email protected] directory=datapump dumpfile=anydata.dump tables=anbob.test_anydata
export: release 19.0.0.0.0 - production on sat nov 6 19:58:07 2021
version 19.3.0.0.0
利来娱乐 copyright (c) 1982, 2019, oracle and/or its affiliates.  all rights reserved.
connected to: oracle database 19c enterprise edition release 19.0.0.0.0 - production
starting "system"."sys_export_table_01":  system/********@cdb1pdb1 directory=datapump dumpfile=anydata.dump tables=anbob.test_anydata
processing object type table_export/table/table_data
processing object type table_export/table/index/statistics/index_statistics
processing object type table_export/table/statistics/table_statistics
processing object type table_export/table/statistics/marker
processing object type table_export/table/table
ora-31693: table data object "anbob"."test_anydata" failed to load/unload and is being skipped due to error:
ora-29913: error in executing odciexttablepopulate callout
ora-21700: object does not exist or is marked for delete

tip:
读取到表里anydata包含已删除的自定义对象的行时,anydata无法转换才有了这个错误,同时影响expdp.

sql>  declare
  2    l_anydata sys.anydata;
  3    my_code varchar2(100);
  4    l_typename varchar2(100);
  5  begin
  6     for i in (select * from anbob.test_anydata) loop
  7      begin
  8   l_typename:=sys.anydata.gettypename(i.msg);
  9   exception
 10             when others then
 11         begin
 12           my_code := sqlcode;
 13              --dbms_output.put_line(my_code);
 14       if my_code in ('-21700')  then
 15        dbms_output.put_line('miss datatype id:'||i.id );
 16               end if;
 17        end;
 18  end;
 19     end loop;
 20* end;
miss datatype id:5
pl/sql procedure successfully completed.
sql> create or replace type anbob.t_stu as object (
  stu_num varchar2(10),
  stu_name varchar2(10)
);
 /
type created.
sql> select * from anbob.test_anydata;
error:
ora-21700: object does not exist or is marked for delete
sql> insert into anbob.test_anydata (id, msg) values (6, sys.anydata.convertobject(anbob.t_stu('2','anbob.com')));
1 row created.
sql> select * from anbob.test_anydata where id=5;
error:
ora-21700: object does not exist or is marked for delete
no rows selected
sql> select * from anbob.test_anydata where id=6;
        id msg()
---------- ----------------------------------------
         6 anydata()

note:重建对象后,对象重建之前的数据依旧无法读取,新录的数据可以读,看来是有先后时间依赖。

解决方法,暂时发现是delete或update对象的列, 再手动补,也就是数据丢失了,如果有更好的方法,请联系我。

sql> delete anbob.test_anydata where id=5;
1 row deleted.
sql> commit;
commit complete.
sql> col msg for a30
sql> select * from anbob.test_anydata;
        id msg()
---------- ------------------------------
         1 anydata()
         2 anydata()
         3 anydata()
         4 anydata()
sql> declare
  l_obj     anbob.t_stu := anbob.t_stu('1','anbob');
  l_anydata sys.anydata;
begin
  -- convert object to anydata and back.
  l_anydata := sys.anydata.convertobject(l_obj);
  insert into anbob.test_anydata (id, msg) values (5, l_anydata);
  if l_anydata.getobject(l_obj) = dbms_types.success
  then
    dbms_output.put_line('t_my_type : ' || l_obj.stu_num || ' : ' || l_obj.stu_name);
  end if;
end;
 15  /
pl/sql procedure successfully completed.
sql> commit;
commit complete.
sql> select * from anbob.test_anydata;
        id msg()
---------- ------------------------------
         1 anydata()
         2 anydata()
         3 anydata()
         4 anydata()
         5 anydata()

请注意,anydata 列中的依赖关系不是元数据级别的数据,因此不会创建从 anydata 到类型的依赖关系, 如果创建的表列是自定义数据类型,那会有强依赖关系,在删除自定义datatype时会检查是否有表定义使用了该类型。在 oracle 中,依赖项总是在元数据级别而不是数据级别进行跟踪,anydata 就是这种情况。这是 anydata 的预期行为。

sqlcl
注意使用sqlcl工具显示更加直观

[[email protected] bin]$ ./sql
sqlcl: release 21.2 production on sun nov 07 10:52:28 2021
利来娱乐 copyright (c) 1982, 2021, oracle.  all rights reserved.
username? (''?) system
password? (**********?) ******
last successful login time: sun nov 07 2021 10:52:36  08:00
connected to:
oracle database 19c enterprise edition release 19.0.0.0.0 - production
version 19.3.0.0.0
sql> show pdbs
    con_id con_name                       open mode  restricted
---------- ------------------------------ ---------- ----------
         2 pdb$seed                       read only  no
         3 pdb1                           read write no
sql> alter session set container=pdb1;
session altered.
sql> select * from anbob.test_anydata where id=6;
   id                                                                                     msg
_____ _______________________________________________________________________________________
    6 opaque(anydata typecode: "typecode_jdbc_struct" - anydata value: "anbob.t_stu(...)")
sql> select * from anbob.test_anydata where id=5;
error starting at line : 1 in command -
select * from anbob.test_anydata where id=5
error report -
ora-21700: object does not exist or is marked for delete

查找自定义对象类型

sql> select id,dump(msg,16) dump_v from anbob.test_anydata where id=6;
   id                                                                                                                                                                                                                          dump_v
_____ _______________________________________________________________________________________________________________________________________________________________________________________________________________________________
    6 typ=58 len=83: 0,1,0,0,0,0,0,1,0,0,0,f1,47,cc,0,3d,48,90,0,37,0,0,33,1,85,1,33,1,1,2,4,0,6c,d0,2a,9d,88,e0,a5,7,b0,e0,55,36,b7,9b,c2,72,9a,0,1,0,0,0,0,13,84,1,fe,0,0,0,13,1,32,9,61,6e,62,6f,62,2e,63,6f,6d,0,0,0,0,0,0,0,0
sql> select id,dump(msg,16) dump_v from anbob.test_anydata where id=5;
   id                                                                                                                                                                                                           dump_v
_____ ________________________________________________________________________________________________________________________________________________________________________________________________________________
    5 typ=58 len=79: 0,1,0,0,0,0,0,1,0,0,0,f1,47,31,0,39,48,90,0,33,0,0,2f,1,85,1,2f,1,1,2,4,0,6c,d0,1f,4c,90,2,1a,d,ac,e0,55,36,b7,9b,c2,72,9a,0,1,0,0,0,0,f,84,1,fe,0,0,0,f,1,31,5,61,6e,62,6f,62,0,0,0,0,0,0,0,0

tip :
数据类型 58 是不透明的 (dtyopq),它可以保存 anydata、anytype、xmltype、anydataset(可能还有其他)。但是如何准确区分哪个,或者如何解释我从 dump() 得到的字节,我在某处找不到。但是我们可以仔细对象类型的oid.

sql> @printtab 'select * from dba_types where owner="anbob"';
owner                         : anbob
type_name                     : t_stu
type_oid                      : d02a9d88e0a507b0e05536b79bc2729a
typecode                      : object
attributes                    : 2
methods                       : 0
predefined                    : no
incomplete                    : no
final                         : yes
instantiable                  : yes
persistable                   : yes
supertype_owner               :
supertype_name                :
local_attributes              :
local_methods                 :
typeid                        :
-----------------
pl/sql procedure successfully completed.

tip:
注意 type的oid为 d02a9d88e0a507b0e05536b79bc2729a,和dump的数据类型对应部分匹配0,1,0,0,0,0,0,1,0,0,0,f1,47,cc,0,3d,48,90,0,37,0,0,33,1,85,1,33,1,1,2,4,0,6c,d0,2a,9d,88,e0,a5,7,b0,e0,55,36,b7,9b,c2,72,9a,0,1,0,0,0,0,13,84,1,fe,0,0,0,13,1,32,9,61,6e,62,6f,62,2e,63,6f,6d,0,0,0,0,0,0,0,0

那就可以从dba_type找报错的那行对象的type是不是丢了

id=5 那行的dump 找相同位置type oid为“d0,1f,4c,90,2,1a,d,ac,e0,55,36,b7,9b,c2,72,9a”
select toid,versions_endscn,versions_operation
 from sys.type$ versions between scn minvalue and maxvalue
 where ',d0,2a,9d,88,e0,a5,7,b0,e0,55,36,b7,9b,c2,72,9a,'
 like '%,'||regexp_replace(dump(type$.toid,16),'^.* ')||',%'
  5  ;
toid                             versions_endscn v
-------------------------------- --------------- -
d02a9d88e0a507b0e05536b79bc2729a                 i
select toid,versions_endscn,versions_operation
 from sys.type$ versions between scn minvalue and maxvalue
 where ',d0,1f,4c,90,2,1a,d,ac,e0,55,36,b7,9b,c2,72,9a,'
 like '%,'||regexp_replace(dump(type$.toid,16),'^.* ')||',%'
  5  ;
toid                             versions_endscn v
-------------------------------- --------------- -
d01f4c90021a0dace05536b79bc2729a                 d
d01f4c90021a0dace05536b79bc2729a        17989537
sql> col type_name for a30
sql> select owner,type_name   from dba_types  where rawtohex(type_oid)= 'd02a9d88e0a507b0e05536b79bc2729a';
owner                          type_name
------------------------------ ------------------------------
anbob                          t_stu
sql> r
  1  with function try(x anydata,d varchar2) return varchar2
  2  as
  3   l_toid varchar2(1000);                                                                                                                                                        l_scn number;
  4   l_name varchar2(1000);
  5   begin
  6   return anydata.gettypename(x);
  7   exception when others then
  8   select rawtohex(toid),versions_endscn into l_toid,l_scn
  9   from sys.type$ versions between scn minvalue and maxvalue
 10   where d like '%,'||regexp_replace(dump(type$.toid,16),'^.* ')||',%'
 11   order by versions_endscn fetch first 1 rows only;
 12   select owner||'.'||type_name into l_name
 13   from dba_types as of scn (l_scn -1)
 14   where rawtohex(type_oid)=l_toid;
 15   return sqlerrm||' -> '||l_name;
 16   end;
 17   select id,try(msg,dump(msg,16)) res  from anbob.test_anydata
 18*
        id res
---------- ----------------------------------------------------------------------------------------------------
         1 sys.varchar2
         2 sys.number
         3 sys.number
         4 sys.date
         5 ora-21700: object does not exist or is marked for delete -> anbob.t_stu
         6 anbob.t_stu

note:
我的测试案例中可以看出对象虽然重建但是oid变了,id=5的行记录对应的数据类型已被删除,从flashback query中可以得到(因为我的undo 还没有被覆盖掉)。 也可以对比对象的值

sql> select dump('2',16) from dual;
dump('2',16)
----------------
typ=96 len=1: 32
sql> select dump('anbob.com',16) from dual;
dump('anbob.com',16)
----------------------------------------
typ=96 len=9: 61,6e,62,6f,62,2e,63,6f,6d

对应用的是anydata dump中的0,1,0,0,0,0,13,84,1,fe,0,0,0,13,1,32,9,61,6e,62,6f,62,2e,63,6f,6d,0,0,0

— enjob —

「喜欢文章,快来给作者赞赏墨值吧」
1人已赞赏
【利来手机国际的版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:[email protected]进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论