ORACLE 迁移到 MogDB 之存储过程改造:系统视图
在 ORACLE 迁移到 MogDB 的过程中,经常遇到一些 ORACLE 系统视图的改造工作。记录如下:
MogDB 官方出了一个兼容性系统视图的插件
https://gitee.com/enmotech/compat-tools/tree/master
compat-tools
介绍
本项目是一个兼容工具集合,旨在为从其他异构数据库,迁移到 openGauss 之后的系统,创建必要的函数,以及系统视图的兼容。为后续的系统运维与应用改造提供便利。
脚本中带有版本控制,直接运行脚本时,会根据如下三种情况进行处理:
若待创建对象在目标数据库中不存在,则直接进行创建
若待创建对象版本高于目标数据库中的对象版本,则进行升级重建
若待创建对象版本不高于目标数据库中的对象版本,则跳过创建
目前看了下,代码较多、复杂,比如在改造过程中遇到的分区表查询的视图 user_tab_partitions
,通过 giteee 来查看 ALL_TAB_PARTITIONS
发现代码较长,不光查询系统视图还用到了自定义的函数。
1 | -- ========================================================================= |
1 | select count(*) from user_tab_partitions where partition_name = 'xxx_partition' |
其实在日常改造过程当中,虽然 pg 是以扩展来号称期方便的特性,其实没必要,国产化改造过程中,要尽可能的简单,满足业务需求,对于上面遇到的,虽然是查询 user_tab_partitions
视图,但是他主要是查询分区表是否存在,MogDB的 pg_partition
就可以满足要求。
1 | select count(*) from pg_partition p where parttype=‘p’ and p.relname = 'xxx_partition‘ |
还有 查询 USER_ALL_TABLES
,来判断表是否存在的。
1 | SELECT COUNT(*) into result FROM USER_ALL_TABLES |
可以通过以下来改造:
1 | SELECT COUNT(*) into result FROM information_schema.tables |
当然如果要通过插件的方式来解决,MogDB可以通过以下三个兼容性的插件来实现,等以后体验安装。
- whale(extension)
https://docs.mogdb.io/zh/mogdb/v3.0/whale - **orafce(extension)
https://docs.mogdb.io/zh/mogdb/v3.0/orafce - compat-tools (sql_script)
https://gitee.com/enmotech/compat-tools
更新:20240603
user_tab_columns
1 | select column_name from user_tab_columns where table_name='''||tab_name||''' and column_id='||col_id; |
改造后:
1 | select column_name from information_schema.columns where table_name'''||tab_name||''' and ordinal_position='||col_id;' |
pg/MogDB 中使用 information_schema.columns
替换 user_tab_columns
,ordinal_position
替换 column_id
这个也可以加上 table_schema
的限制,只限制在当前 schema下面,table_schema=current_schema
.
同样 user_tables 用 informat_schema.tables 改造。
1 | select count(1) from user_tables where table_name=xxx; |
改造如下:
1 | select count(1) from information_schema.tables where table_name='xxx' and table_schema=current_schema; |
原文作者: Hi.MogDB
原文链接: https://hi.mogdb.org/posts/83bfca47/
许可协议: 知识共享署名-非商业性使用 4.0 国际许可协议