Description
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
vaguul commentedon Jun 1, 2026
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-returningpg_proc.prorowsis only the planner row estimate fromROWS nSo
ROWS 1should not be used to collapse the generated return type. A function can beRETURNS TABLE (...) ROWS 1and 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:
The first should generate an object return type, while the second should still generate an array return type. That would protect against treating
ROWS 1as a shape guarantee.peter941221 commentedon Jun 15, 2026
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.
7ashraf commentedon Jun 29, 2026
Hello, I would love to give this a shot!
mandarini commentedon Aug 10, 2026
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 nvs.proretset), and @peter941221 for catching that the first attempt at this (postgres-meta#1077) changed the wrong contract. It flippedReturnsto an array for allROWS 1functions, which broke the embedded-join case where postgrest-js correctly narrows to an object viaisOneToOne.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-hocRETURNS TABLE (...)and scalarSETOFfunctions — which had no such metadata — now keep the array type, matching what PostgREST actually returns.If you regenerate types: this will flip
Returnsfrom object to array for affectedROWS 1functions. 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).