tsvector and tsquery types in PostgreSQL - Time & Space Complexity
We want to understand how the time to search text grows when using tsvector and tsquery types in PostgreSQL.
How does the search time change as the amount of text data increases?
Analyze the time complexity of the following PostgreSQL full-text search query.
SELECT * FROM documents
WHERE to_tsvector('english', content) @@ to_tsquery('english', 'search & query');
This query finds rows where the text content matches the search query using full-text search types.
Look for repeated work done by the query.
- Primary operation: Checking each document's tsvector against the tsquery.
- How many times: Once per document row scanned.
As the number of documents grows, the search checks more tsvectors.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of documents.
Time Complexity: O(n)
This means the search time grows linearly with the number of documents to check.
[X] Wrong: "Full-text search always runs instantly no matter how much data there is."
[OK] Correct: Without indexes, the search must check each document, so time grows with data size.
Understanding how full-text search scales helps you explain database performance clearly and confidently.
"What if we add a GIN index on the tsvector column? How would the time complexity change?"
Practice
tsvector type represent in PostgreSQL full-text search?Solution
Step 1: Understand
tsvectorroletsvectorstores processed text as lexemes for searching.Step 2: Differentiate from
tsquerytsqueryis the search pattern, not the stored text.Final Answer:
A searchable document converted into lexemes -> Option BQuick Check:
tsvector= searchable text [OK]
- Confusing tsvector with tsquery
- Thinking tsvector is a function
- Assuming tsvector stores raw text
tsvector matches a tsquery in PostgreSQL?Solution
Step 1: Recall operator for matching
The@@operator tests iftsvectormatchestsquery.Step 2: Confirm operand order
The syntax istsvector @@ tsquery, not reversed or using other operators.Final Answer:
tsvector @@ tsquery -> Option CQuick Check:
Match operator = @@ [OK]
tsvector @@ tsquery to test matches [OK]- Reversing operands in @@ operator
- Using = or LIKE instead of @@
- Confusing tsquery and tsvector roles
documents with a column content_tsv of type tsvector, what will this query return?SELECT id FROM documents WHERE content_tsv @@ to_tsquery('cat & dog');Solution
Step 1: Understand
The & operator means AND, so it searches for both words.to_tsquery('cat & dog')Step 2: Apply
The query returns rows where@@operatorcontent_tsvmatches both 'cat' and 'dog'.Final Answer:
Rows where content contains both 'cat' and 'dog' -> Option AQuick Check:
AND operator & means both words [OK]
- Thinking & means OR
- Assuming phrase match instead of AND
- Confusing tsquery syntax operators
SELECT * FROM articles WHERE to_tsquery('dog | cat') @@ content_tsv;Solution
Step 1: Check
The left operand must be@@operator usagetsvector, right operandtsquery.Step 2: Identify operand order in query
The query hasto_tsquery('dog | cat')on left (tsquery), which is incorrect.Final Answer:
The operands of @@ are reversed; tsvector must be on the left -> Option AQuick Check:
tsvector @@ tsquery order matters [OK]
- Swapping operands in @@ operator
- Misunderstanding tsquery syntax
- Casting tsvector to text unnecessarily
notes where the content column contains the word 'apple' but not 'pie'. Which tsquery expression correctly achieves this?SELECT * FROM notes WHERE to_tsvector(content) @@ to_tsquery( ? );
Solution
Step 1: Understand tsquery operators
& means AND, ! means NOT, | means OR.Step 2: Construct query for 'apple' AND NOT 'pie'
The correct expression is 'apple & !pie' to include 'apple' but exclude 'pie'.Final Answer:
'apple & !pie' -> Option DQuick Check:
AND + NOT = & ! [OK]
- Using | instead of & for AND
- Omitting & between terms
- Incorrect spacing or missing operators
Ready to prove it?
Pass the test and this pattern moves unknown
