Quick revision
The short answer
jsonb is the right tool for sparse or variable attributes, data passed through from other systems, and documents read and written as a whole. It hurts when it replaces columns we filter, join or constrain on: we lose types, constraints and foreign keys, the planner has poor statistics on values inside jsonb, and every update rewrites the whole, often TOASTed, document. Our review would recommend a hybrid: relational columns for keys, filters and joins, jsonb for the long tail, and generated columns or expression indexes for hot paths.
What to remember
In this answer, we reviewed the proposal to put all product attributes in one jsonb column. jsonb is right for sparse or variable attributes, pass-through data and documents handled as a whole. It hurts for core attributes: types, constraints and foreign keys disappear unless we add CHECK constraints or validation, the planner has poor statistics on values inside jsonb unless we add expression indexes or extended statistics, and updating one key rewrites the whole TOASTed document. The hybrid design keeps keys, filters and joins in columns, the long tail in jsonb, and uses generated columns or expression indexes for hot paths.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
jsonb is the right tool for sparse or variable attributes, data passed through from other systems, and documents read and written as a whole. It hurts when it replaces columns we filter, join or constrain on: we lose types, constraints and foreign keys, the planner has poor statistics on values inside jsonb, and every update rewrites the whole, often TOASTed, document. Our review would recommend a hybrid: relational columns for keys, filters and joins, jsonb for the long tail, and generated columns or expression indexes for hot paths.
Why this question is asked
Avoiding migrations is an attractive promise. A single attributes column means new product fields need no ALTER TABLE, no deployment coordination and no waiting. Many teams have made this choice and some regretted it two years later. Interviewers ask this design review question to see whether we can weigh flexibility against what the database stops doing for us. In this answer, we will see where jsonb fits well, what it costs in correctness, planning and update performance, and design a hybrid schema that keeps most of the flexibility.
1Where jsonb Is the Right Choice
jsonb fits data whose shape we do not control or that varies a lot between rows. Product attributes are a good example in part: a television has screen size and refresh rate, a shirt has fabric and sleeve length, and a catalogue of 300 categories might have thousands of distinct attributes, most of them empty for any given product. As columns, that would be a very wide, very sparse table that needs a migration for every new category.
It also fits data passed through from other systems, such as a supplier's raw feed we store for reference, and documents that the application always reads and writes as a whole, like a configuration blob. In these cases the database does not need to understand the inside of the value.
2Cost 1: Types, Constraints and Foreign Keys Disappear
With columns, the database guarantees that price is a numeric, that it is not null, that it is positive, and that category_id points to a real category. Inside jsonb, none of this exists unless we add it. A bug in one service can write "price": "12,99" as a string, or leave it out, and nothing stops it.
CHECK constraints can recover some safety, but they grow complex quickly, and there is no foreign key from a value inside a document to another table. Every such rule moves into application code, where each service must remember it.
3Cost 2: The Planner Cannot See Inside
PostgreSQL's planner chooses plans from statistics about each column: common values, histograms, distinct counts. For a jsonb column it keeps statistics on whole documents, which says almost nothing about how many products have brand = 'Acme'. So for a filter like attrs ->> 'brand' = 'Acme', it falls back to a fixed default estimate. If the real number is 200,000 rows and the planner guesses 1,500, it may choose a nested loop join that runs for minutes instead of a hash join that takes a second.
There are remedies. An expression index on the value makes ANALYZE collect statistics for that expression, and since PostgreSQL 14 extended statistics can be created on expressions without an index. But each one must be added deliberately for each important key, which is close to the migration work we were trying to avoid.
4Cost 3: Updating One Key Rewrites the Whole Document
jsonb values are immutable as stored. Changing one key, even with jsonb_set, produces a new complete document, and because of MVCC a new version of the whole row. If the document is larger than about 2 KB, PostgreSQL stores it compressed in a separate TOAST table, and an update must write a new TOAST value as well.
So updating stock_count, a single integer, inside a 20 KB attributes document every time an item sells writes over 20 KB of new data, plus WAL, plus dead tuples for vacuum. As a separate column it would write a small new row. There is a second cost: with a GIN index on the document, every change to it modifies an indexed value, so the update can never be a HOT update (one that stays on the same page and skips index maintenance), and every index on the table gets a new entry. With stock as a separate, unindexed column, the update can often stay HOT.
| Concern | Relational columns | Single jsonb column |
|---|---|---|
| Types and NOT NULL | enforced by the database | only via CHECK or application code |
| Foreign keys | yes | no |
| Planner statistics | per column, automatic | default guesses unless added per key |
| Update one attribute | small new row version | whole document rewritten, often TOASTed |
| Add a new attribute | migration | no migration |
| Sparse, varied attributes | wide, mostly empty table | natural fit |
5The Hybrid Design We Would Recommend
The review would not reject jsonb; it would put it in its place. Attributes used for identity, filtering, joining, sorting or constraints become real columns: sku, category_id, brand, price, status, stock. The long tail of category-specific attributes lives in jsonb. For a jsonb attribute that becomes popular in filters, we can expose it as a generated column or index it by expression, without moving the data.
- Columnskeys, filters, joins, constraints, frequent updates
- Generated columns and expression indexeshot paths into the document, typed
- jsonb attrssparse, category-specific long tail
- Application validationa schema per category for the jsonb part
4 layers, from the top down. Step through them one at a time.
Notice that stock is a column, so the frequent update stays small, and that screen_inches is derived from the document but typed and indexed. Adding a new long-tail attribute still needs no migration; promoting one to a column is a deliberate, reviewed step.
6Follow-up questions
How would you map this jsonb column in Hibernate 6?
Hibernate 6 supports JSON natively. We annotate the field with @JdbcTypeCode(SqlTypes.JSON), and it can be a Map<String, Object> or our own class, which Hibernate serialises with Jackson if it is on the classpath. With PostgreSQL the column maps to jsonb. The real columns map as normal fields, and a generated column is mapped read-only, for example with @Column(insertable = false, updatable = false) or @Generated, so Hibernate reads it after insert or update. Check your Hibernate version for the exact options.
Is an EAV table a better alternative?
Entity-attribute-value, a table of (product_id, name, value) rows, avoids migrations too, but it loses types just as badly, needs a join or pivot for every attribute and becomes very slow at scale. jsonb with GIN indexes is usually better for the long tail.
How do we keep jsonb documents consistent without database constraints?
Validate them in the application against a schema per category, version the document shape, and add a few CHECK constraints for the critical rules. Reject writes at the API boundary rather than discovering bad data in reports.
7Conclusion
In this answer, we reviewed the proposal to put all product attributes in one jsonb column. jsonb is right for sparse or variable attributes, pass-through data and documents handled as a whole. It hurts for core attributes: types, constraints and foreign keys disappear unless we add CHECK constraints or validation, the planner has poor statistics on values inside jsonb unless we add expression indexes or extended statistics, and updating one key rewrites the whole TOASTed document. The hybrid design keeps keys, filters and joins in columns, the long tail in jsonb, and uses generated columns or expression indexes for hot paths.
Open with
I would support jsonb for part of this but not all of it. jsonb is a good fit for sparse, category-specific attributes, data passed through from suppliers, and documents we read and write as a whole, because columns for all of that would be a huge, mostly empty table and constant migrations.
Then, if they want more
But moving everything into jsonb has real costs. First, we lose column types, NOT NULL, CHECK constraints and foreign keys unless we add CHECK constraints on the document or validate in code, so bad data like a price stored as a string gets in silently.
Second, the planner has very poor statistics on values inside jsonb, so filters on attributes get default estimates and can produce bad join plans; expression indexes or extended statistics on expressions help, but only per key we remember to add.
Third, updating one key rewrites the whole document, and large documents are TOASTed, so a frequently changing value like stock becomes expensive in writes, WAL and vacuum.
So I would propose a hybrid: real columns for identity, filtering, joins, constraints and frequently updated values, jsonb for the long tail with a GIN index, and generated columns or expression indexes for any jsonb attribute that becomes a hot path.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 14, Hibernate 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A team proposes storing all product attributes in a single jsonb column instead of...": https://scrivio-interview-library.netlify.app/databases/postgresql/acfc804070/