0
0
PostgreSQLquery~10 mins

Indexing JSONB with GIN in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Indexing JSONB with GIN
Create Table with JSONB Column
Insert JSONB Data
Create GIN Index on JSONB Column
Query Using JSONB Operators
PostgreSQL Uses GIN Index to Speed Up Search
Return Results
This flow shows how to create a table with a JSONB column, add a GIN index on it, and then run queries that use the index to quickly find matching JSON data.
Execution Sample
PostgreSQL
CREATE TABLE products (id serial PRIMARY KEY, info JSONB);
INSERT INTO products (info) VALUES
  ('{"name": "apple", "color": "red"}'),
  ('{"name": "banana", "color": "yellow"}');
CREATE INDEX idx_gin_info ON products USING GIN (info);
SELECT * FROM products WHERE info @> '{"color": "red"}';
Create a table with JSONB data, insert two products, create a GIN index on the JSONB column, then query for products where color is red.
Execution Table
StepActionDetailsResult
1Create TableTable 'products' with 'info' JSONB column createdTable ready
2Insert Row 1Insert JSONB {'name': 'apple', 'color': 'red'}Row inserted
3Insert Row 2Insert JSONB {'name': 'banana', 'color': 'yellow'}Row inserted
4Create GIN IndexIndex 'idx_gin_info' created on 'info' columnIndex ready
5QuerySelect rows where info contains {'color': 'red'}Uses GIN index to find matching rows
6Return ResultRow with 'apple' returnedQuery result: 1 row
💡 Query completes after using GIN index to efficiently find matching JSONB data
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
products tableempty1 row (apple)2 rows (apple, banana)2 rows (apple, banana)2 rows (apple, banana)2 rows (apple, banana)
GIN indexnonenonenonecreated on infoused for queryused for query
Query resultnonenonenonenone1 row (apple)1 row (apple)
Key Moments - 3 Insights
Why do we create a GIN index on the JSONB column?
The GIN index helps PostgreSQL quickly find rows where the JSONB data matches a condition, as shown in step 5 where the query uses the index to find the 'apple' row efficiently.
What does the operator '@>' mean in the query?
The '@>' operator means 'contains'. In the query (step 5), it checks if the JSONB column contains the key-value pair {'color': 'red'}, which matches the 'apple' row.
Does the GIN index store the whole JSON document?
No, the GIN index stores keys and values in a way that allows fast searching for parts of the JSON, not the entire document, which is why it speeds up queries like in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the 'products' table after step 3?
A2 rows with 'apple' and 'banana'
B1 row with 'apple'
CEmpty
D3 rows with 'apple', 'banana', and another
💡 Hint
Check the 'products table' row in variable_tracker after step 3
At which step is the GIN index created?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Action' column in execution_table where index creation is mentioned
If we query for color 'yellow' instead of 'red', what would change in the execution table?
ANo rows would be returned
BStep 5 would find the 'banana' row instead of 'apple'
CThe GIN index would not be used
DThe table would have more rows
💡 Hint
Consider how the query condition affects which rows match in step 5
Concept Snapshot
PostgreSQL JSONB GIN Index:
- Create table with JSONB column
- Insert JSON data
- Create GIN index on JSONB column
- Query using '@>' operator to check containment
- GIN index speeds up JSONB key/value searches
- Use for fast filtering on JSONB fields
Full Transcript
This visual execution trace shows how to use a GIN index on a JSONB column in PostgreSQL. First, a table named 'products' is created with a JSONB column called 'info'. Then two rows are inserted with JSON data representing products 'apple' and 'banana'. Next, a GIN index is created on the 'info' column to speed up queries. When a query searches for products where the JSONB data contains the key-value pair 'color' equal to 'red', PostgreSQL uses the GIN index to quickly find the matching row with 'apple'. The trace shows each step, the state of the table and index, and how the query result is returned efficiently. Key points include understanding the '@>' operator for containment and how the GIN index stores JSON keys and values for fast searching.