Best practices for migrating SQL Server MERGE statements to Babelfish for Aurora PostgreSQL
To migrate a SQL Server database to Babelfish for Aurora PostgreSQL, you usually need to perform both automated and manual tasks. The automated tasks involve automatic code conversion using the Babelfish Compass tool with the
-rewrite flag and data migration using AWS Database Migration Service (AWS DMS). The manual tasks involve database compatibility check using the Babelfish Compass tool, migration workarounds for certain database objects that are not supported by Babelfish, and manual validation of results.
In this post we focus on the Babelfish Compass tool’s
-rewrite flag feature, which automatically converts the SQL Server’s MERGE statement to Babelfish-compatible T-SQL code. A MERGE statement is one example showed in this post, but
-rewrite can also be used for other features. We also discuss best practices to manually validate the results.
Using Babelfish Compass, you can check whether your source SQL Server database is compatible with your target Babelfish database and what features aren’t currently supported in Babelfish and cannot be migrated.
PostgreSQL 15 Supports MERGE but Babelfish does not yet support it. Also, PostgreSQL MERGE is not fully equivalent to SQL Server’s MERGE , for example RETURNING and NOT MATCHED BY SOURCE clauses are not supported;
Babelfish Compass -rewrite flag overview
-rewrite flag, you can convert MERGE statements contained in the unsupported section of the Babelfish Compass assessment report.
Run the Babelfish Compass tool in the command prompt:
-rewrite flag proves to be useful when the Babelfish Compass assessment report suggests to rewrite the SQL code manually so that it can be compatible with the Babelfish target.
You can remove some of the manual effort of converting SQL into Babelfish-compatible T-SQL code. However, you must also conduct a careful review of the corresponding rewritten SQL code.
Understanding the SQL Server MERGE statement
SQL Server’s MERGE combines INSERT, UPDATE and DELETE into a single statement and transaction. The MERGE statement selects rows from the source table and performs multiple DML operations on the target table in a single transaction.
To demonstrate scenarios of SQL Server MERGE, we create the following test tables in SQL Server database and in Babelfish.
Person_Target is the target table and
Person_Source is the source, whose rows are merged into the
Person_Target table based on the merge condition:
The following INSERT statements insert sample data into the
The following code merges data from the
Person_Source table into the
Person_Target table, semicolon after MERGE is actually mandatory :
For each row in the
Person_Target table, SQL Server evaluates the search condition known as the merge condition. If the condition is matched, the result becomes true and SQL Server updates the row in the target table with the corresponding data from the
Person_Source table. If the condition isn’t matched for any rows, the result is false and SQL Server inserts the corresponding row from the source table into the target table. If the condition isn’t matched for any rows of source table then the row is deleted from target. The following diagram illustrates this workflow.
We can verify the target table and check if the data in the
Person_Target table matches the preceding diagram.
Equivalent of SQL Server MERGE in Babelfish
For PostgreSQL doesn’t have a direct MERGE-like construct. However, the Compass tool can rewrite MERGE as individual INSERT, UPDATE or DELETE statements for Babelfish
The following statements create the
Person_Target tables in Babelfish and insert the data:
The following code is automatically generated inside a folder called rewritten located inside your report folder.
One of the differences is that
@@rowcount is different compared to SQL Server. That’s why we have
@MERGE_RWRITTEN_ROWCOUNT_n in the rewritten code.
You can verify the data matches in the SQL Server and Babelfish PostgreSQL
person_target table after the code is converted for the procedure.
When the MERGE statement is dynamically constructed in a string variable, it won’t be rewritten using the Babelfish Compass tool. In such scenarios, you must convert it manually. The
-rewrite flag only affects MERGE as long as Babelfish does not support it, once the feature is supported,
-rewrite will not attempt to rewrite it anymore.
In this post we covered one example of MERGE statements used in SQL Server and explained how you can use the
-rewrite flag in Babelfish Compass tool to convert them to equivalent Babelfish T-SQL code.
About the authors
Shyam Sunder Rakhecha is a Lead Consultant with the Professional Services team at AWS based out of Hyderabad, India and specializes in database migrations and modernization. He is helping customers in migration and optimization in AWS cloud. He is curious to explore emerging technology in terms of Databases. He is fascinated with RDBMS and Big Data. He also love to organize team building events and regalement in team.