Skip to content

functions.retrieve with args: [] returns the wrong overload for zero-argument functions #1111

Description

@hsusul

Describe the bug

PostgresMeta.functions.retrieve({ schema, name, args: [] }) is supposed to select the zero-argument overload of a function. When the name is overloaded, the empty args array currently skips the proargtypes filter, so the query returns an arbitrary overload (data[0]).

functions.create() calls retrieve() after creating the function, so creating a zero-argument overload of an existing name can also return the wrong function.

To Reproduce

Using the fixtures in test/db/00-init.sql:

create or replace function public.polymorphic_function_with_no_params_or_unnamed() returns int language sql as 'SELECT 1';
create or replace function public.polymorphic_function_with_no_params_or_unnamed(bool) returns int language sql as 'SELECT 2';
create or replace function public.polymorphic_function_with_no_params_or_unnamed(text) returns text language sql as $$ SELECT 'foo' $$;
await pgMeta.functions.retrieve({
  schema: 'public',
  name: 'polymorphic_function_with_no_params_or_unnamed',
  args: [],
})

Expected behavior

The zero-argument overload is returned (argument_types === '', return_type === 'integer').

If no zero-argument overload exists, retrieve should return the existing "cannot find function" error.

Actual behavior

A different overload is returned. On the test fixtures this is the text overload (argument_types === 'text').

Root cause

In FUNCTIONS_SQL, an empty args array takes the args.length > 0 false branch and emits no p.proargtypes predicate. The inner "''" comparison for the empty-oidvector case is therefore dead code. retrieve() always passes args (default []), so name-based lookup never filters zero-argument functions.

Additional context

Existing tests only cover args: [] for a uniquely named zero-argument function (function_returning_set_of_rows), which hides the bug.

  • Library: @supabase/postgres-meta
  • Branch analyzed: master (641831e)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions