Analyzing index usage with pg_stat in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we check how often database indexes are used, we want to understand how the work grows as data grows.
We ask: How does the cost of using indexes change when the table gets bigger?
Analyze the time complexity of this query that checks index usage statistics.
SELECT indexrelname, idx_scan, idx_tup_read, idx_tup_fetch
FROM pg_stat_user_indexes
WHERE schemaname = 'public';
This query reads index usage stats for all indexes in the public schema.
Look for repeated actions in the query.
- Primary operation: Scanning the pg_stat_user_indexes view rows.
- How many times: Once per index in the public schema.
As the number of indexes grows, the query reads more rows.
| Input Size (number of indexes) | Approx. Rows Read |
|---|---|
| 10 | 10 rows |
| 100 | 100 rows |
| 1000 | 1000 rows |
Pattern observation: The work grows directly with the number of indexes.
Time Complexity: O(n)
This means the time to get index stats grows linearly with how many indexes exist.
[X] Wrong: "The query time depends on the size of the table data."
[OK] Correct: This query reads only index stats, not the table rows, so table size does not affect it.
Understanding how system views scale helps you explain database monitoring and performance checks clearly.
"What if we added a filter on index name? How would that change the time complexity?"
Practice
idx_scan column in pg_stat_user_indexes represent?Solution
Step 1: Understand the purpose of
This system view tracks usage statistics for user-created indexes in PostgreSQL.pg_stat_user_indexesStep 2: Identify the meaning of
Theidx_scanidx_scancolumn counts how many times the index was used in scan operations, showing its usage frequency.Final Answer:
The number of times an index was used in a scan operation -> Option AQuick Check:
idx_scan= index usage count [OK]
- Confusing idx_scan with index size
- Thinking idx_scan shows table row count
- Assuming idx_scan is creation date
pg_stat_user_indexes for a table named customers?Solution
Step 1: Check the column names in
The correct column for the table name ispg_stat_user_indexesrelname, nottablenameortable_name.Step 2: Verify the query syntax
SELECT * FROM pg_stat_user_indexes WHERE relname = 'customers'; usesrelname = 'customers', which is correct to filter indexes for the table named 'customers'.Final Answer:
SELECT * FROM pg_stat_user_indexes WHERE relname = 'customers'; -> Option AQuick Check:
Userelnameto filter by table name [OK]
- Using incorrect column names like tablename or table_name
- Filtering by indexname instead of table name
- Syntax errors in WHERE clause
SELECT indexrelname, idx_scan FROM pg_stat_user_indexes WHERE relname = 'orders';
Which output correctly shows index usage if the table
orders has two indexes orders_pkey with 150 scans and orders_date_idx with 0 scans?Solution
Step 1: Understand the query output
The query selects index names and their scan counts for the 'orders' table, so both indexes should appear with their respective scan counts.Step 2: Match the scan counts to indexes
Givenorders_pkeyhas 150 scans andorders_date_idxhas 0, the output must show both with correct values.Final Answer:
[{"indexrelname": "orders_pkey", "idx_scan": 150}, {"indexrelname": "orders_date_idx", "idx_scan": 0}] -> Option DQuick Check:
Index names match scan counts correctly [OK]
- Swapping scan counts between indexes
- Showing null instead of zero for unused indexes
- Omitting indexes with zero scans
SELECT indexrelname FROM pg_stat_user_indexes WHERE idx_scan = 0;
But it returns no rows, even though you know some indexes are unused. What is the likely cause?
Solution
Step 1: Verify query correctness
The query syntax is correct and will return indexes with zero scans if any exist.Step 2: Understand pg_stat_user_indexes behavior
Index usage stats depend on the statistics collector. If it was recently reset or the server restarted, idx_scan may be zeroed or not updated yet.Final Answer:
The statistics collector has not been reset or updated recently -> Option CQuick Check:
Stats collector state affects idx_scan values [OK]
- Assuming query syntax error causes no results
- Thinking idx_scan never tracks usage
- Believing no indexes exist without checking
products?Solution
Step 1: Filter indexes by table name
Userelname = 'products'to focus on indexes for the products table.Step 2: Identify unused indexes by scan count
Indexes withidx_scan = 0have never been used since last stats reset, so they are candidates for removal.Final Answer:
SELECT indexrelname FROM pg_stat_user_indexes WHERE relname = 'products' AND idx_scan = 0; -> Option BQuick Check:
Unused indexes have idx_scan = 0 [OK]
- Using idx_scan > 0 to find unused indexes
- Confusing idx_tup_read with idx_scan
- Checking for NULL instead of zero scans
