Skip to content

refactor: narrow implicit from_many subquery selections - #3

Closed
jechol wants to merge 1 commit into
mainfrom
perf/prune-relationship-projections
Closed

jechol wants to merge 1 commit into
mainfrom
perf/prune-relationship-projections

Conversation

@jechol

@jechol jechol commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Reduce implicit selections in from_many? relationship joins to the columns referenced by the completed query.

PostgreSQL already recognizes unused columns, and measurements did not show a clear overall performance improvement. The main motivation is to make generated SQL less verbose and easier to inspect while debugging.

A public reproduction compares pinned commits using synthetic Invoice, Product and Payment resources. The query reads Invoice numbers, Product names and the latest Payment's status and paid_at. Each Payment has 20 ordinary billing fields; generated SQL shrinks from 1,532 to 906 bytes. Full before/after SQL are included, and ./run.sh reproduces the comparison with local PostgreSQL.

Other SQL backends, including ones supported in the future, may also benefit if their optimizers do less projection pruning. That potential benefit has not been measured. This change adds application-side reference tracking and is not presented as a runtime performance improvement.

Preserve explicit selections, whole-row references, opaque SQL fragments and set-operation semantics. Pruning runs at execution time so later calculations, filters and sorts can still reuse a join. Companion: jechol/ash_postgres#2.

Validation:

  • The public reproduction checks matching rows and bind parameters, including missing Payments and NULL paid_at. Both versions use the same database, generated migration and seed data.
  • Focused ash_sql, AshPostgres and downstream application tests pass; bypassing the optimizer makes the projection regression tests fail.
  • Invoice, quotation and contract outputs match. SQL comparisons for single records and batches return identical rows and columns.

@jechol
jechol force-pushed the perf/prune-relationship-projections branch from ac98c66 to 7c76ce8 Compare November 7, 2026 07:45
@jechol jechol changed the title perf: narrow implicit from_many subquery selections refactor: narrow implicit from_many subquery selections Nov 9, 2026
@jechol jechol closed this Nov 9, 2026
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