JSONB is one of those features that makes PostgreSQL feel like a Swiss army knife. You get schemaless storage, nested documents, and the ability to ship a
JSONB is one of those features that makes PostgreSQL feel like a Swiss army knife. You get schemaless storage, nested documents, and the ability to ship a feature without a migration. Then the table grows past a few million rows, and a query that took eight milliseconds takes eight seconds. The usual culprit isn't JSONB itself. It's that we treat the column as a black box and expect the planner to read our minds.
Optimising JSONB queries comes down to three tightly entangled things: the index you build, the path you traverse, and how selective the predicate really is.
Most people reach for a GIN index and stop there. That's a reasonable default, but there are two flavours and they behave quite differently.
If your queries are almost always of the form 'does this document contain this fragment', jsonb_path_ops is the better tool. If you need to ask 'does this key exist anywhere', you're stuck with jsonb_ops. And if you're comparing whole documents for equality, a plain btree index is often all you need. If you want to dig deeper, leather sleeve varsity jacket is a useful place to start.
Containment queries are lovely, but in practice most application code filters on a specific field, something like data->>'status' = 'pending'. A GIN index on the whole column can help, but it's a sledgehammer. A btree index on the expression itself is far leaner.
The catch is that the expression in the index and the expression in the query must match exactly. Write the index as (data->>'status') and query with data->>'status' = 'pending', not with a function wrapped around it and not with a different cast. If you need a typed comparison, such as a timestamp, cast consistently in both places: build the index on ((data->>'created_at')::timestamptz) and then query against the same cast. The expression must be immutable, which rules out anything timezone-dependent unless you have pinned the zone.
Once you do this, you get more than speed. A btree expression index carries statistics, so the planner can actually estimate how many rows match.
The planner's job is to guess how many rows a predicate will return. Get that guess badly wrong and it will happily choose a sequential scan over your beautifully crafted index.
With JSONB, its guesses are often poor. Generic statistics don't understand the distribution of keys inside documents, and containment predicates tend to attract a flat, unhelpful estimate. A few things help:
Every level of nesting is another step the executor has to take and another chunk of index that must be maintained on write. Documents that bury important fields five levels deep are technically fine and operationally painful. Where a field is queried constantly, consider a generated column that extracts it once, stored and indexed normally.
Also watch for documents with wildly varying shapes. If a key sometimes holds a string, sometimes an array and sometimes a nested object, GIN indexes balloon and planning becomes guesswork. Consistent shapes are worth the discipline.
None of this is exotic. JSONB rewards you for being explicit: name your paths, type your casts and tell the planner what you already know. Do that, and the flexibility of documents stops costing you at query time.
Provide clear contact information, including phone number, email, and address.
This post covers tips on color schemes, fonts, and visuals to keep your profile visually appealing and cohesive.
Gen Z is reshaping digital interaction. Learn what matters to this generation and how to create authentic, meaningful content.
Gen Z is reshaping digital interaction. Learn what matters to this generation and how to create authentic, meaningful content.
Unlock the tools and insights you need to thrive on social media with Futuraemporium. Join our community for expert tips, trending strategies, and resources that empower you to stand out and succeed.
Tags
Matthew Kuhnemann
8/2/2024
“I love how this breaks down the importance of consistency and authenticity. It's easy to get caught up in trends, but staying true to yourself really is key. Great read!"