Skip to content

Feature/generate return row by returning in postgresql#57

Merged
mackee merged 3 commits intomasterfrom
feature/generate-return-row-by-returning-in-postgresql
Jun 19, 2025
Merged

Feature/generate return row by returning in postgresql#57
mackee merged 3 commits intomasterfrom
feature/generate-return-row-by-returning-in-postgresql

Conversation

@mackee
Copy link
Copy Markdown
Owner

@mackee mackee commented Jun 19, 2025

Summary

This pull request updates the PostgreSQL dialect of the query builder to improve how rows are retrieved after INSERT and UPDATE operations. Previously, INSERT and UPDATE queries would use the RETURNING clause to get the primary key(s), then issue a secondary SELECT statement to fetch the entire row from the database. With these changes, all columns are now fetched directly using RETURNING * (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 RETURNING clause 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

  • INSERT queries:
    • Instead of using RETURNING only for primary keys and selecting the full row with another query, the code now uses RETURNING to fetch all columns.
    • Bulk insert and insert-on-conflict code paths are also updated to use these semantics.
    • APIs and generated code now return full row data directly.
  • UPDATE queries:
    • UPDATE operations now return the modified rows using RETURNING for all queried columns, removing the need for a select-after-update.
  • API changes (breaking):
    • The existing ToSql method for INSERT no longer automatically appends a RETURNING clause.
    • A new ToSqlWithReturning method is introduced for cases where a RETURNING clause is needed (backward compatible with old behavior).
    • Test cases updated to reflect the new result structure for INSERT/UPDATE statements.

mackee added 3 commits June 19, 2025 14:47
… 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.
@mackee mackee merged commit 79d54f7 into master Jun 19, 2025
1 check passed
@mackee mackee deleted the feature/generate-return-row-by-returning-in-postgresql branch June 19, 2025 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant