← All posts
·8 min read·GuardLayer

Supabase: Could not find the function in the schema cache

SupabasePostgRESTRPCSECURITY DEFINERpgvector

Could not find the function public.X(a, b) in the schema cache (PGRST202, HTTP 404) means PostgREST has no function in that schema whose name and argument names match what you sent. Compare the names in the error with the function's parameter names, check the project and schema, then run notify pgrst, 'reload schema';. If you have to drop and recreate the function to change its parameters, put its revoke/grant lines back in the same migration, and don't make it security definer to get more rows back.

The error comes from PostgREST, the server behind supabase.rpc(). It isn't about permissions. It means the call never matched a function, so nothing ran.

What does "Could not find the function in the schema cache" mean?

PostgREST keeps a cached map of your database and resolves every RPC call against it. In its Error.hs the message is built from three parts: the schema, the function name, and the argument names your request carried, in the form Could not find the function <schema>.<name>(<arg names>) in the schema cache. The code is PGRST202 and the status is 404. Supabase's error code reference sums it up as "Caused by a stale function signature, otherwise the function may not exist in the database."

const { data, error } = await supabase.rpc("get_stats", { userId: user.id });
// error.code    === "PGRST202"
// error.message === "Could not find the function public.get_stats(userId) in the schema cache"
// error.hint    === "Perhaps you meant to call the function public.get_stats(user_id)"

The part in parentheses is the most useful thing in the error: it lists the keys you sent, not the parameters the function has. Read the details and hint fields too. When a function with that name exists but no parameter set matches, PostgREST fuzzy-matches the overloads and suggests one (Perhaps you meant to call the function public.get_stats(user_id)). When the name itself is close to another function in the schema, the hint names that function instead.

Two variants of the message:

  • ... without parameters in the schema cache means your request carried no arguments at all, while every function with that name needs at least one. Either pass the arguments, give them defaults, or check that your client really sends a body. A supabase-js issue reports zero-argument RPC calls failing this way from React Native.
  • No parameter list at all appears for a single unnamed text, xml or bytea parameter called with a matching Content-Type.

One split that saves time: reading PostgREST's schema-cache query, the function list isn't filtered by privileges. A function that exists but that your role can't execute comes back as 42501 (permission denied for function ...), not as PGRST202. A 404 is never fixed with a GRANT.

The causes, in the order to check them

1. The argument names don't match. PostgREST matches functions by the names of the JSON keys, so { userId } won't reach a parameter called user_id, and an extra key nobody declared also breaks the match. Optional parameters only work if the function gives them a default. Rename the keys in the call, or the parameters in the function, so the list in the error matches the function exactly.

2. You're talking to a different database. The function exists locally or in another project, and the app points somewhere else. Compare the project ref in NEXT_PUBLIC_SUPABASE_URL with the dashboard you're looking at, and remember that a local migration isn't on the hosted project until you supabase db push it.

3. The function is in another schema. An unqualified call resolves against the default schema only, usually public. For api.get_stats, call supabase.schema("api").rpc("get_stats", ...), and make sure the schema is in the exposed list. The PGRST205 post covers the same schema rules for tables.

4. The cache is stale. You created or changed the function after PostgREST last loaded its map. Run:

notify pgrst, 'reload schema';

There is no separate "clear cache" button; the reload rebuilds the map.

5. It isn't something PostgREST can call. The cache only holds plain functions: a create procedure is never listed, and neither is a function that returns trigger. Parameters without names can't be matched by name either. Use create function with named parameters.

6. Two overloads fit. When more than one function matches, the error changes to Could not choose the best candidate function between: ... (PGRST203). Drop the overload you don't use; the hint asks you to rename parameters or the function so overloading can be resolved.

PGRST202 on match_documents

The most common form in the wild is Could not find the function public.match_documents(...) in the schema cache. It comes from vector-store integrations (LangChain, n8n and similar) that call a match_documents RPC with their own argument names, often query_embedding, match_count and filter. A function created from an older snippet usually has a different set, for example no filter, and the call stops matching.

The fix is the same as cause 1: read the argument list in the error and give the function exactly those names, with defaults for the ones that are optional:

