0
0
PostgreSQLquery~10 mins

Hash index for equality in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hash index for equality
Query with equality condition
Check if hash index exists on column
Yes
Compute hash of search value
Use hash index to find matching rows
Return rows matching equality condition
End
When a query uses equality (=) on a column with a hash index, PostgreSQL computes the hash of the search value and uses the index to quickly find matching rows.
Execution Sample
PostgreSQL
CREATE INDEX idx_hash_name ON users USING hash (name);
SELECT * FROM users WHERE name = 'Alice';
Create a hash index on the 'name' column and query rows where name equals 'Alice'.
Execution Table
StepActionInput/ConditionResult/Output
1Receive querySELECT * FROM users WHERE name = 'Alice'Start processing query
2Check index type on 'name'Hash index existsUse hash index for lookup
3Compute hashHash('Alice')Hash value computed
4Search hash indexHash valueFind matching row pointers
5Fetch rowsRow pointers from indexRetrieve rows where name = 'Alice'
6Return resultMatching rowsRows with name 'Alice' returned
7EndNo more stepsQuery complete
💡 Query ends after returning all rows matching the equality condition using the hash index.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
querySELECT * FROM users WHERE name = 'Alice'SameSameSame
hash_index_existsUnknownTrueTrueTrue
hash_valueNoneHash('Alice')Hash('Alice')Hash('Alice')
row_pointersNoneNoneFound pointersFound pointers
result_rowsNoneNoneNoneRows with name 'Alice'
Key Moments - 2 Insights
Why does PostgreSQL use a hash index only for equality conditions?
Because hash indexes store hash values that match exactly, they work efficiently only for equality (=) checks, not for range or inequality conditions. See execution_table step 2 and 3.
What happens if there is no hash index on the column?
PostgreSQL will not use a hash index and will perform a sequential scan or use another index type. This is implied in execution_table step 2 where the presence of the hash index is checked.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the hash value computed for?
AThe entire row data
BThe search value 'Alice'
CThe table name
DThe index name
💡 Hint
Refer to execution_table step 3 where hash is computed for 'Alice'
At which step does PostgreSQL fetch the actual rows from the table?
AStep 5
BStep 3
CStep 2
DStep 7
💡 Hint
Check execution_table step 5 where rows are retrieved using row pointers
If the query used a condition 'name LIKE 'Al%'' instead of equality, what would happen?
AHash index would still be used
BQuery would fail
CHash index would not be used
DHash index would be converted to B-tree
💡 Hint
Hash indexes only support equality, see key_moments explanation
Concept Snapshot
Hash index in PostgreSQL:
- Created with: CREATE INDEX idx ON table USING hash(column);
- Used only for equality (=) conditions
- Works by hashing the search value and quickly locating matching rows
- Not suitable for range or pattern searches
- Provides fast lookups when equality condition matches
Full Transcript
This visual execution trace shows how PostgreSQL uses a hash index for queries with equality conditions. First, the query is received and PostgreSQL checks if a hash index exists on the searched column. If yes, it computes the hash of the search value. Then it uses the hash index to find pointers to matching rows. Finally, it fetches and returns the rows where the column equals the search value. Hash indexes only work efficiently for equality conditions, not for ranges or patterns. If no hash index exists, PostgreSQL uses other methods like sequential scans or different indexes.