Extensions (pg_trgm, uuid-ossp, hstore) in PostgreSQL - Time & Space Complexity
When using PostgreSQL extensions like pg_trgm, uuid-ossp, and hstore, it's important to understand how their operations scale as data grows.
We want to know how the time to perform tasks changes when the amount of data increases.
Analyze the time complexity of the following example using pg_trgm for similarity search.
-- Enable pg_trgm extension
CREATE EXTENSION IF NOT EXISTS pg_trgm;
-- Create table with text column
CREATE TABLE products (id SERIAL PRIMARY KEY, name TEXT);
-- Create trigram index
CREATE INDEX trgm_idx ON products USING gin (name gin_trgm_ops);
-- Search for similar names
SELECT * FROM products WHERE name % 'apple';
This code sets up a trigram index and searches for names similar to 'apple'.
Look at what repeats when searching with pg_trgm:
- Primary operation: Comparing trigrams of the search word against indexed trigrams in the table.
- How many times: For each candidate row, the trigram similarity is checked, but the index reduces the number of rows scanned.
As the number of rows grows, the index helps keep searches fast by narrowing candidates.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 comparisons, quickly filtered by index |
| 100 | More comparisons, but index limits to a small subset |
| 1000 | Index still narrows search, so comparisons grow slowly |
Pattern observation: Thanks to the index, the search does not check every row, so operations grow slower than the total data size.
Time Complexity: O(k + log n)
This means the search time grows slowly as data grows, thanks to the index helping find matches quickly. Here, k is the number of candidates returned by the index.
[X] Wrong: "Using pg_trgm means every search checks all rows one by one."
[OK] Correct: The trigram index lets PostgreSQL skip most rows, so it does not scan the whole table each time.
Understanding how extensions like pg_trgm affect query speed shows you know how databases handle large data efficiently.
"What if we removed the trigram index? How would the time complexity of the similarity search change?"