Fixing 520 Errors in the Database REST API

Last edited: 1/17/2025

In the context of the database API, Cloudflare 520 errors most often occur when 16+KB worth of data is present in the headers/URL of your requests.

The API will include filters within the URL, so a request like so:


_10
let { data: countries, error } = await supabase.from('countries').select('name')

translates to a URL like:


_10
https://<project ref>.supabase.co/rest/v1/countries?select=name

However, appending too much data to the URL can exceed the 16KB limitation, triggering a 520 failure. This typically occurs with lengthy in clauses, as demonstrated here:


_10
const { data, error } = await supabase
_10
.from('countries')
_10
.select()
_10
.not('id', 'in', '(5,6,7,8,9,...10,000)')

To circumvent this issue, you must use RPCs. They are database functions that you can call from the API. Instead of including a query's structure within the URL or header, they move it into the request's payload.

Here is a basic example of a database function


_10
create or replace function example(id uuid[])
_10
returns uuid[]
_10
language plpgsql
_10
as $$
_10
begin
_10
raise log 'the function example was called with an array size of: %', (select array_length(id, 1));
_10
return id;
_10
end;
_10
$$;

The RPC can then call the function with an array that contains more than 16KB of data


_10
const { data, error } = await supabase.rpc('example', { id: ['e2f34fb9-bbf9-4649-9b2f-09ec56e67a42', ...900 more UUIDs] })