openGauss MogDB 迁移适配——存储过程重载

简介: openGauss MogDB 迁移适配——存储过程重载

1. 异构迁移

在给某金融级客户迁移时遇到了各种各样的兼容性问题,本次迁移涉及到多个存储过程的适配,DB2到MogDB,这里做一个简单的整理。

2. mtk迁移

mtk全称为 The Database Migration Toolkit,是一个云和恩墨自主研发的可以将Oracle/DB2/MySQL/openGauss数据库的数据结构,全量数据高速导入到MogDB的工具。

逻辑对象统计如下

b7b24df889c6d41b30cde56ed3801329_20220111-995ebb13-e718-432e-b15d-8f0bd7cf6c47.png

MTK 截图部分展示

98c2b755020bccfc17c4ecc562344ffc_20220111-17b2f4e2-906c-4567-8ff1-4b738366e0b8.png

其中两次迁移对比发现存储过程少了7个,去源端查询发现有7个完全同名的存储过程,即存在存储过程重载现象。

3. 函数重载

多个函数具有相同的名称,只要参数不同即可。如果多个函数具有相同的名称,那么我们说这些函数是重载的。当一个函数被调用时,根据输入参数调用确切的函数,存储过程也是一样的原理。

MogDB 存储过程支持重载功能

feff546c8c6fdbed28c4297bea851448_20220111-35a0fc57-0dce-49aa-a35e-b354c8ab66a5.png

4. 存储过程重载操作

  • 创建
postgres=> create or replace procedure procedure1(p1 text, out p2 text)
postgres-> package 
postgres-> as Declare
postgres$> BEGIN
postgres$> p2 = concat('procedure parameter: p1,a');
postgres$> end;
postgres$> /
CREATE PROCEDURE
postgres=> create or replace procedure procedure1(p1 integer, out p2 text)
postgres-> package 
postgres-> as Declare
postgres$> BEGIN
postgres$> p2 = concat('procedure parameter: p1,a');
postgres$> end;
postgres$> /
CREATE PROCEDURE
postgres=> \df procedure1
                                               List of functions
 Schema |    Name    | Result data type |   Argument data types   |  Type  | fencedmode | propackage | prokind 
--------+------------+------------------+-------------------------+--------+------------+------------+---------
 lee    | procedure1 | text             | p1 integer, OUT p2 text | normal | f          | t          | p
 lee    | procedure1 | text             | p1 text, OUT p2 text    | normal | f          | t          | p
(2 rows)
postgres=>
  • 删除
postgres=> drop procedure procedure1;
ERROR:  function procedure1 asks parameters
postgres=> drop procedure procedure1(text);   
DROP PROCEDURE
postgres=> \df procedure1
                                               List of functions
 Schema |    Name    | Result data type |   Argument data types   |  Type  | fencedmode | propackage | prokind 
--------+------------+------------------+-------------------------+--------+------------+------------+---------
 lee    | procedure1 | text             | p1 integer, OUT p2 text | normal | f          | t          | p
(1 row)
postgres=> drop procedure procedure1(integer);  
DROP PROCEDURE
postgres=> \df procedure1
                                         List of functions
 Schema | Name | Result data type | Argument data types | Type | fencedmode | propackage | prokind 
--------+------+------------------+---------------------+------+------------+------------+---------
(0 rows)
  • 首次创建时不带package属性,再使用带package属性去重载时会创建失败
postgres=> create or replace procedure procedure1(p1 text, out p2 text) 
postgres-> as Declare
postgres$> BEGIN
postgres$> p2 = concat('procedure parameter: p1,a');
postgres$> end;
postgres$> /
CREATE PROCEDURE
postgres=> create or replace procedure procedure1(p1 text, out p2 text)
postgres-> package 
postgres-> as Declare
postgres$> BEGIN
postgres$> p2 = concat('procedure parameter: p1,a');
postgres$> end;
postgres$> /
ERROR:  Do not allow package function replace not package function.
  • 当有同名函数存在时创建报错
