Oracle数据库迁移到PostgreSQL/EDB初探

由于某些非技术方面的原因,我们也在搞一些开源数据库引入,替换商业数据库,于是瞄上了PostgreSQL。


PostgreSQL本身的技术不在这里做介绍,虽然国内PostgreSQL没有Mysql那么流行,但是搜索一下,还是能够找到不少PostgreSQL中文的一些技术资料, 其中PG的官方文档是最详尽且最有助于学习PG的:

最新的9.4版本的online doc:http://www.postgresql.org/docs/9.4/static/index.html 

山东瀚高科技提供的中文翻译文档:http://www.highgo.com.cn/docs/docs90cn/


Oracle迁移到PostgreSQL:

比较流行的迁移工具 ora2pg:  http://ora2pg.darold.net/config.html#testing

大致原理:

该工具使用perl写的, 主要迁移功能都由ora2pg.conf 这个参数文件控制,该参数文件定义源库Oracle和目标库的DSN,通过DBD::Oracle Perl 、 DBD::Pg Perl 的module连接到源库和目标库, 按照用户制定的参数,导出源库Oracle的相关对象结构(也可以连数据一起导),导入到目标库PG。 导入可以是在线的(导出脚本不落地直接导入PG),也可以先生成导出脚本到本地,手动修改复核后,再手动使用psql导入PG。 

ora2pg命令行各参数使用也比较清晰明了, 支持各种导出导入选项,比如你可以只导某些schema或者对象,或者排除某些schema或者对象不导, 也可以什么都不做,只对源库做一个迁移PG的分析,出具一份报告等等。

具体使用方法,还是自己去实际尝试会比较好。 


使用Ora2pg的方案将Oracle迁移至PG, 遇到的问题多少与源Oracle数据库有多少与PG不兼容的东西成正比。下面是我们遇到的问题简单总结:

应用程序里面sqlmap.xml 人工REVIEW时的问题发现:

OraclePostgreSQL
dual表没有dual表,可以直接select 1、select user、select xxx
时间函数(sysdate)

current_date

current_time

current_timestamp

trunctrunc/date trunc(时间截断函数)
sys_guid()有类似sys_guid的函数uuid_generate_v4,但需要安装,create extension "uuid-oosp".
nvlPG可以使用COALESCE
where rownum < ...select row_number() over() , * from XXXX或者limit



PG导入ora2pg产生的迁移脚本时发现的问题:

tables      

DEFAULT Sys_guid();

session# serial# 字段

DEFAULT uuid_generate_v4;

去掉#

partitions  父表在tables中已创建,创建子表时,由于大小写问题提示找不到父表。表名称区分大小写,继承父表时,大小写改为与父表相同。增加或减少分区要修改触发器函数。
synonyms  

转换过来的语句类似如下:

CREATE VIEW public.tablename AS SELECT * FROM owner.tablename SECURITY DEFINER;

PG中没有同义词,自动创建为视图,转换过来的视图名称与存在的表名相同,需要修改视图名称。

SECURITY DEFINER不能加到创建语句的后面,可通过授权来控制权限。

packages    

1、PG中没有v$session.(oracle原来的packages里面大多有这个)

select sid, serial# into v_sid, v_serial# from v$session

2、有些转换过来的语句顺序不正确,需要重构。

3、packages自动转换为function,且function会创建到以原来packages命名的schema下

ora2pg会把Oracle里面的package header转换为同名的schema
procedures  

1、dbms_output.put_line('成功插入数据!');-->RAISE NOTICE '%',('成功插入数据!');

2、 dbms_output.put_line(sqlerrm) --- RAISE NOTICE '%', sqlerrm;

3、表的子查询必须包围在圆括弧里并且必须赋予一个别名

4、start with connect by 递归查询在PG中WITH RECURSIVE

views   

1、字符类型问题

2、递归查询没有转换成功

3、外连接中的“+”号没有转换

4、DECODE函数需要重构

5、COALESCE 函数返回值类型不匹配

1、字符类型需要调整。

2、start with connect by 递归查询在PG中WITH RECURSIVE

3、 (+)这样的外连接写法需要调整为SQL标准的 table1 [LEFT|RIGHT|FULL] OUTER JOIN table2 ON (...);

