Skip to content

One SELECT took our customer panel from 8 seconds to 35 ms

A customer wrote that "My Services" in our panel is slow

It was: 8 seconds to list their own services

The fix: (SELECT ...) around one function call

The setup

Our client panel runs on PostgREST, and we are proud of it. It is the best solution imaginable

Every customer logs in as their own database role, and every view filters rows to that customer:

create view my_services as
select s.*
  from services s
 where s.client = get_client(current_user);

get_client() maps the login to the customer account

Small function, marked STABLE

Roughly like this, minus a check that the login exists:

create function get_client(p_login text)
    returns text
    language plpgsql
    stable
as $$
begin
    return (select client from logins where login = p_login);
end;
$$;

The problem

Postgres called get_client() for every row it checked

Not once per query. Once per row

STABLE does not help

It promises the result will not change within the statement, it does not tell Postgres to cache it

The services table has about 10,600 rows, one customer has 742

Every page load: 10,600 function calls to show 742 rows

-- 10,000 calls, like a scan over 10,000 rows
select count(*) from generate_series(1, 10000)
 where get_client(current_user) is not null;
-- Time: 8297 ms

0.8 ms per call. Nobody notices one

10k of them is 8 seconds

The fix

create or replace view my_services as
select s.*
  from services s
 where s.client = (select get_client(current_user));

A subquery that does not reference the outer row becomes an InitPlan

Postgres runs it once, keeps the value, compares every row against it

EXPLAIN

Before:

Seq Scan on services s
  Filter: (client = get_client(CURRENT_USER))
  Rows Removed by Filter: 9862

After:

Seq Scan on services s
  Filter: (client = $0)
  InitPlan 1
    ->  Result

Newer PostgreSQL versions show $0 as (InitPlan 1).col1

Same thing

Numbers

Server time, same customer:

Page Before After
My Services 8,104 ms 35 ms
My Projects 4,653 ms 109 ms
My VMs ~2,500 ms ~800 ms

My Services buffer reads: 1.4 million down to 5,500

The VM list got a bonus

With one fixed value to match, the planner stopped reading a foreign table once per VM. What is left there is bandwidth stats, separate fix

Check yours

A function in WHERE that returns the same value for every row: wrap it, (select fn(...))

Row level security policies calling a "current user" function have the same problem

In EXPLAIN ANALYZE look for:

  • Filter: with a function call in it
  • Rows Removed by Filter in the thousands, on a query returning a handful of rows

Views with the pattern:

select schemaname, viewname
  from pg_views
 where schemaname not in ('pg_catalog', 'information_schema')
   and definition ~* '[\w.]+\(\(?current_user'
   and definition !~* '\(\s*select\s+[\w.]+\(\(?current_user';

Postgres stores view definitions normalized, like get_client((CURRENT_USER)::text), hence the \(?

Rough check. Confirm every hit with EXPLAIN ANALYZE