fix(typescript): make grouped function sort fully deterministic#1071
Open
selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
Open
fix(typescript): make grouped function sort fully deterministic#1071selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
selenaalpha77-sketch wants to merge 1 commit intosupabase:masterfrom
Conversation
When multiple overloaded functions share the same name, argument_types, and return_type, the sort order was not guaranteed to be stable across different database states or dump/restore cycles. Add function id as a final tiebreaker so that SetofOptions generation is always deterministic. Fixes supabase#1008
avallete
requested changes
Apr 27, 2026
Member
avallete
left a comment
There was a problem hiding this comment.
I don't think we want to rely over the id as it'll have the same issue, depending on the order this get inserted into the database the oid will change, causing unstable results.
Maybe instead we could rely over the whole definition text as the tiebreaker ? That way, function with the same name, same arguments, same return type AND exact same body definition will always be sorted the same way. Regardless of their insertion order in the database ?
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.
Problem
When generating TypeScript types, functions with the same name are grouped together and sorted by
argument_typesandreturn_typeto produce stable output. However, if two overloaded functions have identicalargument_typesandreturn_typestrings (for example, embedded join functionsB→A,C→A,D→Athat all share the same return table), the sort order is non-deterministic and can change acrosssupabase db reset/ dump / restore cycles.This causes
SetofOptionsin the generated types to be unstable — the same database schema can generate different TypeScript output depending on the internal PostgreSQL OID ordering.Closes #1008
Fix
Add
a.fn.id - b.fn.idas a final tiebreaker in the sort comparator. Functionidis the PostgreSQL OID (a stable integer per function), so this guarantees a fully deterministic ordering regardless of how functions are sequenced in the catalog after a dump/restore.Changes
src/server/templates/typescript.ts: Added|| a.fn.id - b.fn.idas tiebreaker in the grouped function sortTesting
To reproduce the original bug:
fn_b_to_a(b),fn_c_to_a(c),fn_d_to_a(d)— all returningsetof aSetofOptionsin the outputsupabase db reset→ restore → typegen again🤖 Generated with Claude Code