Profiling Queries
Before deciding to cache a query, it's worth investigating the performance impact each query has on your application.
One method for doing so:
Enabling metrics
You can use the pg_stat_statements extension to retrieve detailed information about the queries running against your Postgres instance.
Connect to your database via the shell. Run the following command to see if pg_stat_statements is installed.
SELECT calls, query FROM pg_stat_statements LIMIT 1;If an error is returned, enable pg_stat_statments with the following command:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;ALTER SYSTEM SET shared_preload_libraries = 'pg_stat_statements'; and restart your Postgres instance before re-running the CREATE EXTENSION command.To enable Readyset metrics, start Readyset with the following options:
To include query-specific execution metrics, also pass:
You can access Readyset metrics at <metrics address>/metrics, where the metrics address is defined by the --metrics-address option (default: 0.0.0.0:6034/metrics).
Analyzing per-query metrics
Readyset can cache many SELECT queries.
To find SELECT queries with the highest latency, run:
SELECT query, calls, total_exec_time, mean_exec_time from pg_stat_statements WHERE query ILIKE '%SELECT%' order by mean_exec_time DESC;Similarly, Readyset can be used to offload high-impact queries to improve throughput.
To find the most frequently-run SELECT queries, run:
SELECT query, calls, total_exec_time, mean_exec_time from pg_stat_statements WHERE query ILIKE '%SELECT%' order by calls DESC;To find queries that cause the most total load on the database, run:
SELECT query, calls, total_exec_time, mean_exec_time from pg_stat_statements WHERE query ILIKE '%SELECT%' order by total_exec_time DESC;Readyset metrics are formatted for easy integration with Prometheus. However, the quickest way to examine per-query metrics is to use a simple metrics utility written in Python that queries the metrics endpoint and displays latencies for queries received by Readyset.
- Download the metrics utility and its
requirements.txt:
curl -L -O "https://readyset.io/quickstart/metrics.py"
curl -L -O "https://readyset.io/quickstart/requirements.txt"- Install dependencies for the utility:
pip3 install -r requirements.txt- Set the
METRICS_HOSTenvironment variable to the IP address/hostname portion of--metrics-address:
export METRICS_HOST="<metrics-host>"- Run the script with
python3 metrics.py --host=${METRICS_HOST}- You can filter the output of this script to show only queries displayed in
SHOW PROXIED QUERIESorSHOW CACHESby passing the--filter-queriesflag and piping the output of those Readyset commands into the script like so:
PGPASSWORD=noria psql --host=127.0.0.1 --port=5433 --username=postgres --dbname=noria -c "SHOW CACHES" | python3 metrics.py --filter-queries