Skip to content

Type generation incorrectly treats PostgreSQL ROWS 1 RPC functions as returning a single object instead of an array #46525

Closed
supabase/postgres-meta
#1096

Description

@IdrisCelik

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

When generating TypeScript types for PostgreSQL RPC functions, functions declared with ROWS 1 are incorrectly typed as returning a single object instead of an array of objects.

In PostgreSQL, ROWS 1 is only a planner hint that indicates the expected number of returned rows. It does not enforce a maximum row count, nor does it change the shape of the result returned by PostgREST. PostgREST still returns an array for set-returning functions regardless of the ROWS estimate.

However, the generated Supabase types appear to interpret ROWS 1 as meaning the function returns a single object, causing the generated RPC return type to omit the trailing [].

This leads to a mismatch between the generated types and the actual runtime response shape.

Additionally, calling .single() on such an RPC produces confusing typings. The type generation already incorrectly collapses the array due to ROWS 1, and .single() applies another array-to-object transformation. While the final inferred type may appear correct, it is derived through incorrect assumptions and does not accurately model the underlying API behavior.

To Reproduce

Create a PostgreSQL function that returns a set and specifies ROWS 1:

create function test_function()
returns table (
  id int,
  name text
)
rows 1
language sql
as $$
  select 1 as id, 'test' as name
$$;

Generate Supabase types.

Observe the generated RPC type:

test_function: {
  Args: Record<PropertyKey, never>
  Returns: {
    id: number
    name: string
  }
}

Use the RPC:

const { data } = await supabase.rpc('test_function')

At runtime, PostgREST returns:

[
  {
    id: 1,
    name: 'test'
  }
]

but the generated type indicates:

{
  id: number
  name: string
}

Expected behavior

Functions declared with ROWS 1 should still be generated as returning an array type when they are set-returning functions.

Expected generated type:

test_function: {
  Args: Record<PropertyKey, never>
  Returns: {
    id: number
    name: string
  }[]
}

Type generation should reflect the actual response shape returned by PostgREST rather than the PostgreSQL row estimate.

Additionally, .single() should be the mechanism that narrows an array response to a single object type, rather than ROWS 1 influencing the generated return type.

Screenshots

N/A

System information

  • OS: Any
  • Browser (if applicable): Any
  • Version of supabase-js: Latest
  • Version of Node.js: Any

Additional context

The issue appears to stem from type generation interpreting PostgreSQL's ROWS 1 metadata as a guarantee of a single-row result. PostgreSQL documentation describes ROWS as an estimate used by the query planner, not a constraint on the number of rows returned.

Because PostgREST continues to serialize set-returning function results as arrays, the generated types become inconsistent with runtime behavior and can cause TypeScript errors or incorrect assumptions in application code.

Activity

  1. added
    bug
    on May 30, 2026
  2. added
    external-issue
    on May 30, 2026
  3. vaguul commented on Jun 1, 2026

    @vaguul

    Not a maintainer, but this diagnosis makes sense. The important distinction for the type generator is probably:

    • pg_proc.proretset / the function return type tells whether the function is set-returning
    • pg_proc.prorows is only the planner row estimate from ROWS n

    So ROWS 1 should not be used to collapse the generated return type. A function can be RETURNS TABLE (...) ROWS 1 and still return zero, one, or multiple rows at runtime, with PostgREST serializing it as an array.

    A regression test that seems worth adding would compare these two cases:

    create function returns_one_object()
    returns jsonb
    language sql
    as $$ select '{"id":1}'::jsonb $$;
    
    create function returns_table_with_rows_1()
    returns table (id int, name text)
    rows 1
    language sql
    as $$ select 1, 'test' $$;

    The first should generate an object return type, while the second should still generate an array return type. That would protect against treating ROWS 1 as a shape guarantee.

  4. added
    database
    pr-opened
    and removed on Jun 3, 2026
  5. peter941221 commented on Jun 15, 2026

    @peter941221
    Contributor

    I opened postgres-meta#1077 for this, but after tracing the generated type into supabase-js I do not think postgres-meta is the bug.

    The generated Database type for function_using_setof_rows_one already keeps Returns as a single row and sets SetofOptions to isOneToOne: true and isSetofReturn: true:
    https://github.com/supabase/supabase-js/blob/master/packages/core/postgrest-js/test/types.generated.ts#L754-L765

    rpc.ts then turns that combination into an array for direct RPC calls:
    https://github.com/supabase/supabase-js/blob/master/packages/core/postgrest-js/src/types/common/rpc.ts#L126-L131

    That lines up with the runtime tests:
    direct rpc returns an array
    https://github.com/supabase/supabase-js/blob/a25062e9285fa8e3bd702c59ddda0d87ad1fcc27/packages/core/postgrest-js/test/advanced_rpc.test.ts#L1230-L1258

    embedded function joins return an object
    https://github.com/supabase/supabase-js/blob/a25062e9285fa8e3bd702c59ddda0d87ad1fcc27/packages/core/postgrest-js/test/embeded_functions_join.test.ts#L382-L428

    So my postgres-meta patch changed the wrong contract by turning Returns into Row[] and flipping isOneToOne to false. I closed that PR.

  6. 7ashraf commented on Jun 29, 2026

    @7ashraf

    Hello, I would love to give this a shot!

  7. added and removed
    pr-opened
    on Jul 29, 2026
  8. mandarini commented on Aug 10, 2026

    @mandarini
    Contributor

    Fixed in postgres-meta#1096, released in postgres-meta v0.96.7 (latest is v0.96.8), and included in Supabase CLI v2.113.0 (current stable).

    Thanks @vaguul for the precise root-cause diagnosis (prorows/ROWS n vs. proretset), and @peter941221 for catching that the first attempt at this (postgres-meta#1077) changed the wrong contract. It flipped Returns to an array for all ROWS 1 functions, which broke the embedded-join case where postgrest-js correctly narrows to an object via isOneToOne.

    The actual fix gates the array-collapse on the same condition that emits SetofOptions: named-relation functions (table/view/composite return types) are unchanged, since postgrest-js already restores the array for direct RPC calls via that metadata. Ad-hoc RETURNS TABLE (...) and scalar SETOF functions — which had no such metadata — now keep the array type, matching what PostgREST actually returns.

    If you regenerate types: this will flip Returns from object to array for affected ROWS 1 functions. Any resulting type errors point at a real runtime mismatch. PostgREST has always returned an array here — so update call sites to handle the array (or use .single() where you actually expect one row).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdatabaseexternal-issueto-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions