Amazon Redshift is a fully managed, petabyte-scale data warehouse service in the cloud. With Amazon Redshift, you can analyze all your data to derive holistic insights about your business and your customers. It supports stored procedures where prepared SQL code is saved and the code can be reused over and over again.
Stored procedures are commonly used to encapsulate logic for data transformation, data validation, and business-specific logic. By combining multiple SQL steps into a stored procedure, you can create reusable code blocks that can run together as a single transaction or multiple individual transactions. You can also schedule stored procedures to automate data processing on Amazon Redshift. For more information, refer to Bringing your stored procedures to Amazon Redshift.
In the Redshift stored procedure default atomic transaction mode, a call to a Redshift stored procedure will create its own transaction when the call starts or is part of the existing transaction if an explicit transaction is opened before the stored procedure is called. All the statements inside a procedure behave as if they are in a single transaction block that ends when the stored procedure call finishes. A nested call to another procedure is treated like any other SQL statement and operates within the context of the same transaction as the caller. Statements for TRUNCATE, COMMIT, and ROLLBACK and the exception handling block with arbitrary SQL statements close the current transaction and start a new transaction implicitly. This behavior can cause challenges in migration to Amazon Redshift from other systems like Teradata.
In this post, we discuss the enhancements to Amazon Redshift stored procedures for non-atomic transaction mode. This mode provides enhanced transaction controls that enable you to automatically commit the statements inside the stored procedure.
The new non-atomic transaction mode feature provides three enhancements on stored procedures in Amazon Redshift:
Some restrictions have also been introduced for Redshift stored procedures:
configuration\_parameter option for non-atomic transaction mode stored proceduresCursors in non-atomic transaction mode stored procedures will behave differently compared to the default atomic transaction mode:
The following are key advantages of this feature from a user perspective:
The new optional keyword NONATOMIC has been added to the stored procedure definition syntax, as shown in the following code:
This optional keyword creates the stored procedure under the non-atomic transaction mode. If you don’t specify the keyword, then the default atomic mode will be the transaction mode when creating the stored procedure.
NONATOMIC means each DML and DDL statement in the procedure will be implicitly committed.
Without non-atomic mode, the procedure will create its own transaction when the call starts or be part of the existing transaction if an explicit transaction is opened before it is called. Every statement within the stored procedure will belong to this one transaction.
Let’s consider the customer contact table custcontacts, which stores customer primary and secondary contact phone numbers:
We insert three sample customer records with no contact values:
You need to create a stored procedure to update the primary and secondary phone numbers. The requirement is not to roll back updates to the primary contact number if updates to the secondary contact number fail for some reason.
You can achieve this by creating the stored procedure with the NONATOMIC keyword. The NONATOMIC keyword ensures that each statement in the stored procedure runs in its own implicit transaction block. Therefore, if the UPDATE statement for the secondary phone fails, then it won’t roll back the data update made to the primary phone. See the following code:
Now let’s call the stored procedure passing the secondary phone number with more than 10 digits, which will fail in the secondaryphone UPDATE statement due to incorrect length:
The preceding procedure call will update the primary phone number successfully. The secondary phone number update fails. However, the primaryphone update will not roll back because it ran in its own implicit transaction block due to the NONATOMIC clause in the stored procedure definition.
Exceptions are handled in stored procedures differently based on the atomic or non-atomic mode:
Let’s continue with the previous example to illustrate exception handling in non-atomic mode.
Create the following table to log exceptions raised by stored procedures:
Now update the sp\_update\_custcontacts() procedure to handle exceptions. Note that we’re adding an EXCEPTION block in the procedure definition. It inserts a record in the procedure\_log table in the event of an exception.
Now create one more stored procedure, which will call the preceding procedure. It also has an EXCEPTION block and inserts a record in the procedure\_log table in the event of an exception.
Let’s call the parent procedure we created:
This in turn will call the sp\_update\_custcontacts() procedure. The inner procedure sp\_update\_custcontacts() will fail because we’re updating the secondary phone with an invalid value. The control will enter the EXCEPTION block of the sp\_update\_custcontacts() procedure and make an insert into the procedure\_log table.
However, it will not re-raise the exception in non-atomic mode. Therefore, the parent procedure sp\_update\_customer() will not get the exception passed from the sp\_update\_custcontacts() procedure. The control will not enter the EXCEPTION block of the sp\_update\_customer() procedure.
If you query the procedure\_log table, you will see an entry only for the error handled by the sp\_update\_custcontacts() procedure:

Now redefine the sp\_update\_custcontacts() procedure with the RAISE statement:
Let’s call the parent stored procedure sp\_update\_customer() again:
Now the inner procedure sp\_update\_custcontacts() will re-raise the exception to the parent procedure sp\_update\_customer() after handling the exception in its own EXCEPTION block. Then the control will reach the EXCEPTION block in the parent procedure and insert another record into the procedure\_log table.
If you query the procedure\_log table now, you will see two entries: one by the inner procedure sp\_update\_custcontacts() and another by the parent procedure sp\_update\_customer(). This demonstrates that the RAISE statement in the inner procedure re-raised the exception.

You can issue a START TRANSACTION statement to begin a transaction block inside the stored procedure. It will open a new transaction inside the stored procedure. For examples, refer to Nonatomic mode stored procedure transaction management.
In this post, we discussed the enhancements to Redshift stored procedures for non-atomic transaction mode, which provides enhanced transaction controls to enable you to automatically commit the statements inside the stored procedure. This mode also enables easier migration to Amazon Redshift from other systems like Teradata. Try out these enhancements and let us know your experience in comments.
Milind Oke is a Data Warehouse Specialist Solutions Architect based out of New York. He has been building data warehouse solutions for over 15 years and specializes in Amazon Redshift.
Satesh Sonti is a Sr. Analytics Specialist Solutions Architect based out of Atlanta, specialized in building enterprise data platforms, data warehousing, and analytics solutions. He has over 17 years of experience in building data assets and leading complex data platform programs for banking and insurance clients across the globe.
Kiran Chinta is a Software Development Manager at Amazon Redshift. He leads a strong team in query processing, SQL language, data security, and performance. Kiran is passionate about delivering products that seamlessly integrate with customers’ business applications with the right ease of use and performance. In his spare time, he enjoys reading and playing tennis.
Huichen Liu is a software development engineer on the Amazon Redshift query processing team. She focuses on query optimization, statistics and SQL language features. In her spare time, she enjoys hiking and photography.