
# GaussDB Oracle兼容模式
#### **兼容模式设置**
1. GaussDB Oracle语法兼容模式包括：集中式版A兼容模式以及分布式版ORA兼容模式。
2. 目标库为GaussDB，且源库为非MySQL/GoldenDB数据库迁移时，默认往GaussDB Oracle语法兼容模式迁移，因此建议创建Oracle语法兼容数据库。
3. 创建Oracle语法兼容类型数据库。

1. 使用具有创建数据库权限的用户登录GaussDB数据库。
2. 执行以下命令，创建Oracle兼容模式的GaussDB数据库。 
   集中式版：
   **create database** ***databasename*** **dbcompatibility = 'A';**
   分布式版：
   **create database** ***databasename*** **dbcompatibility = 'ORA';**
   
   
3. 执行以下命令，查看是否创建成功。 
   **select \* from pg_database where datname = '** ***databasename*** **';**
   - 集中式版：若datcompatibility兼容性一列为A，则Oracle兼容数据库创建成功。
   
   - 分布式版：若datcompatibility兼容性一列为ORA，则Oracle兼容数据库创建成功。
   
   
   
   
 
#### **GUC参数设置**
当使用Oracle兼容模式，可以设置GUC参数提高GaussDB的兼容性，迁移涉及的GUC参数和影响范围可参见[表1]。
![](https://support.huaweicloud.com/usermanual-ugo/public_sys-resources/caution_3.0-zh-cn.png)
GaussDB数据库提供了许多运行参数，配置这些参数可以影响数据库系统的行为。在修改这些参数时请确保用户理解了这些参数对数据库的影响，否则可能会导致无法预料的结果。
 表1GaussDB（Oracle兼容模式）的GUC参数 
| 兼容Oracle行为配置项                                                                                                                                                                                                                                                  | 兼容性配置项                              | 兼容性配置项名字                                                                                                      | 支持的数据库                          | 是否建议打开 |
|:---|:---|:---|:---|:---|
| behavior_compat_options                             | display_leading_zero                | 浮点数显示配置项-小数点前0显示                                                                                              | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | end_month_calculate                 | add_months函数计算逻辑配置项                                                                                           | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | bind_procedure_searchpath           | 未指定模式名的存储过程中的数据库对象的搜索路径配置项。                                                                                   | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | correct_to_number                   | 控制to_number()结果兼容性的配置项                                                                                        | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | unbind_divide_bound                 | 控制对整数除法的结果进行范围校验                                                    | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | convert_string_digit_to_numeric     | 字符串转换为numeric配置项                                                                                              | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | return_null_string                  | 控制函数lpad()和rpad()结果为空字符串''的显示配置项。                                    | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | merge_update_multi                  | 控制在使用MERGE INTO ... WHEN MATCHED THEN UPDATE和INSERT ... ON DUPLICATE KEY UPDATE，当目标表中一条目标数据与多条源数据冲突时UPDATE行为。 | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | plstmt_implicit_savepoint           | 控制存储过程中更新语句的执行是否拥有独立的子事务。                                           | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | rownum_type_compat                  | 控制ROWNUM的类型。                                                                                                  | 集中式版                            | 是      |
| behavior_compat_options                             | aformat_null_test                   | 控制rowtype类型判空逻辑。                                                                                              | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | aformat_regexp_match                | 控制正则表达式函数的匹配行为。                                                                                               | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | compat_cursor                       | 控制隐式游标状态兼容行为。                                                       | 集中式版                            | 是      |
| behavior_compat_options                             | proc_outparam_override              | 控制存储过程出参的重载行为。                                                       | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | proc_implicit_for_loop_variable     | 控制存储过程中FOR_LOOP查询语句行为。                                                                                        | 集中式版                            | 是      |
| behavior_compat_options                             | allow_procedure_compile_check       | 控制存储过程中select语句和open cursor语句的编译检查。                                                                           | 集中式版                            | 是      |
| behavior_compat_options                             | plsql_security_definer              | 创建存储过程时默认为定义者权限。                                                                                              | 集中式版/分布式版                       | 是      |
| behavior_compat_options                             | plpgsql_dependency                  | 创建函数，存储过程，包支持未定义的对象，可以新建成功。                                                                                   | 集中式版                            | 是      |
| behavior_compat_options                             | sys_function_without_brackets       | 开启该参数后，对于A兼容模式下无入参系统函数，可以不带括号进行调用。                                                                            | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | array_count_compat                  | 用于决定变长数组类型的变量为NULL时，调用array_count函数的返回值为NULL值还是0值。                                                            | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | forbid_skip_tableof_empty_str_elem  | 用于控制无索引的集合类型对于'{1,,3}'格式字符串的输入行 为。                                         | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | enable_use_ora_timestamptz          | 开启后TIMESTAMP AT TIME ZONE语法得到的日期时间跟A数据库保持一致。                                                                  | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | forbid_package_function_with_prefix | 创建package内的函数时，如果函数有前缀，则会报错。                                                                                  | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | show_full_error_lineno              | 用于显示包含存储过程头部信息的行号，创建存储过程打印编译报错信息时，显示从CREATE语句开始的行号。                                                           | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | plsql_rollback_keep_user            | 控制在PL/SQL中rollback和rollback to savepoint是否修改当前用户。                                                             | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | current_sysdate                     | 执行sysdate时，会获取当前操作系统时间。                                                                                       | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | select_into_return_null             | 存储过程语句SELECT select_expressions INTO\[STRICT\] target FROM ... 允许在不指定STRICT并且查询结果为空时给变量赋NULL值。                | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| behavior_compat_options                             | time_constexpr_compact              | 执行时间表达式场景，会根据常量是否携带时区，自动返回with timezone类型或without timezone类型。                                                 | GaussDB V2.0-8.200 集中式/分布式开始的版本 | 是      |
| plsql_compile_check_options                                                                                                                                                                                                                                    | plsql_expression_check              | 开启plsql_expression_check参数，需要同时开启plpgsql_dependency参数。                                                        | GaussDB V2.0-3.2集中式版            | 是      |
| a_format_version                                                                                                                                                                                                                                               | 10c                                 | 数据库平台兼容性行为配置项                                                                                                 | 集中式版/分布式版                       | 是      |
| a_format_dev_version                                                                                                                                                                                | s1                                  | 数据库平台迭代小版本兼容性行为配置项。                                                                                           | GaussDB V2.0-3.1集中式版/分布式版       | 是      |
| a_format_dev_version                                                                                                                                                                                | s2                                  | 数据库平台迭代小版本兼容性行为配置项。                                                                                           | GaussDB V2.0-3.2 集中式版/分布式版      | 是      |
| a_format_dev_version                                                                                                                                                                                | s3                                  | 数据库平台迭代小版本兼容性行为配置项。                                                                                           | GaussDB V2.0-3.3集中式版/分布式版       | 是      |
| a_format_dev_version                                                                                                                                                                                | s4                                  | 数据库平台迭代小版本兼容性行为配置项                                                                                            | GaussDB V2.0-8.0 集中式版/分布式版      | 是      |
| a_format_dev_version                                                                                                                                                                                | s5                                  | 数据库平台迭代小版本兼容性行为配置项。                                                                                           | GaussDB V2.0-8.100 集中式版/分布式版    | 是      |
| a_format_dev_version                                                                                                                                                                                | s6                                  | 数据库平台迭代小版本兼容性行为配置项。                                                                                           | GaussDB V2.0-8.200 集中式版/分布式版    | 是      |
| sql_beta_feature                                                                                                                                                                                                                                               | a_style_coerce                      | 影响decode函数                                                                                                    | 集中式版/分布式版                       | 是      |
   
![](https://support.huaweicloud.com/usermanual-ugo/public_sys-resources/note_3.0-zh-cn.png)
a_format_dev_version参数设置：
- GaussDB V2.0-3.1 企业版，设置为 s1。
- GaussDB V2.0-3.2 企业版，设置为 s2。
- GaussDB V2.0-3.3 企业版，设置为 s3。
- GaussDB V2.0-8.0 企业版，设置为 s4。
- GaussDB V2.0-8.100 企业版，设置为 s5。
- GaussDB V2.0-8.200 企业版和 GaussDB V2.0-10.0，设置为 s6。
 
#### 操作步骤
**集中式版**
1. 连接上GaussDB数据库，切换至GaussDB的安装用户下。 
   **su - omm**
   ![](https://support.huaweicloud.com/usermanual-ugo/public_sys-resources/note_3.0-zh-cn.png)
   "omm"为示例用户，切换时以实际安装用户为准。
   
   
2. 执行以下命令，设置参数。 
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "behavior_compat_options='bind_procedure_searchpath,truncate_numeric_tail_zero,plsql_security_definer,proc_outparam_override,aformat_null_test,rownum_type_compat,allow_procedure_compile_check,proc_implicit_for_loop_variable,plstmt_implicit_savepoint,end_month_calculate,disable_rewrite_nesttable,plpgsql_dependency,display_leading_zero,correct_to_number,unbind_divide_bound,convert_string_digit_to_numeric,return_null_string,aformat_regexp_match,compat_cursor,tableof_elem_constraints,merge_update_multi'";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "plsql_compile_check_options='plsql_expression_check'";**
   **gs_guc reload -Z datanode -N all -I all -c "sql_beta_feature='a_style_coerce'";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "a_format_version='10c'";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "a_format_dev_version='s4'";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "intervalstyle=a";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "a_format_date_timestamp='on'";**
   ![](https://support.huaweicloud.com/usermanual-ugo/public_sys-resources/caution_3.0-zh-cn.png)
   - 以上GUC参数设置均以 Oracle 为源、GaussDB 集中式版 V2.0 - 8.0 企业版为目标库，其他链路的 GUC 参数，请参考预检查功能中提示的参数名称和参数值，分别替换掉上述命令中等号前后的内容。
   
   - GUC 参数 plsql_compile_check_options 的值 plsql_expression_check 结构迁移全部完成后，需要设置为空。
   
   
   gs_guc reload -Z coordinator -Z datanode -N all -I all -c "plsql_compile_check_options=''";
   
   
**分布式版**
1. 连接上GaussDB数据库，切换至GaussDB的安装用户下。 
   **su - omm**
   ![](https://support.huaweicloud.com/usermanual-ugo/public_sys-resources/note_3.0-zh-cn.png)
   "omm"为示例用户，切换时以实际安装用户为准。
   
   
2. 执行以下命令设置参数。 
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "behavior_compat_options='bind_procedure_searchpath,truncate_numeric_tail_zero,plsql_security_definer,proc_outparam_override,aformat_null_test,plstmt_implicit_savepoint,end_month_calculate,disable_rewrite_nesttable,display_leading_zero,correct_to_number,unbind_divide_bound,convert_string_digit_to_numeric,return_null_string,aformat_regexp_match,tableof_elem_constraints,merge_update_multi'";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "a_format_version='10c'";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "a_format_dev_version='s4'";**
   **gs_guc reload -Z datanode -N all -I all -c "sql_beta_feature='a_style_coerce'";**
   **gs_guc reload -Z coordinator -Z datanode -N all -I all -c "a_format_date_timestamp='on'";**
   ![](https://support.huaweicloud.com/usermanual-ugo/public_sys-resources/caution_3.0-zh-cn.png)
   以上GUC参数设置均以 Oracle 为源、GaussDB 分布式版 V2.0 - 8.0 企业版为目标库，其他链路的 GUC 参数，请参考预检查功能中提示的参数名称和参数值，分别替换掉上述命令中等号前后的内容。
   
   
 
