0
0
PostgreSQLquery~10 mins

B-tree index (default) behavior in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - B-tree index (default) behavior
Start: Insert/Search Key
Check Root Node
Compare Key with Node Keys
Go to Child
Repeat at Child
If Insert and Node Full
Split Node and Promote Middle Key
Update Parent Node
End
The B-tree index starts at the root, compares keys to find the correct child or leaf, and on insert splits nodes if full, promoting keys up to keep the tree balanced.
Execution Sample
PostgreSQL
CREATE TABLE fruits(id SERIAL PRIMARY KEY, name TEXT);
CREATE INDEX idx_name ON fruits(name);
INSERT INTO fruits(name) VALUES ('apple'), ('banana'), ('cherry');
SELECT * FROM fruits WHERE name = 'banana';
This creates a table and a B-tree index on the 'name' column, inserts some fruits, and queries for 'banana' using the index.
Execution Table
StepActionNode LevelKeys in NodeComparison ResultNext Step/Output
1Start search for 'banana'Root['apple', 'cherry']'banana' > 'apple', < 'cherry'Go to child between 'apple' and 'cherry'
2At leaf nodeLeaf['apple', 'banana', 'cherry']'banana' == 'banana'Found key, return row(s)
3Insert 'date'Leaf['apple', 'banana', 'cherry']'date' > 'cherry'Insert at leaf
4Leaf node full after insertLeaf['apple', 'banana', 'cherry', 'date']Node full (max keys reached)Split leaf node
5Split leaf nodeLeafLeft: ['apple', 'banana'], Right: ['cherry', 'date']Promote 'cherry' to parentUpdate parent node
6Update root node keysRoot['cherry']Root updated with promoted keyInsertion complete
7Search for 'date'Root['cherry']'date' > 'cherry'Go to right child leaf
8At leaf nodeLeaf['cherry', 'date']'date' == 'date'Found key, return row(s)
9End---Search/Insert complete
💡 Search ends when key is found or leaf reached; insert ends after node split and parent update if needed.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
Root Node Keys['apple', 'cherry']['apple', 'cherry']['apple', 'cherry']['cherry']['cherry']['cherry']
Leaf Node Keys['apple', 'banana', 'cherry']['apple', 'banana', 'cherry', 'date']Node splitLeft: ['apple', 'banana'], Right: ['cherry', 'date']N/AN/A
Search KeyN/A'date''date''date''date'N/A
Key Moments - 3 Insights
Why does the root node keys change after inserting 'date'?
Because the leaf node became full and split, the middle key 'cherry' was promoted to the root node to keep the tree balanced, as shown in execution_table rows 4-6.
Why do we go to a child node after comparing keys in the root?
In a B-tree, internal nodes guide the search by comparing keys and choosing the correct child node to continue searching, as seen in execution_table row 1.
What happens when the leaf node is full during insert?
The leaf node splits into two nodes, and the middle key is promoted to the parent node to maintain balance, shown in execution_table rows 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the leaf node split due to being full?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Check the 'Action' column for 'Node full after insert' and 'Split leaf node' in rows.
According to variable_tracker, what are the root node keys after the split and promotion?
A['apple', 'banana']
B['apple', 'cherry']
C['cherry']
D['banana', 'date']
💡 Hint
Look at 'Root Node Keys' values after Step 5 and Step 6 in variable_tracker.
If we searched for 'apple' instead of 'banana', at which step would the search go to the left child?
AStep 7
BStep 1
CStep 2
DStep 8
💡 Hint
Refer to execution_table row 1 where the root node keys guide the search direction.
Concept Snapshot
B-tree index stores keys in sorted order in nodes.
Search starts at root, compares keys, and moves down child nodes.
Leaf nodes hold actual keys and pointers to rows.
On insert, if node is full, it splits and promotes middle key.
This keeps the tree balanced for fast search and insert.
PostgreSQL uses B-tree as default index type.
Full Transcript
A B-tree index in PostgreSQL organizes data in a balanced tree structure. When searching for a key, it starts at the root node and compares the key with keys stored there. Depending on the comparison, it moves to the appropriate child node. This process repeats until it reaches a leaf node where the actual key is stored. If the key is found, the corresponding row is returned. When inserting a new key, if the leaf node is full, it splits into two nodes and the middle key is promoted to the parent node. This promotion may propagate up to the root, keeping the tree balanced. This behavior ensures efficient search and insert operations. The execution table shows step-by-step how a search and insert happen, including node splits and key promotions. The variable tracker shows how keys in nodes change during these steps. Key moments clarify why nodes split and how the tree structure updates. The visual quiz tests understanding of these steps. The concept snapshot summarizes the B-tree index behavior in PostgreSQL.