

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将 Oracle 数据库错误代码迁移到 Amazon Aurora PostgreSQL-Compatible 数据库
<a name="migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database"></a>

*Sai Parthasaradhi 和 Veeranjaneyulu Grandhi，Amazon Web Services*

## Summary
<a name="migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database-summary"></a>

此模式显示如何使用预定义的元数据表将 Oracle 数据库错误代码迁移到 [Amazon Aurora PostgreSQL-Compatible Edition](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html) 数据库。

Oracle 数据库错误代码并不总是有相应的 PostgreSQL 错误代码。错误代码的这种差异会使您很难在目标 PostgreSQL 架构中配置过程或函数的处理逻辑。

您可以通过将对您的 PL/pgSQL 程序有意义的源数据库和目标数据库错误代码存储在元数据表中来简化流程。然后，将该表配置为标记有效的 Oracle 数据库错误代码，并将它们映射到其 PostgreSQL 等效项，然后再继续执行剩余的流程逻辑。如果元数据表中没有 Oracle 数据库错误代码，则流程将退出，但有异常。然后，如果程序需要，您可以手动查看错误详细信息并将新的错误代码添加到表中。

通过使用此配置，Amazon Aurora PostgreSQL-Compatible 数据库可以像 Oracle 源数据库处理错误一样处理错误。

**注意**  
要配置 PostgreSQL 数据库以正确处理 Oracle 数据库错误代码，通常需要更改数据库和应用程序代码。

## 先决条件和限制
<a name="migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database-prereqs"></a>

**先决条件**
+ 一个有效的 Amazon Web Services account
+ 已启动并正在运行的实例和侦听器服务的 Oracle 源数据库
+ 已启动并正在运行的 Amazon Aurora PostgreSQL-Compatible 集群
+ 熟悉 Oracle 数据库
+ 熟悉 PostgreSQL 数据库

## 架构
<a name="migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database-architecture"></a>

下图显示了用于验证和处理数据错误代码的 Amazon Aurora PostgreSQL-Compatible 数据库的工作流程示例：

![Aurora PostgreSQL 兼容数据库的数据错误代码验证与处理](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/82751f40-2fd9-4ce7-ab61-0874552d857b/images/b7ab627e-8f34-4635-8660-93c5c80ce38d.png)


下图显示了如下工作流：

1. 表中包含了 Oracle 数据库错误代码和分类及其等效的 PostgreSQL 错误代码和分类。该表包含 **valid\_error** 列，用于对特定的预定义错误代码是否有效进行分类。

1. **当一个 PL/pgSQL 函数（**func\_processdat** a）抛出异常时，它会调用第二个函数（error\_validation）。 PL/pgSQL **

1. **error\_validation** 函数接受 Oracle 数据库错误代码作为输入参数。然后，该函数对照表检查传入的错误代码，以查看该错误是否包含在表中。

1. 如果表中包含 Oracle 数据库错误代码，则 **error\_validation** 函数将返回**真**值并且流程逻辑继续运行。如果表中未包含错误代码，则该函数返回**假**值，流程逻辑退出，且出现异常。

1. 当函数返回**假**值时，应用程序的职能主管将手动查看错误详细信息以确定其有效性。

1. 然后，要么手动将新的错误代码添加到表中，要么不手动添加。如果错误代码有效且已添加到表中，则下次发生异常时 **error\_validation** 函数将返回**真**值。如果错误代码无效，并且异常发生时流程必须失效，则不会将错误代码添加到表中。

**技术堆栈**
+ Amazon Aurora PostgreSQL
+ pgAdmin
+ Oracle SQL Developer

## 工具
<a name="migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database-tools"></a>
+ [Amazon Aurora PostgreSQL 兼容版](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html)是一个完全托管的、与 ACID 兼容的关系数据库引擎，可帮助您建立、运行和扩展 PostgreSQL 部署。
+ [pgAdmin](https://www.pgadmin.org/) 是 PostgreSQL 的开源管理和开发工具。它提供了图形界面，可简化数据库对象的创建、维护和使用。
+ [Oracle SQL Developer](https://www.oracle.com/in/database/technologies/appdev/sqldeveloper-landing.html) 是一个集成的免费开发环境，可简化传统部署和云部署中 Oracle 数据库的开发和管理。

## 操作说明
<a name="migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database-epics"></a>

### 将 Oracle 数据库错误代码迁移到 Amazon Aurora PostgreSQL-Compatible 数据库中
<a name="migrate-oracle-database-error-codes-to-your-amazon-aurora-postgresql-compatible-database"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在 Amazon Aurora PostgreSQL-Compatible 数据库中创建表。 | 运行以下 PostgreSQL [CREATE TABLE](https://www.postgresql.org/docs/current/sql-createtable.html) 命令：<pre>(<br /><br />    source_error_code numeric NOT NULL,<br /><br />    target_error_code character varying NOT NULL,<br /><br />    valid_error character varying(1) NOT NULL<br /><br />); </pre> | PostgreSQL 开发人员，甲骨文，适用于 PostgreSQL RDS/Aurora  | 
| 将 PostgreSQL 错误代码及其相应的 Oracle 数据库错误代码添加到表中。 | 运行 PostgreSQL [INSERT](https://www.postgresql.org/docs/current/sql-insert.html) 命令将所需错误代码值添加到 **error\_codes** 表中。<br />PostgreSQL 错误代码必须使用字符变化的数据类型（**SQLSTATE** 值）。Oracle 错误代码必须使用数字数据类型（**SQLCODE** 值）。<br />**插入语句示例：**<pre>insert into error_codes values (-1817,'22007','Y');<br />insert into error_codes values (-1816,'22007','Y');<br />insert into error_codes values (-3114,'08006','N');</pre>如果您要捕获特定于 Oracle 的 Java 数据库连接（JDBC）异常，则必须将这些异常替换为通用的跨数据库异常或切换到 PostgreSQL 特定的异常。 | PostgreSQL 开发人员，甲骨文，适用于 PostgreSQL RDS/Aurora  | 
| 创建用于验证错误代码的 PL/pgSQL 函数。 | 通过运行 [Postgre](https://www.postgresql.org/docs/current/sql-createfunction.html) SQL 创建 PL/pgSQL 函数命令来创建函数。确保该函数执行以下操作：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database.html) | PostgreSQL 开发人员，甲骨文，适用于 PostgreSQL RDS/Aurora  | 
| 手动查看 PL/pgSQL 函数记录的新错误代码。 | 手动查看新错误代码。<br />如果新错误代码对用例有效，请运行 PostgreSQL **INSERT** 命令将其添加到 **error\_codes** 表中。<br />–或者–<br />如果新错误代码对用例无效，请不要将其添加到表中。当错误发生时，流程逻辑将继续失效并以异常方式退出。 | PostgreSQL 开发人员，甲骨文，适用于 PostgreSQL RDS/Aurora  | 

## 相关资源
<a name="migrate-oracle-database-error-codes-to-an-amazon-aurora-postgresql-compatible-database-resources"></a>

[附录 A. PostgreSQL 错误代码](https://www.postgresql.org/docs/11/errcodes-appendix.html)（PostgreSQL 文档）

[数据库错误消息](https://docs.oracle.com/cd/E11882_01/server.112/e17766/toc.htm)（Oracle 数据库文档）