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
- Execute the below DDL statements.
1-- Create table2CREATE TABLE your_table_name (3 id UUID PRIMARY KEY,4 column1 TEXT,5 column2 INT,6 column3 BOOLEAN7);89-- Insert statements10INSERT INTO your_table_name (id, column1, column2, column3) VALUES11 (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);- Run the following script (you may need to
pip install psycopg[binary]in addition to Supabase client.
1import time2from supabase import Client, create_client34import psycopg567def psycop_call(): #user_ids: list[str]):8 user="YOUR_SUPABASE_USER"9 password="YOUR_SUPABASE_PASSWORD"10 host="SUPABASE_HOST"11 port=543212 database="postgres"1314 with psycopg.connect(f"host={host} port={port} dbname={database} user={user} password={password}") as conn:15 # Open a cursor to perform database operations16 results = []17 with conn.cursor() as cur:18 start = time.time()19 # Execute a command: this creates a new table20 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)262728def 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)343536if __name__ == "__main__":37 ref = psycop_call()38 sup = supabase_call()39 print(f"postgres: {ref}, supabase: {sup}, ratio: {sup/ref}")- 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.