Bird
Raised Fist0
PostgreSQLquery~20 mins

GiST index for geometric and text in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
GiST Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of GiST index usage on geometric data

Consider a PostgreSQL table shapes with a box column storing geometric boxes. A GiST index is created on this column. What will be the output of the following query?

SELECT count(*) FROM shapes WHERE box && '((1,1),(5,5))'::box;

Assume the table has these rows:

  • box '((0,0),(2,2))'
  • box '((3,3),(6,6))'
  • box '((6,6),(8,8))'
PostgreSQL
CREATE TABLE shapes (id SERIAL PRIMARY KEY, box box);
INSERT INTO shapes (box) VALUES ('((0,0),(2,2))'), ('((3,3),(6,6))'), ('((6,6),(8,8))');
CREATE INDEX shapes_box_gist_idx ON shapes USING gist(box);

SELECT count(*) FROM shapes WHERE box && '((1,1),(5,5))'::box;
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

The && operator checks if two boxes overlap.

🧠 Conceptual
intermediate
1:30remaining
Understanding GiST index benefits for text search

Why is a GiST index useful for full-text search in PostgreSQL?

AIt automatically corrects spelling mistakes in queries.
BIt supports indexing complex data types like tsvector for fast text search.
CIt compresses text data to save disk space.
DIt stores full text documents directly for faster retrieval.
Attempts:
2 left
💡 Hint

Think about what GiST indexes can do with special data types.

📝 Syntax
advanced
2:00remaining
Correct GiST index creation for text search

Which of the following SQL statements correctly creates a GiST index on a tsvector column named document_vector in a table articles?

ACREATE INDEX articles_gist_idx ON articles USING gist(document_vector);
BCREATE INDEX articles_gist_idx ON articles USING btree(document_vector);
CCREATE INDEX articles_gist_idx ON articles USING gist(document_vector text_pattern_ops);
DCREATE INDEX articles_gist_idx ON articles USING hash(document_vector);
Attempts:
2 left
💡 Hint

GiST indexes require the gist keyword and do not use operator classes like text_pattern_ops for tsvector.

optimization
advanced
2:00remaining
Optimizing spatial queries with GiST index

You have a table locations with a point column storing coordinates. You want to find all points within a rectangle quickly. Which query will best use a GiST index on the point column?

ASELECT * FROM locations WHERE point <@ box '((1,1),(5,5))';
BSELECT * FROM locations WHERE point = '(3,3)';
CSELECT * FROM locations WHERE point && box '((1,1),(5,5))';
DSELECT * FROM locations WHERE point < box '((1,1),(5,5))';
Attempts:
2 left
💡 Hint

Look for the operator that checks if a point is inside a box.

🔧 Debug
expert
3:00remaining
Diagnosing GiST index usage issue on text search

A developer created a GiST index on a tsvector column content_vector in table documents. However, queries using to_tsvector('english', content) in the WHERE clause are slow and do not use the index. Why?

AThe <code>content_vector</code> column must be of type <code>text</code> for GiST index to work.
BGiST indexes do not support <code>tsvector</code> columns for text search.
CThe index must be created with the <code>GIN</code> method, not GiST, to be used.
DThe index is on <code>content_vector</code>, but the query uses <code>to_tsvector('english', content)</code> directly, so the index is not used.
Attempts:
2 left
💡 Hint

Check if the query expression matches the indexed column.

Practice

(1/5)
1. What is the main purpose of a GiST index in PostgreSQL?
easy
A. To enforce data integrity constraints
B. To store data in a compressed format
C. To backup the database automatically
D. To speed up searches on complex data types like geometric shapes and text

Solution

  1. Step 1: Understand GiST index purpose

    GiST indexes are designed to speed up searches on complex data types such as geometric shapes and full-text search data.
  2. Step 2: Compare options

    Options A, B, and D describe other database features unrelated to GiST indexes.
  3. Final Answer:

    To speed up searches on complex data types like geometric shapes and text -> Option D
  4. Quick Check:

    GiST index = speed up complex data search [OK]
Hint: GiST indexes speed up complex data searches like shapes and text [OK]
Common Mistakes:
  • Confusing GiST with data compression
  • Thinking GiST enforces constraints
  • Assuming GiST is for backups
2. Which of the following is the correct syntax to create a GiST index named places_location_gist on a column named location in table places?
easy
A. CREATE INDEX ON places USING gist (location);
B. CREATE INDEX places_location_gist ON places USING gist (location);
C. CREATE gist INDEX ON places (location);
D. CREATE INDEX places_location_gist ON gist (location);

