Skip to content

perf: share attribution computation across PostgreSQL dimensions - #4538

Open
dongwonmoon wants to merge 2 commits into
umami-software:devfrom
dongwonmoon:perf/attribution-shared
Open

dongwonmoon wants to merge 2 commits into
umami-software:devfrom
dongwonmoon:perf/attribution-shared

Conversation

@dongwonmoon

@dongwonmoon dongwonmoon commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Summary

The PostgreSQL attribution report repeats conversion and first/last-touch computation for each dimension, issuing eight sequential queries including totals.

This change shares that computation through CTEs and returns the seven dimension results and totals in one SQL statement. It retains the existing filters, attribution models, per-dimension grouping and top-20 limits. ClickHouse is unchanged; no cache or configuration is added.

Verification

  • All 9 focused attribution tests pass; lint passes for both changed files.
  • The official Docker build completes with the GeoIP download skipped.
  • Real Prisma-driver comparisons cover populated property filters, unequal counts and tied top-20 boundaries. Tested baseline/candidate JSON responses match exactly, including array order.
  • A disposable, migrated PostgreSQL database and the built candidate image pass GET/compat POST checks for first/last touch, nonempty paid-ad attribution, empty results and unauthenticated requests.

On fixed synthetic data, an earlier 30-day SQL prototype comparison reduced median time from 228 to 97 ms for first-click and 217 to 90 ms for last-click. These are local prototype measurements, not production results or HTTP timings of this build.

Behavior differences and limitations

The report now reads one statement snapshot instead of eight separate snapshots. If data is committed while the report is being calculated, its result can differ from the previous implementation; an interleaved-write check confirmed this distinction.

The existing rankings have no secondary tie-breaker. Selected rows and array order matched in the tested cases, but this does not guarantee identical tied selections under every execution plan. Production-scale memory usage has not been measured.


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

@vercel

vercel Bot commented Sep 14, 2026

Copy link
Copy Markdown

@dongwonmoon is attempting to deploy a commit to the Umami Software Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR consolidates PostgreSQL attribution reporting from eight sequential statements into one statement that shares the conversion and attribution-model CTEs.

  • Builds seven dimension CTEs and a totals CTE in one snapshot.
  • Constructs the complete attribution response with PostgreSQL JSON functions.
  • Updates focused tests for the single-query execution and response shape.
  • The JSON aggregation should explicitly preserve each dimension's ranked order.

Confidence Score: 4/5

The PR appears safe to merge, with a non-blocking correction recommended to guarantee ranked array ordering.

The shared CTE construction preserves the supported filters, joins, models, and totals behavior, but the new JSON aggregation leaves dimension array ordering dependent on execution order rather than expressing it in the aggregate.

Files Needing Attention: src/queries/sql/attribution/getAttribution.ts

Important Files Changed

Filename Overview
src/queries/sql/attribution/getAttribution.ts Replaces eight PostgreSQL queries with shared CTEs and one JSON-producing statement; aggregate ordering is not explicit.
src/queries/sql/attribution/getAttribution.test.ts Updates mocked PostgreSQL expectations for one raw query and the combined result object.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  P[Attribution parameters] --> E[events CTE]
  E --> M[shared model CTE]
  M --> R[referrer CTE]
  M --> A[paidAds CTE]
  M --> U[five UTM CTEs]
  P --> T[total CTE]
  R --> J[json_build_object]
  A --> J
  U --> J
  T --> J
  J --> O[AttributionResult]
Loading

Reviews (1): Last reviewed commit: "perf: share PostgreSQL attribution compu..." | Re-trigger Greptile

Comment thread src/queries/sql/attribution/getAttribution.ts Outdated
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
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