PostgreSQL 18 introduces enhanced RETURNING clause functionality that allows developers to access both old and new row values in a single DML operation. This eliminates the need for separate queries or complex triggers when tracking data changes. The feature introduces special 'old' and 'new' aliases that work across INSERT, UPDATE, DELETE, and MERGE operations, with customizable alias names to avoid conflicts. Key benefits include simplified audit trail implementation, better change tracking capabilities, and reduced code complexity. The enhancement is particularly valuable for INSERT...ON CONFLICT operations, UPDATE statements requiring before/after comparisons, and comprehensive DELETE logging.

2 Comments

Sort: