0
0
PostgreSQLquery~20 mins

pg_stat_statements for slow queries in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
pg_stat_statements Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Identify the slowest query using pg_stat_statements

Given the pg_stat_statements view, which query will return the SQL statement with the highest average execution time?

PostgreSQL
SELECT query, mean_time FROM pg_stat_statements ORDER BY mean_time DESC LIMIT 1;
ASELECT query, calls FROM pg_stat_statements ORDER BY calls DESC LIMIT 1;
BSELECT query, total_time FROM pg_stat_statements ORDER BY total_time ASC LIMIT 1;
CSELECT query, mean_time FROM pg_stat_statements ORDER BY mean_time DESC LIMIT 1;
DSELECT query, mean_time FROM pg_stat_statements ORDER BY mean_time ASC LIMIT 1;
Attempts:
2 left
💡 Hint

Think about which column shows the average time per execution and how to get the highest value.

🧠 Conceptual
intermediate
1:30remaining
Understanding pg_stat_statements columns

Which column in pg_stat_statements helps identify how many times a query was executed?

Acalls
Bmean_time
Crows
Dtotal_time
Attempts:
2 left
💡 Hint

Look for the column that counts executions.

📝 Syntax
advanced
1:30remaining
Correct syntax to reset pg_stat_statements statistics

Which SQL command correctly resets all statistics collected by pg_stat_statements?

ASELECT pg_stat_statements_reset();
BRESET pg_stat_statements;
CTRUNCATE pg_stat_statements;
DDELETE FROM pg_stat_statements;
Attempts:
2 left
💡 Hint

Resetting statistics is done by a function call, not by deleting or truncating.

optimization
advanced
2:30remaining
Optimizing query to find slow queries with high calls

You want to find queries that are both slow on average and executed many times. Which query correctly selects queries with average time over 100ms and calls over 1000?

PostgreSQL
SELECT query, mean_time, calls FROM pg_stat_statements WHERE mean_time > 100 AND calls > 1000 ORDER BY mean_time DESC;
ASELECT query, mean_time, calls FROM pg_stat_statements WHERE mean_time > 100 AND calls < 1000 ORDER BY calls DESC;
BSELECT query, mean_time, calls FROM pg_stat_statements WHERE mean_time > 100 AND calls > 1000 ORDER BY mean_time DESC;
CSELECT query, mean_time, calls FROM pg_stat_statements WHERE mean_time < 100 AND calls > 1000 ORDER BY mean_time DESC;
DSELECT query, mean_time, calls FROM pg_stat_statements WHERE mean_time >= 100 OR calls >= 1000 ORDER BY calls DESC;
Attempts:
2 left
💡 Hint

Use AND to combine conditions that must both be true.

🔧 Debug
expert
2:00remaining
Diagnose error when querying pg_stat_statements

You run this query but get an error: ERROR: relation "pg_stat_statements" does not exist. What is the most likely cause?

AYou need to prefix pg_stat_statements with the schema name 'public'.
BThe query syntax is incorrect; pg_stat_statements is a function, not a table.
CThe user does not have SELECT permission on pg_stat_statements.
DThe pg_stat_statements extension is not installed or enabled in the current database.
Attempts:
2 left
💡 Hint

Check if the extension is installed and enabled.