Consider a PostgreSQL table employees with columns id, name, and department. An index exists on department and name. Which query will most likely produce an index-only scan result?
SELECT name FROM employees WHERE department = 'Sales';
Index-only scans work best when the query can be answered using only the indexed columns.
Option A queries only columns name and department, which are both in the index. This allows PostgreSQL to use an index-only scan without accessing the table data.
What role does the visibility map play in PostgreSQL's index-only scans?
Think about how PostgreSQL avoids reading the main table during index-only scans.
The visibility map marks pages where all tuples are visible to all transactions. This allows index-only scans to skip heap access for those pages, improving performance.
Given a table orders with an index on customer_id and order_date, which query will NOT use an index-only scan and will force heap access?
Index-only scans require all selected columns to be in the index.
Option C selects total_amount, which is not in the index. This forces PostgreSQL to access the heap to retrieve that column, preventing an index-only scan.
You notice that your index-only scan queries still access the heap frequently. What is the best way to reduce heap access and improve index-only scan performance?
Think about how PostgreSQL knows which pages are safe to skip.
Running VACUUM updates the visibility map, marking pages as all-visible. This allows index-only scans to skip heap access on those pages, improving performance.
You created a multi-column index on products(category, price). You run this query:
SELECT category FROM products WHERE price < 100;
But PostgreSQL does not use an index-only scan. What is the most likely reason?
Consider what PostgreSQL needs to skip heap access.
Even if the index covers the columns, if the visibility map is not updated (e.g., no recent VACUUM), PostgreSQL must access the heap to check tuple visibility, preventing index-only scans.