High latency with supabase client

Last edited: 2/4/2025

Describe the bug

Querying a table using the Supabase client is much slower than querying against the Postgres database directly.

To reproduce

  1. Execute the below DDL statements.
1
-- Create table
2
CREATE TABLE your_table_name (
3
id UUID PRIMARY KEY,
4
column1 TEXT,
5
column2 INT,
6
column3 BOOLEAN
7
);
8
9
-- Insert statements
10
INSERT INTO your_table_name (id, column1, column2, column3) VALUES
11
(uuid_generate_v4(), 'value1', 10, TRUE),
12
(uuid_generate_v4(), 'value2', 20, FALSE),
13
(uuid_generate_v4(), 'value3', 15, TRUE),
14
(uuid_generate_v4(), 'value4', 8, FALSE),
15
(uuid_generate_v4(), 'value5', 25, TRUE),
16
(uuid_generate_v4(), 'value6', 12, FALSE),
17
(uuid_generate_v4(), 'value7', 18, TRUE),
18
(uuid_generate_v4(), 'value8', 30, FALSE),
19
(uuid_generate_v4(), 'value9', 22, TRUE),
20
(uuid_generate_v4(), 'value10', 5, FALSE),
21
(uuid_generate_v4(), 'value11', 17, TRUE),
22
(uuid_generate_v4(), 'value12', 9, FALSE),
23
(uuid_generate_v4(), 'value13', 14, TRUE),
24
(uuid_generate_v4(), 'value14', 28, FALSE),
25
(uuid_generate_v4(), 'value15', 11, TRUE),
26
(uuid_generate_v4(), 'value16', 7, FALSE),
27
(uuid_generate_v4(), 'value17', 19, TRUE),
28
(uuid_generate_v4(), 'value18', 26, FALSE),
29
(uuid_generate_v4(), 'value19', 16, TRUE),
30
(uuid_generate_v4(), 'value20', 21, FALSE);
  1. Run the following script (you may need to pip install psycopg[binary] in addition to Supabase client.
1
import time
2
from supabase import Client, create_client
3
4
import psycopg
5
6
7
def psycop_call(): #user_ids: list[str]):
8
user="YOUR_SUPABASE_USER"
9
password="YOUR_SUPABASE_PASSWORD"
10
host="SUPABASE_HOST"
11
port=5432
12
database="postgres"
13
14
with psycopg.connect(f"host={host} port={port} dbname={database} user={user} password={password}") as conn:
15
# Open a cursor to perform database operations
16
results = []
17
with conn.cursor() as cur:
18
start = time.time()
19
# Execute a command: this creates a new table
20
cur.execute("SELECT * FROM public.your_table_name")
21
cur.fetchall()
22
for record in cur:
23
results.append(record)
24
stop = time.time()
25
return (stop - start)
26
27
28
def supabase_call():
29
supabase: Client = create_client("SUPABASE_URL", "SUPBASE_SERVICE_ROLE_KEY")
30
start = time.time()
31
result = supabase.table("your_table_name").select("*").execute()
32
stop = time.time()
33
return (stop - start)
34
35
36
if __name__ == "__main__":
37
ref = psycop_call()
38
sup = supabase_call()
39
print(f"postgres: {ref}, supabase: {sup}, ratio: {sup/ref}")
  1. You will see that the Supabase client takes longer to execute the same query, especially for smaller tables or queries returning just one row.

Expected behavior

The overhead from PostgREST shouldn't be higher than a few milliseconds at max. 60-70 ms is way too high. This is particular deceiving because one can run the query on the SQL Editor page and it reports the same time as the direct Postgres query, which is not what actually happens.