create or replace function public.match_documents(
  query_embedding vector(1536),
  match_count int default 5,
  filter jsonb default '{}'
)
returns table (id bigint, content text, metadata jsonb, similarity float)
language sql stable
set search_path = public
as $$
  select d.id, d.content, d.metadata, 1 - (d.embedding <=> query_embedding)
  from public.documents d
  where d.metadata @> filter
  order by d.embedding <=> query_embedding
  limit match_count;
$$;

Note what isn't in it: security definer. As an ordinary (invoker) function it runs as the caller, so the documents RLS policies decide which rows each user's search can return.

The fix that turns a 404 into a leak

When you try to rename a parameter with create or replace, Postgres refuses with cannot change name of input parameter "..." (or cannot change return type of existing function), and its hint says to use DROP FUNCTION ... first. So people drop and recreate, like the sample at the top of this page. Two things go wrong on the way.

The grants reset. Replacing a function keeps its permissions. Dropping it doesn't: the new function starts with the defaults, which on Supabase include EXECUTE for anon and authenticated. Any revoke you ran on the old version is gone, and nothing in the error tells you. Put the revoke/grant lines in the same migration as the new create function. Revoking from anon alone isn't enough; the SECURITY DEFINER post explains why you also need public.

security definer skips RLS. Once the call matches, a search over documents with per-user RLS returns only the caller's rows, sometimes none. That looks like another bug, and the quick way to "fix" it is security definer. The function then runs as its owner, RLS no longer applies, and every caller who knows the RPC name, logged in or not, can search everyone's documents. Keep the function as an invoker and fix the policies instead; if results are empty, start with why a select returns an empty array.

If the function truly needs to be a definer, filter by auth.uid() inside it, pin search_path (the search_path post has the details), and revoke it from public and anon.

What GuardLayer catches

The sample at the top of this page fires supabase/security-definer-no-caller-check (warning) on the create function line. The finding says that anyone with the anon key can call match_documents(), that it returns rows from documents without applying the policies that limit each user to their own rows, and that it never checks who is calling. The check reads your migrations together, so it sees the table's RLS policies, the drop and the missing revoke.

It doesn't flag an invoker function that lost its revoke in a drop-and-recreate, because RLS still applies to it. It doesn't see your PostgREST cache, your client's argument names, or which project your app points at, since it reads the repo.

Quick self-check

Run this in the SQL Editor to see what PostgREST should be finding:

select p.oid::regprocedure as signature,
       p.prokind,           -- 'f' = function; 'p' = procedure (not callable)
       p.prosecdef,         -- true = SECURITY DEFINER
       p.proacl             -- who may execute it
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.proname = 'match_documents';

No row means the function isn't in this database (cause 2). A row in another schema is cause 3. A signature whose argument names differ from the ones in the error is cause 1. prosecdef = true on a search function over per-user data is the leak above. And a proacl that still lists anon=X after you "revoked" it means the revoke didn't survive.

grep -rniE "drop function" supabase/migrations/
grep -rniE "security definer" supabase/migrations/ | grep -i match_documents

To see your project the way the API sees it, GuardLayer's free Live check connects read-only and flags SECURITY DEFINER functions that anyone with your publishable key can call.

FAQ

What is PGRST202 in Supabase? It's PostgREST's "function not found" error, returned with HTTP 404 when no function in the schema matches the name and argument names of the RPC call.

Why does the error list parameters my function doesn't have? Because the list is what your request sent. Compare it with the function's real parameter names: that difference is usually the whole bug.

What does "without parameters" mean? Your call carried no arguments, and every function with that name requires some. Pass them or give them defaults.

How do I reload the schema cache? Run notify pgrst, 'reload schema'; in the SQL Editor.

Is PGRST202 a permissions problem? No. A function that exists but can't be executed by your role returns 42501 permission denied. Don't answer a 404 with grant execute ... to anon.

Should match_documents be SECURITY DEFINER? No. As a definer it bypasses the RLS on your documents table, so every caller can search everyone's rows. Keep it as an invoker and let the policies decide.

See which of your Supabase tables anyone can read, free

Connect your Supabase project read-only. GuardLayer checks RLS, policies, grants and functions as they are right now, and gives you the SQL to fix each problem. It never reads your rows and can't change anything.

Check my Supabase project

Prefer to check code instead? Scan your repo, no signup.

Keep reading