icon Join with us for Oracle DBA Training Live Session ENROLL NOW

Postgres 18: OLD and NEW Rows in the RETURNING Clause

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
  • 23 Oct, 2026
  • 0 Comments
  • 7 Mins Read

Postgres 18: OLD and NEW Rows in the RETURNING Clause

OLD and NEW Rows in the RETURNING Clause

Introduction

When working with a PostgreSQL database, modifying data is a common task. Developers frequently use INSERT, UPDATE, and DELETE statements to manage records. However, after modifying a row, it is often necessary to understand what changed.

For example, imagine an e-commerce application where a product’s price changes from ₹1,000 to ₹1,200. The application may need to know the previous price, the updated price, and the exact difference. Traditionally, developers had to execute additional queries or implement custom logic to capture these values.

PostgreSQL 18 introduces a more convenient way to handle this requirement. The enhanced RETURNING clause allows developers to explicitly access both the OLD and NEW versions of a row.

This feature makes data modifications more transparent and simplifies applications that require auditing, change tracking, notifications, or business calculations.

In this blog, we will explore how the PostgreSQL 18 OLD and NEW rows feature works, how to use it with different SQL statements, and where it provides practical benefits.

What Is the RETURNING Clause in PostgreSQL?

The RETURNING clause allows PostgreSQL to return data directly from a row affected by a data modification statement.

It is supported by:

  • INSERT
  • UPDATE
  • DELETE
  • MERGE

Instead of executing a separate SELECT statement after modifying data, developers can retrieve the required values in the same SQL command.

Example: Using RETURNING Before PostgreSQL 18

Consider the following table:

CREATE TABLE employees (
    employee_id SERIAL PRIMARY KEY,
    employee_name VARCHAR(100),
    salary NUMERIC(10,2)
);

Insert a record:

INSERT INTO employees (employee_name, salary)
VALUES ('Rahul', 50000)
RETURNING employee_id, employee_name, salary;

PostgreSQL returns the newly inserted employee details, including the automatically generated employee ID.

Now, update the employee’s salary:

UPDATE employees
SET salary = 60000
WHERE employee_id = 1
RETURNING employee_id, employee_name, salary;

The result contains the updated salary of ₹60,000.

However, the traditional RETURNING clause does not directly provide both the previous and updated salary in the same result.

This is where PostgreSQL 18 introduces an important improvement.

What Is New in PostgreSQL 18?

PostgreSQL 18 allows users to explicitly reference the old and new versions of modified rows using:

  • OLD – Represents the row before the modification.
  • NEW – Represents the row after the modification.

The syntax is straightforward:

RETURNING
    OLD.column_name,
    NEW.column_name;

This makes it possible to compare values before and after a database operation without requiring an additional query.

For example:

UPDATE employees
SET salary = 65000
WHERE employee_id = 1
RETURNING
    OLD.salary AS old_salary,
    NEW.salary AS new_salary;

The output will look like this:

old_salary new_salary
60000.00 65000.00

The previous salary and updated salary are now available together.

This feature is particularly useful for database auditing, reporting, and application development.

Important: PostgreSQL 18 does not introduce the RETURNING clause itself. It extends the clause with explicit access to old and new row values. The syntax is documented in the official PostgreSQL 18 documentation.

Understanding OLD and NEW with Different SQL Statements

Let’s explore how the feature works with different data modification operations.

1. Using OLD and NEW with UPDATE

An UPDATE statement changes existing records. With PostgreSQL 18, developers can retrieve both the original and modified values.

Consider an employee salary update:

UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = 1
RETURNING
    OLD.employee_name,
    OLD.salary AS previous_salary,
    NEW.salary AS updated_salary,
    NEW.salary - OLD.salary AS salary_difference;

Suppose the employee’s previous salary was ₹60,000.

The result will be:

employee_name previous_salary updated_salary salary_difference
Rahul 60000.00 66000.00 6000.00

This example demonstrates how PostgreSQL can calculate the difference between the previous and updated values directly.

Practical use case: HR applications can use this feature to maintain salary change records, generate notifications, and track employee compensation adjustments.

2. Using OLD and NEW with INSERT

When inserting a new record, the NEW row represents the inserted data.

For example:

INSERT INTO employees (employee_name, salary)
VALUES ('Priya', 45000)
RETURNING
    OLD.employee_id AS old_employee_id,
    NEW.employee_id AS new_employee_id,
    NEW.employee_name,
    NEW.salary;

For a normal insert, the old row values are typically NULL, while the new row contains the inserted values.

The result may look like this:

old_employee_id new_employee_id employee_name salary
NULL 2 Priya 45000.00

This is useful when PostgreSQL generates values automatically, such as identity columns, default timestamps, or other database-generated values.

3. Using OLD and NEW with DELETE

A DELETE statement removes an existing record.

Normally, the deleted row is no longer available through a standard query. However, the RETURNING clause can retrieve its values during the deletion.

With PostgreSQL 18, developers can explicitly access the old row.

DELETE FROM employees
WHERE employee_id = 2
RETURNING
    OLD.employee_id,
    OLD.employee_name,
    OLD.salary;

