0
0
Supabasecloud~20 mins

Full-text search with Postgres in Supabase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Postgres Full-text Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
How does Postgres rank full-text search results?

Postgres uses a ranking function to order full-text search results. Which of the following best describes how the ts_rank function ranks documents?

AIt ranks documents based on the number of times the search term appears, giving higher weight to terms appearing earlier in the document.
BIt ranks documents randomly to distribute load evenly across results.
CIt ranks documents by the length of the document, preferring shorter documents regardless of term frequency.
DIt ranks documents alphabetically by the first word in the document.
Attempts:
2 left
💡 Hint

Think about how relevance is usually measured in search engines.

Configuration
intermediate
2:00remaining
Which SQL snippet creates a full-text search index correctly?

You want to create a full-text search index on the content column of a documents table. Which SQL command correctly creates a GIN index for this purpose?

ACREATE INDEX idx_content_fts ON documents USING HASH (content);
BCREATE INDEX idx_content_fts ON documents USING BTREE (to_tsvector('english', content));
CCREATE INDEX idx_content_fts ON documents USING GIN (to_tsvector('english', content));
DCREATE INDEX idx_content_fts ON documents (content);
Attempts:
2 left
💡 Hint

Consider which index type supports fast full-text search queries.

Architecture
advanced
2:00remaining
Best architecture for real-time full-text search updates?

You have a Postgres database with a large articles table. You want to keep the full-text search index updated in real-time as new articles are inserted or updated. Which architecture is best?

AUse triggers on the <code>articles</code> table to update a materialized view with the search vector, and refresh the view periodically.
BUse triggers to update a tsvector column in the same table on insert/update, and index that column with a GIN index.
CManually rebuild the full-text search index once a day during low traffic hours.
DUse an external search engine like Elasticsearch and sync data once a week.
Attempts:
2 left
💡 Hint

Think about minimizing delay and keeping search fast.

security
advanced
2:00remaining
Preventing SQL injection in full-text search queries

You build a search feature using Postgres full-text search. Users enter search terms that are included in SQL queries. Which practice best prevents SQL injection attacks?

AUse parameterized queries or prepared statements to pass user input safely to the <code>plainto_tsquery</code> function.
BEscape single quotes in user input manually before inserting into the query string.
CAllow only alphanumeric characters in user input by filtering out all others before query.
DUse string concatenation to build the query but limit input length to 100 characters.
Attempts:
2 left
💡 Hint

Think about how databases safely handle user input.

Best Practice
expert
2:00remaining
Optimizing full-text search performance on large datasets

You have a Postgres table with millions of rows and a full-text search index on a tsvector column. Queries are slow during peak hours. Which approach best improves performance without sacrificing result accuracy?

ARemove the <code>tsvector</code> column and compute it at query time to save storage space.
BSwitch the index type to GiST for faster writes, even if reads become slower.
CDisable the full-text search index and perform sequential scans with <code>to_tsvector</code> on the fly.
DUse a GIN index with the <code>fastupdate</code> option enabled and vacuum analyze regularly to keep statistics updated.
Attempts:
2 left
💡 Hint

Consider index maintenance and query speed trade-offs.