4、DECODE函数需要重构成(case when some_column = 'some_value' then 'some_other_value' when ... then ... else 'some_default_value' end ) as some_column;

5、COALESCE 函数返回值类型不匹配、需要类型转换。

等等。

可以看到直接将Oracle迁移到PostgreSQL,无论是应用层,还是数据库层面, 都还是有很多改写工作要做的。 

由于有上述诸多问题,我们研究了一下号称为替换Oracle而生的EnterpriseDB(PostgreSQL的商业版):

http://www.enterprisedb.com/products-services-training/products/documentation/enterpriseedition

EnterpriseDB的安装指引:

http://www.enterprisedb.com/docs/en/9.4/instguide/toc.html

EnterpriseDB的官方文档:

http://www.enterprisedb.com/docs/en/9.4/eeguide/toc.html


EnterpriseDB的Oracle迁移PG的工具Migration Toolkit使用指引:

http://www.enterprisedb.com/docs/en/9.4/migrate/toc.html

工具使用比较简单,简单配置一些参数接口, 迁移过程发现的问题相比ora2pg少了很多,但还是有一些:

例如EDB中没有function sys_connect_by_path函数、 oracle trigger语法里面的referencing new as new old as old等。


结论:

虽然EDB的Migration Toolkit这个迁移工具本身没有像ora2pg那么多灵活的迁移选项组合, 但是其功能基本满足迁移需求,而且重要的是由于EDB在PostgreSQL的基础上,外面包了一层,实现了很多Oracle的概念,例如前面提到的dual表、同义词、pkg、内建函数,甚至Oracle的分区表语法可以直接在EDB运行。所以,同样的源Oracle数据库,我们发现90%左右的都可以直接在目标EnterpriseDB运行了,只需要修改一少部分。 


具体EnterpriseDB的兼容Oracle的清单,参考

http://www.enterprisedb.com/products-services-training/products/postgres-plus-advanced-server?quicktabs_advanceservertab=3#quicktabs-advanceservertab


所以,从节省成本与运维稳定两方面平衡考虑, 对于新建系统直接采用PostgreSQL,对于已有系统迁移到PostgreSQL,使用EnterpriseDB也不失为一个过渡期的较好选择。 


附录:

ora2pg --help


Usage: ora2pg [-dhpqv --estimate_cost --dump_as_html] [--option value]


    -a | --allow str  : coma separated list of objects to allow from export.

Can be used with SHOW_COLUMN too.

    -b | --basedir dir: Used to set the default output directory, where files

resulting from exports will be stored.

    -c | --conf file  : Used to set an alternate configuration file than the

default /etc/ora2pg/ora2pg.conf.

    -d | --debug      : Enable verbose output.

    -e | --exclude str: coma separated list of objects to exclude from export.

Can be used with SHOW_COLUMN too.

    -h | --help       : Print this short help.

    -i | --input file : File containing Oracle PL/SQL code to convert with

no Oracle database connection initiated.

    -j | --jobs num   : number of parallel process to send data to PostgreSQL.

    -J | --copies num : number of parallel connection to extract data from Oracle.

    -l | --log file   : Used to set a log file. Default is stdout.

    -L | --limit num  : number of tuples extracted from Oracle and stored in

memory before writing, default: 10000.

    -n | --namespace schema : Used to set the Oracle schema to extract from.

    -o | --out file   : Used to set the path to the output file where SQL will

be written. Default: output.sql in running directory.

    -p | --plsql      : Enable PLSQL to PLPSQL code conversion.

    -P | --parallel num: Number of parallel tables to extract at the same time.

    -q | --quiet      : disable progress bar.

    -s | --source DSN : Allow to set the Oracle DBI datasource.

    -t | --type export: Used to set the export type. It will override the one

given in the configuration file (TYPE).

    -u | --user name  : Used to set the Oracle database connection user.

    -v | --version    : Show Ora2Pg Version and exit.

    -w | --password pwd : Used to set the password of the Oracle database user.

    --forceowner: if set to 1 force ora2pg to set tables and sequences owner

 like in Oracle database. If the value is set to a username this

 one will be used as the objects owner. By default it's the user

 used to connect to the Pg database that will be the owner.

    --nls_lang code: use this to set the Oracle NLS_LANG client encoding.

    --client_encoding code: Use this to set the PostgreSQL client encoding.

    --view_as_table str: coma separated list of view to export as table.

    --estimate_cost   : activate the migration cost evalution with SHOW_REPORT

    --cost_unit_value minutes: number of minutes for a cost evalution unit.

 default: 5 minutes, correspond to a migration conducted by a

 PostgreSQL expert. Set it to 10 if this is your first migration.

   --dump_as_html     : force ora2pg to dump report in HTML, used only with

                        SHOW_REPORT. Default is to dump report as simple text.

   --init_project NAME: initialise a typical ora2pg project tree. Top directory

                        will be created under project base dir.

   --project_base DIR : define the base dir for ora2pg project trees. Default

                        is current directory.