postgres=> CREATE OR REPLACE FUNCTION procedure1(p_timestamp timestamp with time zone)
postgres->  RETURNS integer
postgres->  LANGUAGE sql
postgres->  NOT FENCED NOT SHIPPABLE
postgres-> AS $function$
postgres$>     select (extract('day' from (p_timestamp - '0001-01-01bc'))-365)::integer;
postgres$> $function$;
CREATE FUNCTION
postgres=> 
postgres=> create or replace procedure procedure1(p1 text, out p2 text)
postgres-> package 
postgres-> as Declare
postgres$> BEGIN
postgres$> p2 = concat('procedure parameter: p1,a');
postgres$> end;
postgres$> /
ERROR:  Do not allow package function replace not package function.

5. 存储过程修改为函数

postgres=> create or replace procedure procedure1(p1 text, out p2 text)
postgres-> package 
postgres-> as Declare
postgres$> BEGIN
postgres$> p2 = concat('procedure parameter: p1,a');
postgres$> end;
postgres$> /
CREATE PROCEDURE
postgres=> drop procedure procedure1 (text);
DROP PROCEDURE
postgres=> create or replace function procedure1(p1 text, out p2 text)
postgres-> returns void
postgres-> as $$
postgres$> BEGIN
postgres$> p2 = concat('procedure parameter: p1,a');
postgres$> end;
postgres$> $$
postgres-> language plpgsql;
CREATE FUNCTION

6. 总结

  • MogDB、openGauss支持存储过程的重载。
  • 删除重载属性的存储过程需要传入相应参数。
  • 创建重载属性的存储过程需要在as前加上package关键字。
  • 当同schema下有同名的函数或者不带package属性的存储过程时,创建重载存储过程会报错。
目录
相关文章
|
存储 Oracle 关系型数据库
Oracle存储过程迁移ODPS-03(专有云):ODPS1.0支持exists语法
专有云目前还有不少ODPS1.0版本,主要是应对V3之前的平台版本。这个版本的ODPS不支持exists语法,如何换种写法实现。
2971 0
|
SQL 存储 Oracle
Oracle存储过程迁移ODPS-02(专有云):循环逻辑修改一例(构造代码表)
总有同学提问,说我原来在oracle的存储过程中的循环现在maxcompute支持了,我的程序怎么迁移。这个问题其实非常难以回答,一般来说99%的oracle的代码都可以通过maxcompute的SQL和函数来替代,这个也包括一般的循环语句要解的问题。
2064 0
|
存储 SQL 专有云
Oracle存储过程迁移ODPS-01(专有云):支持DML(delete/update/merge)SQL
关系型数据库支持的DML(delete/update/merge)SQL ,在maxcompute(ODPS)该如何写? 总有人问,现写了一个例子,应该可以说明了。 有问题,欢迎大家指正。
2513 0
|
SQL 存储 关系型数据库
如何迁移RDS中的加密存储过程
1. 背景介绍 目前,迁移RDS SQL Server中的数据到其他RDS SQL Server时,使用DTS数据传输服务进行迁,无法将加密存储过程顺利迁出。加密的存储过程,无法script出其定义。 备注:当您考虑加密数据库存储过程之前,建议先做一个备份。2. 查看SQL Server中加密存储过程和函数的方法 1)在存储过程所在的数据库下,创建存储过程sp_decrypt (出自微软BI
2667 0
|
存储 分布式计算 Oracle
|
15天前
|
存储 SQL 关系型数据库
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
|
1月前
|
存储 关系型数据库 MySQL
Mysql基础第二十六天,使用存储过程
Mysql基础第二十六天,使用存储过程
28 0
Mysql基础第二十六天,使用存储过程
|
1月前
|
存储 SQL 关系型数据库
【MySQL 数据库】9、存储过程
【MySQL 数据库】9、存储过程
202 0
|
3月前
|
存储 关系型数据库 MySQL
MySQL-调用存储过程
MySQL-调用存储过程
101 2
|
3月前
|
存储 关系型数据库 MySQL
MySQL 中创建存储过程
MySQL 中创建存储过程
24 1