Solution

  1. Step 1: Recall correct CREATE INDEX syntax

    The correct syntax is: CREATE INDEX index_name ON table_name USING gist (column_name);
  2. Step 2: Match options to syntax

    CREATE INDEX places_location_gist ON places USING gist (location); matches the correct syntax with index name, table, USING gist, and column.
  3. Final Answer:

    CREATE INDEX places_location_gist ON places USING gist (location); -> Option B
  4. Quick Check:

    CREATE INDEX ... ON table USING gist (column) [OK]
Hint: Remember: CREATE INDEX name ON table USING gist (column) [OK]
Common Mistakes:
  • Omitting index name
  • Placing USING gist in wrong position
  • Using 'CREATE gist INDEX' which is invalid
3. Given the table shapes with a box column of type box, and a GiST index created on box, what will the query below return?

SELECT * FROM shapes WHERE box && '(1,1),(4,4)'::box;

The operator && means "overlaps" for geometric types.
medium
A. All rows where the box overlaps the box from (1,1) to (4,4)
B. All rows where the box is exactly equal to (1,1),(4,4)
C. All rows where the box is contained inside (1,1),(4,4)
D. Syntax error due to wrong operator

Solution

  1. Step 1: Understand the && operator for box type

    The && operator checks if two boxes overlap in PostgreSQL geometric types.
  2. Step 2: Interpret the query condition

    The query selects rows where the box column overlaps the box defined by coordinates (1,1) and (4,4).
  3. Final Answer:

    All rows where the box overlaps the box from (1,1) to (4,4) -> Option A
  4. Quick Check:

    box && box = overlap check [OK]
Hint: && means overlap for geometric types in PostgreSQL [OK]
Common Mistakes:
  • Confusing overlap with equality
  • Thinking && means containment
  • Assuming syntax error with &&
4. You created a GiST index on a text column for full-text search but your queries are still slow. Which of the following is a likely cause?
medium
A. You created the index on the wrong table
B. GiST indexes do not support text search
C. You forgot to use the @@ operator in your WHERE clause
D. You need to vacuum the table before using the index

Solution

  1. Step 1: Understand GiST index usage for full-text search

    GiST indexes support full-text search but queries must use the @@ operator to use the index.
  2. Step 2: Analyze options

    The most likely cause is forgetting the @@ operator, which prevents index usage. GiST indexes do not support text search is false--GiST supports text search. Options A and D are possible but less directly related to index usage.
  3. Final Answer:

    You forgot to use the @@ operator in your WHERE clause -> Option C
  4. Quick Check:

    Full-text search needs @@ operator to use GiST index [OK]
Hint: Use @@ operator in WHERE to leverage GiST full-text index [OK]
Common Mistakes:
  • Assuming GiST doesn't support text search
  • Not using @@ operator in queries
  • Ignoring index table or vacuum issues
5. You want to create a GiST index on a table documents with a column content of type tsvector for fast full-text search. Which of the following statements correctly creates the index and allows efficient search for the phrase 'open source'?
hard
A. CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE content @@ to_tsquery('open & source');
B. CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE content LIKE '%open source%';
C. CREATE INDEX content_gist_idx ON documents USING btree (content); SELECT * FROM documents WHERE content @@ to_tsquery('open & source');
D. CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE to_tsvector(content) @@ to_tsquery('open & source');

Solution

  1. Step 1: Create GiST index on tsvector column

    The correct syntax is to create a GiST index on the tsvector column directly, as in CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE content @@ to_tsquery('open & source');
  2. Step 2: Use proper full-text search query

    CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE content @@ to_tsquery('open & source'); uses content @@ to_tsquery('open & source'), which is the correct way to search for both words with AND logic. CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE content LIKE '%open source%'; uses LIKE, which does not use the index. CREATE INDEX content_gist_idx ON documents USING btree (content); SELECT * FROM documents WHERE content @@ to_tsquery('open & source'); uses btree index, which is not suitable for tsvector. CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE to_tsvector(content) @@ to_tsquery('open & source'); applies to_tsvector on the column again, which is unnecessary and inefficient.
  3. Final Answer:

    CREATE INDEX content_gist_idx ON documents USING gist (content); SELECT * FROM documents WHERE content @@ to_tsquery('open & source'); -> Option A
  4. Quick Check:

    GiST index + tsvector column + @@ to_tsquery = efficient search [OK]
Hint: Index tsvector column and query with @@ and to_tsquery [OK]
Common Mistakes:
  • Using LIKE instead of @@ for full-text search
  • Creating btree index on tsvector column
  • Applying to_tsvector again in WHERE clause