Emulate Oracle PL/SQL associative arrays in Amazon Aurora PostgreSQL and Amazon RDS for PostgreSQL
Rajkumar Raghuwanshi, Bhanu Ganesh Gudivada, and Sachin Khanna, Amazon Web Services
Summary
This pattern describes how to emulate Oracle PL/SQL associative arrays with empty index positions in Amazon Aurora PostgreSQL
We provide a PostgreSQL alternative to using aws_oracle_ext
functions for handling empty index positions when migrating an Oracle database. This pattern uses an additional column to store index positions, and it maintains Oracle's handling of sparse arrays while incorporating native PostgreSQL capabilities.
Oracle
In Oracle, collections can be initialized as empty and populated using the EXTEND
collection method, which appends NULL
elements to the array. When working with PL/SQL associative arrays indexed by PLS_INTEGER
, the EXTEND
method adds NULL
elements sequentially, but elements can also be initialized at nonsequential index positions. Any index position that isn't explicitly initialized remains empty.
This flexibility allows for sparse array structures where elements can be populated at arbitrary positions. When iterating through collections using a FOR LOOP
with FIRST
and LAST
bounds, only the initialized elements (whether NULL
or with a defined value) are processed, while empty positions are skipped.
PostgreSQL (Amazon Aurora and Amazon RDS)
PostgreSQL handles empty values differently from NULL
values. It stores empty values as distinct entities that use one byte of storage. When an array has empty values, PostgreSQL assigns sequential index positions just like non-empty values. But sequential indexing requires additional processing because the system must iterate through all indexed positions, including the empty ones. This makes traditional array creation inefficient for sparse datasets.
AWS Schema Conversion Tool
The AWS Schema Conversion Tool (AWS SCT) typically handles Oracle-to-PostgreSQL migrations using aws_oracle_ext
functions. In this pattern, we propose an alternative approach that uses native PostgreSQL capabilities, which combines PostgreSQL array types with an additional column for storing index positions. The system can then iterate through arrays using just the index column.
Prerequisites and limitations
Prerequisites
An active AWS account.
An AWS Identity and Access Management (IAM) user with administrator permissions.
An instance that’s compatible with Amazon RDS or Aurora PostgreSQL.
A basic understanding of relational databases.
Limitations
Some AWS services aren’t available in all AWS Regions. For Region availability, see AWS services by Region
. For specific endpoints, see the Service endpoints and quotas page, and choose the link for the service.
Product versions
This pattern was tested with the following versions:
Amazon Aurora PostgreSQL 13.3
Amazon RDS for PostgreSQL 13.3
AWS SCT 1.0.674
Oracle 12c EE 12.2
Architecture
Source technology stack
On-premises Oracle database
Target technology stack
Amazon Aurora PostgreSQL
Amazon RDS for PostgreSQL
Target architecture

The diagram shows the following:
A source Amazon RDS for Oracle database instance
An Amazon EC2 instance with AWS SCT for converting Oracle functions to the PostgreSQL equivalent
A target database that’s compatible with Amazon Aurora PostgreSQL
Tools
AWS services
Amazon Aurora is a fully managed relational database engine that's built for the cloud and compatible with MySQL and PostgreSQL.
Amazon Aurora PostgreSQL-Compatible Edition is a fully managed, ACID-compliant relational database engine that helps you set up, operate, and scale PostgreSQL deployments.
Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the AWS Cloud. You can launch as many virtual servers as you need and quickly scale them up or down.
Amazon Relational Database Service (Amazon RDS) helps you set up, operate, and scale a relational database in the AWS Cloud.
Amazon Relational Database Service (Amazon RDS) for Oracle helps you set up, operate, and scale an Oracle relational database in the AWS Cloud.
Amazon Relational Database Service (Amazon RDS) for PostgreSQL helps you set up, operate, and scale a PostgreSQL relational database in the AWS Cloud.
AWS Schema Conversion Tool (AWS SCT) supports heterogeneous database migrations by automatically converting the source database schema and a majority of the custom code to a format that’s compatible with the target database.
Other tools
Oracle SQL Developer
is an integrated development environment that simplifies the development and management of Oracle databases in both traditional and cloud-based deployments. pgAdmin
is an open source management tool for PostgreSQL. It provides a graphical interface that helps you create, maintain, and use database objects. In this pattern, pgAdmin connects to the RDS for PostgreSQL database instance and queries the data. Alternatively, you can use the psql command line client.
Best practices
Test data set boundaries and edge scenarios.
Consider implementing error handling for out-of-bounds index conditions.
Optimize queries to avoid scanning sparse data sets.
Epics
Task | Description | Skills required |
---|---|---|
Create a source PL/SQL block in Oracle. | Create a source PL/SQL block in Oracle that uses the following associative array:
| DBA |
Run the PL/SQL block. | Run the source PL/SQL block in Oracle. If there are gaps between the index values of an associative array, no data is stored in those gaps. This allows the Oracle loop to iterate only through the index positions. | DBA |
Review the output. | Five elements were inserted into the array (
| DBA |
Task | Description | Skills required |
---|---|---|
Create a target PL/pgSQL block in PostgreSQL. | Create a target PL/pgSQL block in PostgreSQL that uses the following associative array:
| DBA |
Run the PL/pgSQL block. | Run the target PL/pgSQL block in PostgreSQL. If there are gaps between the index values of an associative array, no data is stored in those gaps. This allows the Oracle loop to iterate only through the index positions. | DBA |
Review the output. | The array length is greater than 5 because
| DBA |
Task | Description | Skills required |
---|---|---|
Create a target PL/pgSQL block with an array and user-defined type. | To optimize performance and match Oracle's functionality, we can create a user-defined type that stores both index positions and their corresponding data. This approach reduces unnecessary iterations by maintaining direct associations between indices and values.
| DBA |
Run the PL/pgSQL block. | Run the target PL/pgSQL block. If there are gaps between the index values of an associative array, no data is stored in those gaps. This allows the Oracle loop to iterate only through the index positions. | DBA |
Review the output. | As shown in the following output, the user-defined type stores only populated data elements, which means that the array length matches the number of values. As a result,
| DBA |
Related resources
AWS documentation
Other documentation