internetarchive / openlibrary

One webpage for every book ever published!
https://openlibrary.org
GNU Affero General Public License v3.0
5.13k stars 1.35k forks source link

Add Postgres logging & instrumentation: identify/evaluate idle connections #7838

Open mekarpeles opened 1 year ago

mekarpeles commented 1 year ago

Enable logging in your PostgreSQL database to identify which queries are being executed, and their execution times.

Enable logging in postgresql.conf by setting log_statement='all'

See: https://www.youtube.com/watch?v=rQ57lcHIDJI

ChatGPT says:

Enabling logging in PostgreSQL will allow you to see the SQL statements that are being executed, along with information such as the query execution time, the number of rows affected, and the execution plan. This information can be useful in identifying problematic queries that are taking a long time to execute or causing excessive database load.

In particular, the log_min_duration_statement configuration parameter controls the minimum amount of time that a statement must take before it is logged. Setting this parameter to a lower value, such as 100ms or 1s, can help you identify slow-running queries.

To identify the most time-consuming queries, you can sort the log output by the duration field. This can be done using the pgfouine tool, which can parse the PostgreSQL log files and generate a report showing the most time-consuming queries, along with their execution time and frequency.

Alternatively, you can use a tool like pgBadger or pganalyze to analyze the PostgreSQL logs and generate visualizations of query performance metrics, such as query duration, frequency, and errors.

Once you have identified the problematic queries, you can optimize them by adding indexes, rewriting them to use more efficient SQL, or optimizing the schema design.

To monitor query performance in real-time, you can use tools like pg_stat_statements, which tracks the performance of all SQL statements executed on a PostgreSQL server, or pg_activity, which provides a real-time view of PostgreSQL server activity, including queries, connections, and locks.

github-actions[bot] commented 8 months ago

Assignees removed automatically after 14 days.