5 min read
|
Saved February 14, 2026
|
Copied!
Do you care about this?
PostgreSQL 18 introduces significant improvements to the RETURNING clause, particularly with the addition of OLD and NEW aliases. This allows developers to easily access both previous and current data states during DML operations, streamlining data tracking and simplifying application logic.
If you do, here's more
PostgreSQL 18 introduces significant enhancements to the RETURNING clause, particularly with the addition of OLD and NEW aliases. These changes simplify data manipulation and tracking during DML operations like INSERT, UPDATE, DELETE, and MERGE. Prior to this update, the RETURNING clause had limitations; for example, INSERT and UPDATE could only return current values, while DELETE returned only old values. This often required developers to run separate queries or implement complex triggers to track changes.
The new OLD and NEW aliases allow users to access both previous and current states of data within a single statement. For instance, in an UPDATE operation, you can now retrieve both the old value and the new value for any column. This capability is particularly useful in MERGE statements, which combine the logic of INSERT and UPDATE. The article provides a practical example using a product inventory system, demonstrating how to merge data from a staging table and track changes in product details through the RETURNING clause.
By leveraging these enhancements, developers can build comprehensive audit trails without the need for triggers. The article illustrates how to create an audit table and perform a MERGE operation that captures old and new values for each product, as well as the specific changes made. This streamlined approach not only improves application performance but also enhances data integrity and traceability, making it easier to manage complex data operations.
Questions about this article
No questions yet.