[{"data":1,"prerenderedAt":28},["ShallowReactive",2],{"$f6VAMV3bz5aOcT7H6IZ3icOKkAaXp50T8UKfpRtHCf2I":3},{"slug":4,"title":5,"description":6,"date":7,"tags":8,"draft":12,"readingMinutes":13,"html":14,"related":15},"postgres-jsonb","Postgres JSONB: When I Use It and Which Index I Pick","PostgreSQL jsonb in practice on Postgres 18: json vs jsonb, containment queries, jsonb_ops vs jsonb_path_ops GIN indexes, and the update traps.","2026-11-17",[9,10,11],"postgres","jsonb","database",false,5,"\u003Cp>Postgres \u003Ccode>jsonb\u003C/code> is a column type that stores JSON in a parsed binary form, so Postgres can search inside it and index it. The plain \u003Ccode>json\u003C/code> type keeps your exact text and reparses it every time you touch it. If you&#39;re asking which one to use, it&#39;s \u003Ccode>jsonb\u003C/code>, and the only real reason to pick \u003Ccode>json\u003C/code> is when you need the original text back byte for byte, like a webhook body you&#39;ll verify a signature against later.\u003C/p>\n\u003Cp>Everything below is checked against the PostgreSQL 18 docs (18.7 came out on November 12, 2026; 19 is at its second release candidate).\u003C/p>\n\u003Ch2>What jsonb throws away\u003C/h2>\n\u003Cp>The \u003Ca href=\"https://www.postgresql.org/docs/current/datatype-json.html\">JSON types page\u003C/a> is clear about it: \u003Ccode>jsonb\u003C/code> &quot;does not preserve white space, does not preserve the order of object keys, and does not keep duplicate object keys. If duplicate keys are specified in the input, only the last value is kept.&quot;\u003C/p>\n\u003Cp>Key order bites people more than you&#39;d think. Somebody stores a settings object, reads it back, and the keys come out in a different order, and a snapshot test fails or a hash changes. Postgres never promised the order. That&#39;s the same webhook point as above: if anything downstream depends on the exact bytes, keep a \u003Ccode>text\u003C/code> or \u003Ccode>json\u003C/code> copy next to it.\u003C/p>\n\u003Ch2>Where it earns its place, and where it doesn&#39;t\u003C/h2>\n\u003Cp>I use \u003Ccode>jsonb\u003C/code> for data whose shape I don&#39;t control or that changes per row: payloads from a third-party API, per-customer settings, form answers where every form has different questions, feature flags, audit log &quot;before and after&quot; snapshots.\u003C/p>\n\u003Cp>I don&#39;t use it for anything I filter, join or sort on all the time. If every query has \u003Ccode>WHERE data-&gt;&gt;&#39;status&#39; = &#39;paid&#39;\u003C/code>, then \u003Ccode>status\u003C/code> wants to be a real column with a real type and a \u003Ccode>CHECK\u003C/code> or an enum. Real columns get proper statistics. The planner has nothing on the keys inside a \u003Ccode>jsonb\u003C/code> value unless you give it an expression index (or, since Postgres 14, extended statistics on the expression), so on a big table it guesses row counts for those filters and sometimes picks a terrible plan. It&#39;s a common path: an app starts with one \u003Ccode>data jsonb\u003C/code> column &quot;to move fast&quot;, and two years later every important field lives in there. Getting out of that is a migration project, not a refactor.\u003C/p>\n\u003Cp>A decent rule: if a key shows up in more than a couple of \u003Ccode>WHERE\u003C/code> clauses, promote it to a column.\u003C/p>\n\u003Ch2>Containment is the query to learn\u003C/h2>\n\u003Cp>The operator that makes \u003Ccode>jsonb\u003C/code> worth it is \u003Ccode>@&gt;\u003C/code>, &quot;contains&quot;:\u003C/p>\n\u003Cpre>\u003Ccode class=\"language-sql\">SELECT id FROM events\nWHERE payload @&gt; &#39;{&quot;type&quot;: &quot;invoice.paid&quot;, &quot;livemode&quot;: true}&#39;;\n\u003C/code>\u003C/pre>\n\u003Cp>It matches rows whose payload has at least those keys with those values, anywhere else can be anything. Arrays work too, and order doesn&#39;t matter: \u003Ccode>&#39;[1, 2, 3]&#39;::jsonb @&gt; &#39;[3, 1]&#39;\u003C/code> is true. Most &quot;find the rows where this nested thing equals that&quot; queries can be written as containment, and containment is what the GIN index is good at.\u003C/p>\n\u003Ch2>jsonb_ops or jsonb_path_ops\u003C/h2>\n\u003Cp>A GIN index on a \u003Ccode>jsonb\u003C/code> column comes in two flavours, and the default isn&#39;t always the one I want.\u003C/p>\n\u003Cpre>\u003Ccode class=\"language-sql\">-- default: jsonb_ops\nCREATE INDEX events_payload_gin ON events USING GIN (payload);\n\n-- smaller, faster for @&gt; but no key-exists operators\nCREATE INDEX events_payload_path ON events USING GIN (payload jsonb_path_ops);\n\u003C/code>\u003C/pre>\n\u003Cp>The default \u003Ccode>jsonb_ops\u003C/code> supports the key-exists operators \u003Ccode>?\u003C/code>, \u003Ccode>?|\u003C/code> and \u003Ccode>?&amp;\u003C/code>, plus \u003Ccode>@&gt;\u003C/code> and the jsonpath operators \u003Ccode>@?\u003C/code> and \u003Ccode>@@\u003C/code>. \u003Ccode>jsonb_path_ops\u003C/code> drops the key-exists ones and keeps the other three. The docs say a \u003Ccode>jsonb_path_ops\u003C/code> index &quot;is usually much smaller&quot; and searches &quot;typically perform better&quot;, especially when your queries use keys that appear in most rows.\u003C/p>\n\u003Cimg src=\"/blog/postgres-jsonb/jsonb-index-choice.webp\" alt=\"Which Postgres jsonb index to create, by the query you run: jsonb_path_ops, jsonb_ops, a GIN or a B-tree expression index\" width=\"1600\" height=\"900\" loading=\"lazy\">\u003Cp>So my default is \u003Ccode>jsonb_path_ops\u003C/code>, unless the app really asks &quot;does this key exist?&quot; with \u003Ccode>?\u003C/code>. There&#39;s one odd gap the docs mention: \u003Ccode>jsonb_path_ops\u003C/code> makes no index entries for structures with no values, like \u003Ccode>{&quot;a&quot;: {}}\u003C/code>, and searching for those becomes a full index scan. Querying for empty objects is rare enough that I accept that.\u003C/p>\n\u003Cp>Often you don&#39;t want either. If the query is always \u003Ccode>payload-&gt;&gt;&#39;customer_id&#39; = $1\u003C/code>, a plain B-tree on that expression is smaller than any GIN index and gives the planner statistics:\u003C/p>\n\u003Cpre>\u003Ccode class=\"language-sql\">CREATE INDEX events_customer ON events ((payload-&gt;&gt;&#39;customer_id&#39;));\n\u003C/code>\u003C/pre>\n\u003Cp>And if you only search inside one part of the document, index just that part. The docs&#39; own example is \u003Ccode>CREATE INDEX ... USING GIN ((jdoc -&gt; &#39;tags&#39;))\u003C/code>, which serves \u003Ccode>jdoc -&gt; &#39;tags&#39; ? &#39;qui&#39;\u003C/code>. A GIN index on the whole column can&#39;t help that query, because the \u003Ccode>?\u003C/code> isn&#39;t applied to the indexed column itself.\u003C/p>\n\u003Ch2>Updating one key rewrites the whole value\u003C/h2>\n\u003Cp>Postgres doesn&#39;t patch a \u003Ccode>jsonb\u003C/code> value in place. \u003Ccode>UPDATE ... SET payload = jsonb_set(payload, &#39;{status}&#39;, &#39;&quot;paid&quot;&#39;)\u003C/code> writes a new copy of the entire document, and values over about 2 kB get compressed and stored out of line (TOAST), so that write isn&#39;t free either. A 40 kB document with a counter inside it that you bump on every page view is a bad idea. Put the counter in its own column.\u003C/p>\n\u003Cp>Two smaller traps on the same line:\u003C/p>\n\u003Cul>\n\u003Cli>\u003Ccode>jsonb_set\u003C/code> is strict. Pass it a SQL \u003Ccode>NULL\u003C/code> as the new value and the whole result is \u003Ccode>NULL\u003C/code>, which means you just wiped the column. Use \u003Ccode>jsonb_set_lax\u003C/code> (Postgres 13+) or \u003Ccode>to_jsonb(...)\u003C/code> around the value, which turns a NULL into JSON \u003Ccode>null\u003C/code>.\u003C/li>\n\u003Cli>Subscript updates like \u003Ccode>SET payload[&#39;a&#39;][&#39;b&#39;] = &#39;1&#39;\u003C/code> are nice to read, but if any row has \u003Ccode>a\u003C/code> set to a string or a number, the statement raises an error and the transaction aborts. On a NULL column, subscripting creates \u003Ccode>{&quot;a&quot;: ...}\u003C/code> instead of failing, which is a different behaviour from \u003Ccode>jsonb_set\u003C/code>.\u003C/li>\n\u003C/ul>\n\u003Ch2>What I&#39;d actually do\u003C/h2>\n\u003Cp>Start with real columns for what you know, add one \u003Ccode>jsonb\u003C/code> column for what you don&#39;t, index it with \u003Ccode>jsonb_path_ops\u003C/code> once you&#39;re querying it with \u003Ccode>@&gt;\u003C/code>, and add B-tree expression indexes for the two or three keys you filter on every day. Then watch which keys keep showing up in \u003Ccode>WHERE\u003C/code> clauses and move them out.\u003C/p>\n\u003Cp>If you&#39;re on Supabase, all of this applies as-is, it&#39;s the same Postgres; I wrote about \u003Ca href=\"/blog/supabase-rls\">Supabase RLS\u003C/a> and \u003Ca href=\"/blog/postgres-full-text-search\">Postgres full text search\u003C/a> too. And if you have a table where \u003Ccode>jsonb\u003C/code> slowly took over the schema and queries crawl, \u003Ca href=\"https://calendly.com/director-norsky/meeting\">book a call\u003C/a>. The first conversation is free.\u003C/p>\n",[16,20,24],{"slug":17,"title":18,"description":19},"postgres-partitioning","Postgres Partitioning: When It Pays Off, and the Traps","Postgres partitioning checked against the PostgreSQL 18 docs: range, list and hash, the primary key rule, pruning, attaching without locks, and when not to bother.",{"slug":21,"title":22,"description":23},"postgres-full-text-search","Postgres Full Text Search: Enough for Most Apps","Postgres full text search checked on PostgreSQL 18: tsvector columns, GIN indexes, websearch_to_tsquery, ranking, ts_headline, typos with pg_trgm, and the limits.",{"slug":25,"title":26,"description":27},"postgres-vs-mongodb","Postgres vs MongoDB: Pick by the Questions You'll Ask Later","Postgres vs MongoDB in November 2026: Postgres 19 just shipped, MongoDB 8.3, JSONB vs documents, joins, transactions, licensing, and how I choose for a new app.",1795910047816]