Blog
PostgreSQL JSONB: Flexible Schema, Permanent Query Bill
JSONB is useful when part of a record is sparse, external, or evolves independently. It is dangerous when a team uses one payload column to postpone every data-model decision.
Keep Core Questions Relational
If you filter, join, validate, or aggregate a field in normal business flows, a typed column is usually clearer.
Store payment status, merchant_id, amount, and timestamps as columns. Provider-specific metadata can live in JSONB when its shape varies.
create table payments (
id bigint primary key,
merchant_id bigint not null,
status text not null,
amount numeric(19,2) not null,
provider_metadata jsonb not null default '{}'::jsonb
);
Index The Query You Have
GIN indexes support useful containment and key operations but can be large and expensive to update. Expression indexes may be smaller for one hot path:
create index payments_provider_reference
on payments ((provider_metadata ->> 'reference'));
Now document expected type and missing-key behavior. JSON strings containing numbers do not behave like numeric columns.
Validation Did Not Disappear
The application still needs schema checks for important payloads. A malformed document may survive writes and fail six months later during reporting.
JSONB also changes migration style, not the need for migration. Backfill new shapes in batches, support old and new readers during transition, and measure index growth.
Use JSONB to contain real variability. Using it to avoid understanding the domain is hiding mess under one comfortable operator.