Skip to content

fix!: use stable per-query aliases for Preload and multi-hop Joins - #783

Open
jalaziz wants to merge 1 commit into
stephenafamo:mainfrom
jalaziz:fix/741-stable-query-aliases
Open

jalaziz wants to merge 1 commit into
stephenafamo:mainfrom
jalaziz:fix/741-stable-query-aliases

Conversation

@jalaziz

@jalaziz jalaziz commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Preload and generated multi-hop Joins suffixed table aliases with the process-global bob.NextUniqueInt counter, so building the same query twice produced different SQL and defeated pgx prepared-statement caching (#741).

Aliases now come from a counter on bob.Load (NextQueryAlias: bob_q0, bob_q1, ...). Preloader.Apply passes the query's allocator to the preload tree through the new orm.WithNextAlias option, and generated multi-hop joins allocate intermediate aliases when applied. Preloaders called without an allocator fall back to unique bob_g aliases.

Also fixes two Preload bugs found along the way:

  • PreloadAs on multi-side relationships aliased every side, producing duplicate aliases; it now applies only to the final side.
  • to_where columns were qualified with the parent table instead of the joined table, producing invalid or incorrect ON clauses.

BREAKING CHANGE: orm.Preloader now takes optional ...orm.PreloaderOpt after parent. Calls such as loader("") still compile, but functions declared with the old exact signature must add the variadic parameter.

Fixes #741

Preload and generated multi-hop Joins suffixed table aliases with the
process-global bob.NextUniqueInt counter, so building the same query
twice produced different SQL and defeated pgx prepared-statement caching
(stephenafamo#741).

Aliases now come from a counter on bob.Load (NextQueryAlias: bob_q0,
bob_q1, ...). Preloader.Apply passes the query's allocator to the
preload tree through the new orm.WithNextAlias option, and generated
multi-hop joins allocate intermediate aliases when applied. Preloaders
called without an allocator fall back to unique bob_g aliases.

Also fixes two Preload bugs found along the way:
- PreloadAs on multi-side relationships aliased every side, producing
  duplicate aliases; it now applies only to the final side.
- to_where columns were qualified with the parent table instead of the
  joined table, producing invalid or incorrect ON clauses.

BREAKING CHANGE: orm.Preloader now takes optional ...orm.PreloaderOpt
after parent. Calls such as loader("") still compile, but functions
declared with the old exact signature must add the variadic parameter.
@jalaziz
jalaziz force-pushed the fix/741-stable-query-aliases branch from dbf6b35 to edee13e Compare October 12, 2026 04:23
@xrn

xrn commented Oct 21, 2026

Copy link
Copy Markdown

@jalaziz hope that it is ok for you and you would not be angry - created version without introduction of breaking change - #785

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.

NextUniqueInt in Preload does not play nice with pgx's statement caching

2 participants