Skip to content

Database Troubleshooting

Walter Moar edited this page Nov 1, 2023 · 7 revisions

Home > Developer > DevOps > Database Troubleshooting


CHEFS uses the PostgreSQL database with high availability provided by Patroni. As CHEFS users increase the database grows both in size and the number of queries that it has to process. The queries must be very efficient, otherwise they will become a bottleneck, the load on the database will grow until it reaches the CPU limit, and then API calls will begin to fail.

Basic Monitoring (TODO)

Using pg_stat_statements

Using the primary database pod, check if the pg_stat_statements extension is installed:

SELECT * FROM pg_extension WHERE extname = 'pg_stat_statements';

If not then check that it's available:

SELECT * FROM pg_available_extensions WHERE name = 'pg_stat_statements';

It should be there! If it isn't then you're on your own. To install the extension:

CREATE EXTENSION pg_stat_statements;

Now re-check that the extension is installed:

SELECT * FROM pg_extension WHERE extname = 'pg_stat_statements';

Doing a SELECT for the view should fail:

SELECT * FROM pg_stat_statements;

SQL Error [55000]: ERROR: pg_stat_statements must be loaded via shared_preload_libraries

You need to edit the Patroni configuration, which is fun because Patroni uses a minimalist container that doesn't have an editor like ed or vi.

  1. Open a terminal for one of the database pods.
  2. Temporarily install busybox to use it as an editor:
    1. export EDITOR=/tmp/vi
    2. curl https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox --output $EDITOR
    3. chmod 700 $EDITOR
  3. Run patronictl edit-config and add shared_preload_libraries: pg_stat_statements under parameters. This changes the file /home/postgres/pgdata/pgroot/data/patroni.dynamic.json, and then updates /home/postgres/pgdata/pgroot/data/postgresql.conf
  4. Delete the editor: rm $EDITOR
  5. Run patronictl restart
Clone this wiki locally