PostgreSQL JSONB vs Relational: When to Use Each in Fintech
PostgreSQL JSONB vs Relational: When to Use Each in Fintech
PostgreSQL's JSONB support creates a genuine design question: should financial data live in relational tables or JSONB columns? The answer isn't one or the other — it's both, used appropriately.
What JSONB Excels At
JSONB shines for data that has variable structure. Payment method details are the canonical example — a credit card has different fields than a bank transfer, which has different fields than a digital wallet. Modeling this relationally requires either sparse columns or an EAV pattern, both of which are painful. JSONB stores exactly the fields that exist for each payment method, no more, no less.
What Relational Excels At
Relational tables excel for data that you query by value. Transaction amounts, dates, statuses — these are queried, sorted, filtered, and aggregated constantly. JSONB can do all of these things with GIN indexes, but the query patterns are more verbose and the query planner has less information to optimize with.
The Hybrid Approach
The pattern I've settled on: core transaction data lives in relational columns for query performance. Metadata and variable-structure data live in a JSONB column. The relational columns handle the hot path (list recent transactions, filter by status, sum by date range). The JSONB column handles the long tail (payment method details, gateway responses, custom fields per merchant). This gives you the best of both worlds — fast queries on the data you query most, flexibility for the data you don't.
$ /related
Financial Audit Logs: What to Record and What Regulators Want
A practical guide to building audit logging systems that satisfy PCI-DSS, SOC 2, and regional banking regulations without over-engineering.
PostgreSQL Partial Indexes Cut My Query Time by 60%
How a single partial index reduced query latency on a 50M-row payments table and what I learned about PostgreSQL query planning along the way.
Rate Limiting a Payment Gateway in Production
Lessons learned implementing rate limiting on a high-throughput payment gateway — the algorithms, the edge cases, and the production incidents.