See full documentation at http://ora2pg.darold.net/ for more help or see manpage with 'man ora2pg'.



EDB的Migration Toolkit:


runMTK.sh -help

Running EnterpriseDB Migration Toolkit (Build 48.0.1) ...


EnterpriseDB Migration Toolkit (Build 48.0.1)


Usage: runMTK [-options] SCHEMA


If no option is specified, the complete schema will be imported.


where options include:

-helpDisplay the application command-line usage.

-versionDisplay the application version information.

-verbose [on|off] Display application log messages on standard output (default: on).


-schemaOnlyImport the schema object definitions only.

-dataOnlyImport the table data only. When -tables is in place, it imports data only for the selected tables. Note: If there are any FK constraints defined on target tables, use -truncLoad option along with this option.


-sourcedbtype db_type The -sourcedbtype option specifies the source database type. db_type may be one of the following values: mysql, oracle, sqlserver, sybase, postgresql, enterprisedb. db_type is case-insensitive. By default, db_type is oracle.

-targetdbtype db_type The -targetdbtype option specifies the target database type. db_type may be one of the following values: oracle, sqlserver, postgresql, enterprisedb. db_type is case-insensitive. By default, db_type is enterprisedb.


-allTablesImport all tables.

-tables LISTImport comma-separated list of tables.

-constraintsImport the table constraints.

-indexesImport the table indexes.

-triggersImport the table triggers.

-allViewsImport all Views.

-views LISTImport comma-separated list of Views.

-allProcsImport all stored procedures.

-procs LISTImport comma-separated list of stored procedures.

-allFuncsImport all functions.

-funcs LISTImport comma-separated list of functions.

-allPackagesImport all packages.

-packages LIST Import comma-separated list of packages.

-allSequencesImport all sequences.

-sequences LIST Import comma-separated list of sequences.

-targetSchema NAME Name of the target schema (default: target schema is named after source schema).

-allDBLinksImport all Database Links.

-allSynonymsIt enables the migration of all public and private synonyms from an Oracle database to an Advanced Server database.  If a synonym with the same name already exists in the target database, the existing synonym will be replaced with the migrated version.

-allPublicSynonymsIt enables the migration of all public synonyms from an Oracle database to an Advanced Server database.  If a synonym with the same name already exists in the target database, the existing synonym will be replaced with the migrated version.

-allPrivateSynonymsIt enables the migration of all private synonyms from an Oracle database to an Advanced Server database.  If a synonym with the same name already exists in the target database, the existing synonym will be replaced with the migrated version.


-dropSchema [true|false] Drop the schema if it already exists in the target database (default: false).

-truncLoadIt disables any constraints on target table and truncates the data from the table before importing new data. This option can only be used with -dataOnly.

-safeModeTransfer data in safe mode using plain SQL statements.

-copyDelimiterSpecify a single character to be used as delimiter in copy command when loading table data. Default is \t

-batchSizeSpecify the Batch Size to be used by the bulk inserts. Valid values are  1-1000, default batch size is 1000, reduce if you run into Out of Memory exception

-cpBatchSize Specify the Batch Size in MB, to be used in the Copy Command. Valid value is > 0, default batch size is 8 MB

-fetchSize Specify fetch size in terms of number of rows should be fetched in result set at a time. This option can be used when tables contain millions of rows and you want to avoid out of memory errors.

-filterPropThe properties file that contains table where clause.

-skipFKConstSkip migration of FK constraints.

-skipCKConstSkip migration of Check constraints.

-ignoreCheckConstFilterBy default MTK does not migrate Check constraints and Default clauses from Sybase, use this option to turn off this filter.

-fastCopyBypass WAL logging to perform the COPY operation in an optimized way, default disabled.

