PostgreSQL Advanced Features You Should Be Using
CTEs, window functions, JSONB operators, full-text search, and generated columns — the features that eliminate application-layer complexity.
PostgreSQL is far more powerful than most developers realize. Features like window functions, CTEs, JSONB operators, full-text search, and generated columns can replace hundreds of lines of application code with a single query — executing faster and with better data integrity. These aren't exotic features; they're production workhorses we use daily.
Window Functions
Window functions perform calculations across rows related to the current row without grouping them. They're essential for ranking, running totals, moving averages, and comparing rows. If you're fetching data and then processing it in application code to add rankings or running totals, window functions can do it in a single query.
-- Rank products by revenue within each category
SELECT
product_name,
category,
revenue,
RANK() OVER (PARTITION BY category ORDER BY revenue DESC) as category_rank,
revenue - LAG(revenue) OVER (PARTITION BY category ORDER BY revenue DESC) as gap_to_next,
SUM(revenue) OVER (PARTITION BY category) as category_total,
ROUND(revenue * 100.0 / SUM(revenue) OVER (PARTITION BY category), 1) as pct_of_category
FROM products
WHERE revenue > 0;JSONB: The Best of Both Worlds
PostgreSQL's JSONB type gives you the flexibility of a document database with the reliability of a relational database. Use it for semi-structured data — user preferences, API responses, event metadata, form submissions — where the schema varies between records but you still want to query and index the data.
-- Store flexible metadata alongside structured data
CREATE TABLE events (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
event_type TEXT NOT NULL,
user_id BIGINT REFERENCES users(id),
metadata JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ DEFAULT now()
);
-- Index specific JSONB paths for fast querying
CREATE INDEX idx_events_metadata_source ON events USING GIN ((metadata -> 'source'));
-- Query JSONB fields
SELECT * FROM events
WHERE metadata @> '{"source": "mobile", "version": "2.1"}'
AND metadata ->> 'campaign_id' IS NOT NULL;Common Table Expressions (CTEs)
CTEs (WITH clauses) make complex queries readable by breaking them into named, composable steps. Recursive CTEs are particularly powerful for traversing tree structures (org charts, category hierarchies, threaded comments) without application-layer recursion.
Full-Text Search
PostgreSQL's built-in full-text search handles most search use cases without adding Elasticsearch to your stack. It supports stemming, ranking, phrase matching, and fuzzy search with GIN indexes. For 90% of applications, it's more than sufficient — and it's one less service to deploy, monitor, and maintain.
Before adding a new dependency (Redis for caching, Elasticsearch for search, application code for data processing), check if PostgreSQL can do it natively. It almost always can, and keeping logic in the database means better performance and fewer moving parts.
PostgreSQL's advanced features are well-documented, production-tested, and significantly underused. Investing a few hours learning window functions, JSONB operators, and full-text search will save you weeks of application code over the lifetime of your project.
Sarah Chen
Cloud Infrastructure Architect