Feature/generate return row by returning in postgresql#57
Merged
Conversation
… of PK re-select BREAKING CHANGE: PostgreSQL INSERT statements now use `RETURNING` with all columns and return full row values directly, rather than performing a second SELECT by primary key. This affects result structures for single and bulk inserts, including on conflict operations. Code generation template updated accordingly.
Previously, UPDATE methods for PostgreSQL selected updated rows using a separate SELECT by PK after executing the update statement. This change updates the templates and generated code to use the RETURNING clause in PostgreSQL UPDATE statements, allowing the updated rows to be fetched directly within the update query. This approach improves performance and accuracy by returning the actual modified records without requiring an additional SELECT. Test cases have been adjusted accordingly.
…turning for INSERT BREAKING CHANGE: The ToSql method for INSERT queries in the PostgreSQL dialect no longer automatically appends the RETURNING clause. A new ToSqlWithReturning method has been introduced which appends the RETURNING clause as before. Users who require the RETURNING clause should use ToSqlWithReturning, or explicitly add it themselves when using ToSql. This provides more control over the generated SQL and aligns with user responsibility for query output.
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 pull request updates the PostgreSQL dialect of the query builder to improve how rows are retrieved after
INSERTandUPDATEoperations. Previously, INSERT and UPDATE queries would use theRETURNINGclause to get the primary key(s), then issue a secondarySELECTstatement to fetch the entire row from the database. With these changes, all columns are now fetched directly usingRETURNING *(or the necessary column list) in the INSERT/UPDATE statement itself, eliminating the need for a second query.Motivation
The previous implementation introduced unnecessary overhead by always performing a secondary fetch to retrieve full row data after modifications. By leveraging PostgreSQL's
RETURNINGclause to obtain all columns in a single query, we reduce round-trips to the database and ensure that the returned row reflects the actual state after the write operation. This also avoids potential race conditions where data might change between the write and the follow-up select.Details
RETURNINGonly for primary keys and selecting the full row with another query, the code now usesRETURNINGto fetch all columns.RETURNINGfor all queried columns, removing the need for a select-after-update.ToSqlmethod for INSERT no longer automatically appends aRETURNINGclause.ToSqlWithReturningmethod is introduced for cases where aRETURNINGclause is needed (backward compatible with old behavior).