1

Our Django tests are getting slow, and a lot of time is spend in cursor.execue().

There are roughly two ways now: Rewrite the tests to mock the ORM, or make PostgreSQL faster.

This question is about "make PostgreSQL faster".

What can I do to improve PostgreSQL speed if D (Durability) of ACID is not important. Everything could be kept in RAM.

Constraint: This question is about what you can do without modifying the Django/Python/SQL code.

asked Mar 2, 2021 at 10:12
5
  • As Laurenz pointed out, query tuning generally yields the biggest gains in performance by far. If you provide some example queries, their EXPLAIN ANALYZE, and the database schema, likely we can recommend ways to improve them. Commented Mar 2, 2021 at 12:10
  • @J.D. the test suite creates several thousand DB queries. All of them are fast. There are no missing indexes. Commented Mar 2, 2021 at 13:01
  • I never said anything about missing indexes, rather their execution plans might tell of ways to improve their performance (either through tuning, architecting, or even instance changes as you're asking about) and fast is a subjective term. If your queries were perfectly fast then you wouldn't have this question on how to "make PostgreSQL faster". Commented Mar 2, 2021 at 13:18
  • 1
    @J.D. I would like to stick to this: This question is about what you can do without modifying the Django/Python/SQL code. I know that there are thousand other ways, but let's focus on this topic here. Commented Mar 2, 2021 at 13:28
  • 1
    Then the suggestions that Laurenz made are about the extent of what you can do, you can give those a try. Commented Mar 2, 2021 at 14:02

1 Answer 1

4
  • use UNLOGGED tables throughout

  • set shared_buffers big enough to contain the whole database

  • if you have bigger queries, increase work_mem

  • have enough RAM to contain shared_buffers plus work_mem times the number of client connections

Typically, you can gain most by tuning the queries that use most of the time.

answered Mar 2, 2021 at 10:35
6
  • If the data isn't important, turning off fsync will probably speed up things as well Commented Mar 2, 2021 at 13:30
  • @a_horse_with_no_name For unlogged tables that won't make much difference. Commented Mar 2, 2021 at 13:59
  • But the unlogged tables require changing the SQL scripts that setup the database, whereas turning off fsync requires no changes to the schema migration scripts Commented Mar 2, 2021 at 14:00
  • 1
    @a_horse_with_no_name True. But then I'd set synchronous_commit = off rather than the dreaded fsync. Bypassing WAL altogether with unlogged table should perform best of all. Commented Mar 2, 2021 at 14:13
  • I love PG, since the docs are great. Now I found an official page which exactly this topic postgresql.org/docs/current/non-durability.html Commented Mar 3, 2021 at 6:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.