-customColTypeMapping LISTUse custom type mapping represented by a semi-colon separated list, where each entry is specified using COL_NAME_REG_EXPR=TYPE pair. e.g. .*ID=INTEGER

-customColTypeMappingFile PROP_FILEThe custom type mapping represented by a properties file, where each entry is specified using COL_NAME_REG_EXPR=TYPE pair. e.g. .*ID=INTEGER

-offlineMigration [PATH] This performs offline migration and saves the DDL/DML scripts in files for a later execution. By default the script files will be saved under user home folder, if required follow -offlineMigration option with a custom path. 

-logDir LOG_PATH Specify a custom path to save the log file. By default, on Linux the logs will be saved under folder $HOME/.enterprisedb/migration-toolkit/logs. In case of Windows logs will be saved under folder %HOMEDRIVE%%HOMEPATH%\.enterprisedb\migration-toolkit\logs.

-copyViaDBLinkOra This option can be used to copy data using dblink_ora COPY commad. This option can only be used in Oracle to EnterpriseDB migration mode.

-singleDataFileUse single SQL file for offline data storage for all tables. This option cannot be used in COPY format.

-allUsers Import all users and roles from the source database.

-users LIST Import the selected users/roles from the source database. LIST is a comma-separated list of user/role names e.g. -users MTK,SAMPLE

-allRules Import all rules from the source database.

-rules LIST Import the selected rules from the source database. LIST is a comma-separated list of rule names e.g. -rules high_sal_emp,low_sal_emp

-allGroups Import all groups from the source database.

-groups LIST Import the selected groups from the source database. LIST is a comma-separated list of group names e.g. -groups acct_emp,mkt_emp

-allDomains Import all domain, enumeration and composite types from the source database.

-domains LIST Import the selected domain, enumeration and composite types from the source database. LIST is a comma-separated list of domain names e.g. -domains d_email,d_dob, mood

-objecttypesImport the user-defined object types.

-replaceNullChar <CHAR> If null character is part of a column value, the data migration fails over JDBC protocol. This option can be used to replace null character with a user-specified character.

-importPartitionAsTable [LIST] Use this option to import Oracle Partitioned table as a normal table in EnterpriseDB. To apply the rule on a selected set of tables, follow the option by a comma-separated list of table names.

-enableConstBeforeDataLoad Use this option to re-enable constraints (and triggers) before data load. This is useful in the scenario when the migrated table is mapped to a partition table in EnterpriseDB.

-checkFunctionBodies [true|false] When set to false, it disables validation of the function body during function creation, this is to avoid errors if function contains forward references. Applicable when target database is Postgres/EnterpriseDB, default is true.

-retryCount VALUESpecify the number of re-attempts performed by MTK to migrate objects that failed due to cross-schema dependencies. The VALUE parameter should be greater than 0, default is 2.

-analyze It invokes ANALYZE operation against a target Postgres or Postgres Plus Advanced Server database. The ANALYZE collects statistics for the migrated tables that are utilized for efficient query plans.

-vacuumAnalyze It invokes VACUUM and ANALYZE operations against a target Postgres or Postgres Plus Advanced Server database. The VACUUM reclaims dead tuple storage whereas ANALYZE collects statistics for the migrated tables that are utilized for efficient query plans.

-loaderCount VALUESpecify the number of jobs (threads) to perform data load in parallel. The VALUE parameter should be greater than 0, default is 1.

-logFileSize VALUEIt represents the maximum file size limit (in MB) before rotating to a new log file, defaults to 50MB.

-logFileCount VALUEIt represents the number of files to maintain in log file rotation history, defaults to 20. Specify a value of zero to disable log file rotation.

-useOraCaseIt preserves the identifier case while migrating from Oracle, except for functions, procedures and packages unless identifier names are given in quotes. 

-logBadSQLIt saves the DDL scripts for the objects that fail to migrate, in a .sql file in log folder.

-targetDBVersionIt represents the major.minor version of the target database. This option is applicable for offline migration mode and is used to validate certain migration options as per target db version [default is 9.4 for EnterpriseDB database].


Database Connection Information:

The application will read the connectivity information for the source and target database servers from toolkit.properties file.

Refer to MTK readme document for more information.





转载于:https://my.oschina.net/rocky0202/blog/403082

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值