The output contains the deleted employee’s details.

For a normal delete operation, the NEW row values are typically NULL.

Practical use case: Applications can record deleted customer accounts, removed inventory items, or discontinued products for auditing purposes.

4. Using OLD and NEW with INSERT ON CONFLICT

One of the most useful applications of this feature is handling conflicting records.

Consider an inventory table:

CREATE TABLE products (
    product_id INT PRIMARY KEY,
    product_name VARCHAR(100),
    price NUMERIC(10,2)
);

Insert an initial record:

INSERT INTO products
VALUES (101, 'Laptop', 50000);

Now, suppose the product price changes during another insert operation.

INSERT INTO products
VALUES (101, 'Laptop', 55000)
ON CONFLICT (product_id)
DO UPDATE
SET price = EXCLUDED.price
RETURNING
    OLD.price AS previous_price,
    NEW.price AS updated_price;

The result will be:

previous_price updated_price
50000.00 55000.00

This example demonstrates how PostgreSQL can capture both values when an insert encounters a conflict and performs an update instead.

It is especially useful for applications that synchronize data from external systems.

Real-World Use Cases of PostgreSQL 18 OLD and NEW Rows

The feature provides practical benefits across multiple application scenarios.

Database Auditing

Organizations often need to track changes to important records.

For example, when a customer’s address changes, an application may need to record the previous address and the new address.

Using OLD and NEW, developers can retrieve both values directly from the modification statement.

Financial Applications

Banking and financial applications frequently update account balances.

Capturing the old and new balances helps applications understand the effect of a transaction.

However, this feature does not replace transaction controls, access restrictions, or a properly designed audit system.

Inventory Management

Inventory systems need to track stock movements.

For example:

  • Previous stock quantity: 100
  • Updated stock quantity: 75
  • Quantity reduced: 25

The RETURNING clause can calculate the difference immediately after the update.

Data Synchronization

Applications that synchronize records between databases or external services can use old and new values to identify changes.

This can reduce the amount of additional SQL logic required to determine which fields were modified.

Common Mistakes to Avoid

Although the feature is straightforward, developers should understand a few important points.

1. Assuming OLD and NEW Are Always Available

The values depend on the operation being performed.

SQL operation OLD values NEW values
INSERT Typically NULL Inserted row
UPDATE Previous row Updated row
DELETE Deleted row Typically NULL
MERGE Depends on the action Depends on the action

For MERGE, the returned values depend on whether the operation inserts, updates, or deletes a row.

2. Confusing EXCLUDED with NEW

In an INSERT ... ON CONFLICT DO UPDATE statement, EXCLUDED represents the proposed row that was not inserted because of the conflict.

NEW represents the resulting row after the operation.

These values can differ if the update applies additional expressions or transformations.

3. Ignoring Triggers

Triggers can modify data during an operation.

PostgreSQL’s RETURNING output reflects the row as modified by triggers, so developers should consider trigger behavior when interpreting old and new values.

4. Using SELECT Instead of RETURNING Unnecessarily

Executing an additional SELECT statement just to retrieve modified values can introduce unnecessary complexity.

Whenever possible, use RETURNING to retrieve the required data directly.

Best Practices for Using OLD and NEW in PostgreSQL 18

To use this feature effectively, follow these recommendations:

  • Use meaningful aliases: Names such as old_salary and new_salary make results easier to understand.
  • Return only required columns: Avoid returning unnecessary data, particularly in large tables.
  • Test conflict scenarios: Verify the behavior of INSERT ... ON CONFLICT DO UPDATE carefully.
  • Consider triggers: Ensure that the returned values match the application’s expectations.
  • Use transactions where necessary: For complex operations, maintain data consistency using appropriate transaction controls.
  • Validate your PostgreSQL version: Ensure that the database is running PostgreSQL 18 or a compatible version before using the new syntax.

Conclusion

PostgreSQL 18 introduces a valuable enhancement to the RETURNING clause by allowing developers to explicitly access both OLD and NEW row values.

This improvement simplifies data modification workflows and makes it easier to understand exactly what changed during an operation.

Whether you are developing an employee management system, maintaining inventory, building financial applications, or synchronizing data between systems, the feature provides a convenient way to retrieve previous and updated values.

By reducing the need for additional queries and simplifying SQL logic, PostgreSQL 18 makes data modification operations more expressive and easier to maintain.

Key Takeaways

  • PostgreSQL 18 supports explicit OLD and NEW references in the RETURNING clause.
  • OLD represents the previous row, while NEW represents the resulting row.
  • The feature works with INSERT, UPDATE, DELETE, and MERGE.
  • It is especially useful for auditing, inventory tracking, financial applications, and data synchronization.
  • Developers should understand trigger behavior, conflict handling, and operation-specific NULL values.

Explore more PostgreSQL concepts, practical database tutorials, and expert-led training with Learnomate Technologies.

lets talk - learnomate helpdesk

Book a Free Demo

lets talk - learnomate helpdesk

Book a Free Demo