What is the purpose of using a GIN index on a JSONB column in PostgreSQL?
A GIN index on a JSONB column helps speed up searches for keys and values inside the JSON data, making queries faster when filtering or searching within JSON documents.
Click to reveal answer
beginner
How do you create a GIN index on a JSONB column named 'data' in a table called 'items'?
Use the SQL command:
CREATE INDEX idx_items_data ON items USING GIN (data);
This creates a GIN index on the 'data' JSONB column to speed up queries.
Click to reveal answer
intermediate
What kind of queries benefit most from a GIN index on JSONB?
Queries that use the containment operator @> to check if a JSONB column contains a specific key or value, or queries that search for existence of keys or values inside JSON documents.
Click to reveal answer
beginner
True or False: GIN indexes on JSONB columns can index nested keys and values inside the JSON structure.
True. GIN indexes can index nested keys and values, allowing efficient queries deep inside JSON documents.
Click to reveal answer
intermediate
What is a downside of using GIN indexes on JSONB columns?
GIN indexes can be slower to update when data changes, so write operations may be slower. Also, they use more disk space compared to regular indexes.
Click to reveal answer
Which SQL command creates a GIN index on a JSONB column named 'info' in table 'users'?
ACREATE INDEX idx_users_info ON users (info);
BCREATE INDEX idx_users_info ON users USING BTREE (info);
CCREATE INDEX idx_users_info ON users USING HASH (info);
DCREATE INDEX idx_users_info ON users USING GIN (info);
✗ Incorrect
GIN indexes are created using 'USING GIN' syntax. BTREE and HASH are different index types not optimized for JSONB.
What operator is commonly used in queries that benefit from a GIN index on JSONB?
A<>
B=
C@>
DLIKE
✗ Incorrect
The containment operator '@>' checks if the JSONB column contains a specified JSON value and is optimized by GIN indexes.
True or False: GIN indexes on JSONB columns can speed up searches for nested keys.
ATrue
BFalse
COnly for top-level keys
DOnly for arrays
✗ Incorrect
GIN indexes index nested keys and values, allowing efficient queries inside complex JSON structures.
What is a common trade-off when using GIN indexes on JSONB columns?
AFaster writes but slower reads
BSlower writes but faster reads
CNo difference in performance
DSlower reads and writes
✗ Incorrect
GIN indexes improve read query speed but can slow down write operations due to index maintenance.
Which PostgreSQL data type is required to use GIN indexing for JSON data?
AJSONB
BTEXT
CJSON (plain)
DVARCHAR
✗ Incorrect
GIN indexing works with the JSONB data type, which stores JSON in a binary format optimized for indexing.
Explain how a GIN index improves query performance on a JSONB column.
Think about how searching inside a big JSON document can be slow without an index.
You got /4 concepts.
Describe the trade-offs of using a GIN index on JSONB data in PostgreSQL.
Consider what happens when data changes and the index needs to be updated.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a GIN index on a JSONB column in PostgreSQL?
easy
A. To convert JSONB data into plain text for easier reading
B. To compress the JSONB data to save disk space
C. To automatically backup JSONB data to another table
D. To speed up queries that search for specific keys or values inside the JSONB data
Solution
Step 1: Understand GIN index purpose
GIN indexes are designed to speed up searches inside complex data types like JSONB by indexing keys and values.
Step 2: Identify the benefit for JSONB queries
Queries using operators like @> that check for existence of keys or values run faster with GIN indexes.
Final Answer:
To speed up queries that search for specific keys or values inside the JSONB data -> Option D
Quick Check:
GIN index = faster JSONB key/value search [OK]
Hint: GIN indexes speed up JSONB key/value searches [OK]
Common Mistakes:
Thinking GIN compresses JSONB data
Confusing GIN with backup or conversion tools
Assuming GIN indexes speed up all query types
2. Which of the following is the correct syntax to create a GIN index on a JSONB column named data in a table items?
easy
A. CREATE INDEX idx_data ON items USING GIN (data);
B. CREATE INDEX idx_data ON items USING BTREE (data);
C. CREATE INDEX idx_data ON items USING GIN (data (jsonb_path_ops));
D. CREATE INDEX idx_data ON items USING HASH (data);
Solution
Step 1: Recall correct GIN index syntax
The standard way to create a GIN index on a JSONB column is: CREATE INDEX index_name ON table_name USING GIN (column_name);
Step 2: Check options for correctness
CREATE INDEX idx_data ON items USING GIN (data); matches the correct syntax. CREATE INDEX idx_data ON items USING BTREE (data); uses BTREE which is not suitable for JSONB. CREATE INDEX idx_data ON items USING GIN (data (jsonb_path_ops)); has incorrect syntax (extra parentheses around jsonb_path_ops). CREATE INDEX idx_data ON items USING HASH (data); uses HASH which is not valid for JSONB indexing.
Final Answer:
CREATE INDEX idx_data ON items USING GIN (data); -> Option A
Quick Check:
Correct GIN index syntax = CREATE INDEX idx_data ON items USING GIN (data); [OK]
Hint: Use USING GIN with column name in parentheses [OK]
Common Mistakes:
Using BTREE or HASH instead of GIN
Incorrect syntax with extra keywords
Missing parentheses around column name
3. Given a table products with a JSONB column attributes and a GIN index on it, what will this query return?
SELECT * FROM products WHERE attributes @> '{"color": "red"}';
medium
A. All rows where the JSONB column contains the key "color" with value "red"
B. All rows where the JSONB column contains any key named "color"
C. All rows where the JSONB column contains the value "red" anywhere
D. Syntax error due to incorrect JSON format
Solution
Step 1: Understand the @> operator
The @> operator checks if the left JSONB contains the right JSONB as a subset, meaning the key-value pair must exist exactly.
Step 2: Analyze the query condition
The condition attributes @> '{"color": "red"}' means the JSONB column must have a key "color" with value "red".
Final Answer:
All rows where the JSONB column contains the key "color" with value "red" -> Option A
Quick Check:
@> means contains key-value pair exactly [OK]
Hint: Use @> to find exact key-value pairs in JSONB [OK]
Common Mistakes:
Thinking @> matches only keys, not values
Assuming it matches any occurrence of value
Confusing JSON syntax causing errors
4. You created a GIN index on a JSONB column but your queries are still slow. What is the most likely cause?
medium
A. The GIN index does not support JSONB data types
B. The index was created without specifying the JSONB column
C. The queries are not using the @> operator
D. The table has no rows
Solution
Step 1: Understand when GIN index helps
GIN indexes on JSONB columns speed up queries that use operators like @> which check for containment.
Step 2: Identify why queries remain slow
If queries do not use @> or other supported operators, the index won't be used, causing slow performance.
Final Answer:
The queries are not using the @> operator -> Option C
Quick Check:
GIN index helps only with supported operators [OK]
Hint: Use @> operator to leverage GIN index speedup [OK]
Common Mistakes:
Assuming GIN indexes speed up all JSONB queries
Not verifying query operator usage
Thinking GIN index doesn't support JSONB
5. You want to create a GIN index on a JSONB column info in table users to optimize queries checking for existence of specific keys only, ignoring values. Which index operator class should you use?
hard
A. jsonb_ops
B. jsonb_path_ops
C. btree_gin
D. hash_ops
Solution
Step 1: Understand GIN operator classes for JSONB
PostgreSQL provides two main operator classes for JSONB GIN indexes: jsonb_ops (default) indexes keys and values, and jsonb_path_ops indexes only keys for existence queries.
Step 2: Choose operator class for key existence queries
Since the goal is to optimize queries checking for key existence only, jsonb_path_ops is the best choice as it creates a smaller, faster index for this purpose.
Final Answer:
jsonb_path_ops -> Option B
Quick Check:
Key-only queries use jsonb_path_ops [OK]
Hint: Use jsonb_path_ops for key-existence queries [OK]
Common Mistakes:
Using default jsonb_ops which indexes values too
Choosing unrelated operator classes like btree_gin
Confusing hash_ops with GIN operator classes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.