What is PostgreSQL's JSONB Type

3 min read
TL;DR
  • A clear comparison of PostgreSQL JSON vs JSONB from performance, indexing, and ops perspectives.
  • Gives you defensible criteria for choosing the right document-storage strategy in real systems.
Cover

Before We Begin

JSONB is a way to store JSON data in PostgreSQL databases. PostgreSQL actually provides both json and jsonb, and they differ in storage format and query characteristics. When I first used PostgreSQL and encountered JSONB, I simply thought this unfamiliar type was the same as JSON and used it without much consideration. However, when problems arose during use, I organized the content I had to investigate.

JSON

JSON format is a text-based lightweight data exchange format. As a side note, when I first started developing about 10 years ago, data formats like SOAP and XML could often be seen, but now JSON has become commonplace.

JSON in Databases

Multiple databases have introduced JSON types. This is because, as mentioned above, the JSON format has become widespread, and demand for the ability to directly store and process JSON data has grown.

JSON type in MySQL: MySQL also provides a dedicated JSON type, but its implementation details differ from PostgreSQL's json and jsonb. In this post, I focus on the difference between PostgreSQL's two JSON-related types.

JSON / JSONB in PostgreSQL: PostgreSQL added json in version 9.2 and jsonb in version 9.4. json preserves the input text more closely, while jsonb stores a parsed binary representation.

  • Comparison Table
JSONJSONB
StoragePreserves the original textStores a parsed binary representation
Input costLowerHigher
Reparsing requiredYes for many operationsNo, it is already decomposed for processing
IndexingDoesn't support general GIN indexingSupported
Data processingSlower for most operatorsFaster for most operators
WhitespacesPreserves whitespace and key orderRemoves insignificant whitespace and does not preserve order
DuplicatesPreserved in the original textDuplicate keys collapse to the last value

The advantage is that indexing can be created on JSONB data, speeding up searches in large datasets. Since it is already parsed, many operators also work more efficiently on JSONB.

The disadvantage is that JSONB may incur additional input overhead during storage because of the internal conversion process. Also, the stored representation does not preserve whitespace or key order exactly as entered.

Data Comparison in JSONB

When comparing against a JSONB column, it is common to convert a string literal to jsonb first.

SELECT * 
FROM table_name 
WHERE jsonb_column_name = 'JSON_string_value'::jsonb;

To check if it matches the stored value, you need to compare after type conversion with ::jsonb as shown above.

More guides related to JSONB queries can be found in the PostgreSQL documentation and the link below.

Understanding how to handle JSON types in databases allows you to predict how they work and their performance differences.