[Pg]: Add PostgreSQL 18 OLD/NEW returning support - #6183
Open
fabric0de wants to merge 6 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for PostgreSQL 18's
RETURNING WITH (OLD AS ..., NEW AS ...)syntax to the PostgreSQL dialect.It introduces an opt-in API for returning the previous and resulting row values from mutation queries:
The same API is supported for PostgreSQL
INSERT,UPDATE, andDELETEqueries.Behavior
The returned values follow PostgreSQL's
OLDandNEWsemantics:INSERT:oldisnull, andnewcontains the inserted row.UPDATE:oldcontains the row before the update, andnewcontains the updated row.DELETE:oldcontains the deleted row, andnewisnull.INSERT ... ON CONFLICT DO UPDATE: the values reflect the outcome of each returned row.Either value can be requested independently:
The existing
.returning()and.returning({ id: users.id })APIs remain unchanged.Implementation
The PostgreSQL dialect emits aliased
OLDandNEWrows using:The result mapper reconstructs the nested
oldandnewobjects and preservesnullwhen a row version does not exist.OLD/NEW projections cannot be mixed with regular returning fields. Invalid mixed projections are rejected at both the type and runtime levels.
This initial implementation intentionally supports whole-row
oldandnewprojections only. PartialOLD/NEWprojections and other advanced projection forms are outside the scope of this PR.Tests
The implementation includes:
INSERT,UPDATE, andDELETEINSERT ... ON CONFLICT DO UPDATEcoverageRelated to #5109