Postgres Scan Types in EXPLAIN Plans
Postgres Scan Types in EXPLAIN Plans
When working with PostgreSQL, understanding query performance is crucial. One of the most powerful tools available to developers and DBAs is the EXPLAIN command. It reveals how PostgreSQL executes a query, including the scan methods it uses to retrieve data.
In this blog, we’ll break down the different scan types in PostgreSQL EXPLAIN plans, when they are used, and how to optimize them.
What is EXPLAIN in PostgreSQL?
EXPLAIN is a command that shows the execution plan of a query. It helps you understand:
- How tables are accessed
- Which indexes are used
- Estimated cost and rows
- Join strategies and scan types
Example:
EXPLAIN SELECT * FROM employees WHERE salary > 50000;
Why Scan Types Matter
Scan types define how PostgreSQL reads data from tables or indexes. Choosing the right scan method can significantly improve performance.
Types of Scan in PostgreSQL
1. Sequential Scan (Seq Scan)
What it is:
A Sequential Scan reads the entire table from start to end.
Example:
EXPLAIN SELECT * FROM employees;
When PostgreSQL Uses It:
- Small tables
- No suitable index available
- Large portion of table is needed
Pros:
- Simple and efficient for small tables
Cons:
- Slow for large tables
Optimization Tip:
- Create indexes for frequently filtered columns
2. Index Scan
What it is:
An Index Scan uses an index to find matching rows, then fetches data from the table.
Example:
EXPLAIN SELECT * FROM employees WHERE employee_id = 101;
When Used:
- Queries with selective conditions
- Index exists on filtered column
Pros:
- Faster than sequential scan for selective queries
Cons:
- Still needs table access (heap fetch)
3. Index Only Scan
What it is:
Retrieves data directly from the index without accessing the table.
Example:
EXPLAIN SELECT employee_id FROM employees WHERE employee_id = 101;
When Used:
- All required columns are in the index
- Visibility map allows it
Pros:
- Very fast (no table access)
Cons:
- Requires proper indexing strategy
4. Bitmap Index Scan
What it is:
Creates a bitmap of matching rows using an index.
Example:
EXPLAIN SELECT * FROM employees WHERE department_id = 5;
When Used:
- Medium selectivity queries
- Multiple rows match condition
5. Bitmap Heap Scan
What it is:
Works with Bitmap Index Scan to fetch actual rows from the table.
How it Works:
- Bitmap Index Scan identifies row locations
- Bitmap Heap Scan retrieves data
Pros:
- Efficient for large result sets
- Reduces random I/O
6. Tid Scan
What it is:
Fetches rows using Tuple ID (TID).
Example:
EXPLAIN SELECT * FROM employees WHERE ctid = '(0,1)';
When Used:
- Rare use cases
- System-level operations
7. Subquery Scan
What it is:
Used when a query involves subqueries.
Example:
EXPLAIN SELECT * FROM (SELECT * FROM employees) AS emp;
8. Function Scan
What it is:
Used when querying a function returning a result set.
Example:
EXPLAIN SELECT * FROM generate_series(1,10);
9. Values Scan
What it is:
Handles queries using VALUES.
Example:
EXPLAIN VALUES (1, 'A'), (2, 'B');
10. CTE Scan (Common Table Expression)
What it is:
Used when querying CTEs.
Example:
WITH emp_cte AS ( SELECT * FROM employees ) SELECT * FROM emp_cte;
How PostgreSQL Chooses Scan Types
PostgreSQL uses a cost-based optimizer that considers:
- Table size
- Index availability
- Row selectivity
- Disk I/O cost
- CPU cost
Practical Tips to Optimize Scan Types
1. Use Indexes Wisely
Create indexes on frequently filtered columns:
CREATE INDEX idx_emp_salary ON employees(salary);
2. Analyze Table Statistics
ANALYZE employees;
3. Use EXPLAIN ANALYZE
EXPLAIN ANALYZE SELECT * FROM employees WHERE salary > 50000;
4. Avoid SELECT *
Fetch only required columns.
5. Use Partial Indexes
For better selectivity:
CREATE INDEX idx_high_salary ON employees(salary) WHERE salary > 50000;
Conclusion
Understanding scan types in PostgreSQL EXPLAIN plans is essential for optimizing query performance. Each scan type has its own purpose, and choosing the right strategy can drastically reduce execution time.
Whether you’re a beginner or an experienced DBA, mastering these concepts will help you:
- Write efficient queries
- Diagnose slow performance
- Optimize database workloads
At Learnomate Technologies, we provide practical training on PostgreSQL, real-time query optimization, and performance tuning techniques. Our hands-on approach helps you understand concepts like EXPLAIN plans deeply and apply them in real-world scenarios.





