Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Responses are slow when the schema cache reloads #3327

Closed
steve-chavez opened this issue Mar 14, 2024 · 3 comments · Fixed by #3335
Closed

Responses are slow when the schema cache reloads #3327

steve-chavez opened this issue Mar 14, 2024 · 3 comments · Fixed by #3335
Labels
enhancement a feature, ready for implementation QOS

Comments

@steve-chavez
Copy link
Member

steve-chavez commented Mar 14, 2024

Problem

Whenever the db is complex, loading the schema cache takes a while (this is #3046). One consequence of that is that requests that use the schema cache are slow:

# $ nix-shell

$ PGRST_SERVER_TIMING_ENABLED=1 PGRST_DB_SCHEMAS="apflora" postgrest-with-postgresql-16 -f test/io/big_schema.sql postgrest-run

14/Mar/2024:11:22:15 -0500: Starting PostgREST 12.1 (160caaf)...
...
14/Mar/2024:11:22:15 -0500: Schema cache queried in 33.7 milliseconds
$ curl 'localhost:3000/tpopmassn?select=*,tpop(*)' -i
Server-Timing: jwt;dur=0.0, parse;dur=0.1, plan;dur=11371.5, transaction;dur=0.6, response;dur=0.0

[]

# see the "plan;dur=11371.5", it's ~11 seconds

(Note that requests that don't use the schema cache, such as curl 'localhost:3000/tpop, aren't affected by this, they're fast)

That is expected of #3046. However, the same problem happens when reloading the schema cache.

postgres=# notify pgrst;
14/Mar/2024:11:26:10 -0500: Attempting to connect to the database...
14/Mar/2024:11:26:10 -0500: Successfully connected to PostgreSQL 16.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 12.3.0, 64-bit
14/Mar/2024:11:26:10 -0500: Config reloaded
14/Mar/2024:11:26:10 -0500: Schema cache queried in 45.8 milliseconds
$ curl 'localhost:3000/tpopmassn?select=*,tpop(*)'
Server-Timing: jwt;dur=0.0, parse;dur=0.1, plan;dur=12828.9, transaction;dur=0.5, response;dur=0.0

Solution

Use the stale schema cache while waiting for the new one to be loaded.

Edit: The fix is soo simple. And the test is sooo much work.

Related

@steve-chavez steve-chavez added QOS enhancement a feature, ready for implementation idea Needs of discussion to become an enhancement, not ready for implementation and removed enhancement a feature, ready for implementation idea Needs of discussion to become an enhancement, not ready for implementation labels Mar 14, 2024
@steve-chavez
Copy link
Member Author

Use the stale schema cache while waiting for the new one to be loaded.

This definitely can create some stale schema cache errors. In some cases it'd be better to wait instead of err.

Perhaps we should revert this once #3046 is solved.

@steve-chavez
Copy link
Member Author

In some cases it'd be better to wait instead of err.

This is definitely always the desired behavior I think. It's better to be correct/slower and not let clients run into some non-existent column error. I'll revert back this change.


Then perhaps we should remove the below claim on https://postgrest.org/en/v12/references/schema_cache.html#schema-cache-reloading

There’s no downtime when reloading the schema cache. The reloading will happen on a background thread while serving requests.

Or clarify that responses will be slower for a while.

@wolfgangwalther
Copy link
Member

This is definitely always the desired behavior I think. It's better to be correct/slower and not let clients run into some non-existent column error. I'll revert back this change.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement a feature, ready for implementation QOS
Development

Successfully merging a pull request may close this issue.

2 participants