Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Your company runs both PostgreSQL and MySQL 8.4 and teams want to store JSON in both. Compare JSON support relevant to performance and correctness, and set guidance.

Both engines store JSON in a binary form and neither enforces a schema by default, but PostgreSQL's jsonb has richer operators and GIN indexes, while MySQL indexes JSON through generated columns, functional indexes and multi-valued indexes and can update some values in place. My guidance would be the same on both: core data in relational columns, JSON only for extension attributes, an explicit index for every query path, size limits, and ORM mappings tested on each engine.

Why this question is asked

When a company runs two database engines, the same word can hide different behaviour. A team that learned JSON on PostgreSQL may assume a GIN index exists in MySQL, and a team coming from MySQL may assume small JSON updates are cheap in PostgreSQL. Interviewers ask this scenario to see whether we can compare the engines on the points that matter in production, performance and correctness, and turn that into guidance that teams can follow. In this answer, we will recap how each engine stores JSON, compare indexing, updates and validation, look at the Hibernate mapping that has to work on both, and finish with the rules I would publish.

1A Quick Recap of How Each Engine Stores JSON

PostgreSQL has jsonb, a parsed binary format. Keys are sorted for fast lookup, whitespace is gone, and duplicate keys keep the last value. A document larger than about 2 KB is compressed and moved out of the row into TOAST storage, a side table for large values. MySQL 8.4 has a single JSON type, also stored in a binary format that allows looking up a key without parsing the whole document. It also keeps the last duplicate key, and the size of one document is limited by max_allowed_packet.

So on storage the engines look alike. The differences show up when we query, index, update and validate, which is where the guidance has to be precise.

2Indexing: GIN Versus Generated Columns

PostgreSQL can index a whole jsonb column with GIN, which serves containment (@>), key-exists and jsonpath queries for any key. It can also index one extracted value with an expression B-tree index. MySQL has no index over a whole JSON document. Instead we index a value we extract, either through a generated column or a functional index, and for arrays MySQL 8.0.17 added multi-valued indexes, which store one index entry per array element.

Java
-- MySQL 8.4: index one scalar path through a generated column
ALTER TABLE orders
ADD COLUMN country CHAR(2) AS (doc->>'$.shipping.country') VIRTUAL,
ADD INDEX idx_country (country);
SELECT id FROM orders WHERE country = 'DE'; -- uses idx_country
-- MySQL 8.4: multi-valued index over an array of tags
ALTER TABLE orders ADD INDEX idx_tags ((CAST(doc->'$.tags' AS CHAR(32) ARRAY)));
SELECT id FROM orders WHERE 'gift' MEMBER OF (doc->'$.tags'); -- uses idx_tags
-- PostgreSQL 17: one GIN index serves containment on any path
CREATE INDEX orders_doc_gin ON orders USING gin (doc jsonb_path_ops);
SELECT id FROM orders WHERE doc @> '{"tags":["gift"]}'; -- uses orders_doc_gin

Notice what this means for teams. In PostgreSQL a GIN index can quietly cover queries nobody planned for. In MySQL every query path needs its own planned index, and the query must be written in the form the index expects (MEMBER OF, JSON_CONTAINS or JSON_OVERLAPS for multi-valued indexes). A functional index on a ->> expression in MySQL also needs care with the collation of the cast, so the safest habit is a generated column with an explicit type.

3Updates: In Place Versus a New Row Version

PostgreSQL uses MVCC, multi-version concurrency control: an UPDATE never changes a row in place, it writes a new version of the row. Changing one key with jsonb_set therefore writes the whole document again, and if the document was TOASTed, the whole TOASTed value is written again too. A counter kept inside a 50 KB document costs 50 KB of new data, plus WAL, per increment.

MySQL can perform a partial update of a JSON column in place when the statement uses JSON_SET, JSON_REPLACE or JSON_REMOVE on the same column and the new value is not larger than the old one. With binlog_row_value_options=PARTIAL_JSON, the binary log can also record only the change. That makes small edits to large documents cheaper, but only in those cases, so it is not something to build a design on without testing.

SQL
-- PostgreSQL: a new row version with the whole document
UPDATE orders SET doc = jsonb_set(doc, '{status}', '"shipped"') WHERE id = 7; -- UPDATE 1
-- MySQL: can be done in place, the new value is not longer than "packed"
UPDATE orders SET doc = JSON_SET(doc, '$.status', 'packed') WHERE id = 7; -- 1 row affected
JSON in PostgreSQL 17 and MySQL 8.4
PointPostgreSQL jsonbMySQL JSON
Whole-document indexGIN (jsonb_ops, jsonb_path_ops)none
Index on one pathexpression B-treegenerated column or functional index
Index on array elementsGIN with @>multi-valued index, MEMBER OF
Small update costnew row version, whole documentin place in some cases
Path languagejsonpath, SQL/JSON functions in 17JSON path, JSON_TABLE
Schema checkextension or applicationJSON_SCHEMA_VALID in CHECK

So the correctness is the same, but the cost is different. Frequently updated values should not live inside large documents on either engine, and in PostgreSQL that rule matters most.

4Correctness: Who Validates the Shape?

Neither engine checks the shape of a document by default. Any valid JSON is accepted. MySQL has JSON_SCHEMA_VALID, which checks a document against a JSON Schema, and since CHECK constraints are enforced from 8.0.16 we can put it in one. PostgreSQL has no built-in schema check; we can write CHECK constraints with jsonb_typeof and the ? operator for the essential rules, use an extension such as pg_jsonschema if our hosting allows it, or validate in the application.

Java
-- MySQL 8.4
ALTER TABLE orders ADD CONSTRAINT doc_shape CHECK (JSON_SCHEMA_VALID(
'{"type":"object","required":["v","currency"],
"properties":{"currency":{"type":"string","maxLength":3}}}', doc));
INSERT INTO orders (doc) VALUES ('{"v":1}');
-- ERROR 3819: Check constraint 'doc_shape' is violated.
-- PostgreSQL 17
ALTER TABLE orders ADD CONSTRAINT doc_shape
CHECK (doc ? 'v' AND doc ? 'currency' AND jsonb_typeof(doc->'currency') = 'string');

In practice I would validate in the application on both engines, because the same Java code can then use one schema for both databases, and keep a small database CHECK as the last line of defence.

5Testing the Hibernate Mapping on Both Engines

With Hibernate 6 and 7 we map a JSON column with @JdbcTypeCode(SqlTypes.JSON). Hibernate then uses jsonb on PostgreSQL and JSON on MySQL and serializes the field with Jackson when it is on the class path. The mapping looks portable, but the details are not: queries written as native SQL use different operators, and how a dialect binds the parameter can differ between versions, so check your version.

Java
@Entity
class Order {
@Id Long id;
@JdbcTypeCode(SqlTypes.JSON)
Map<String, Object> extra; // jsonb in PostgreSQL, JSON in MySQL
}

So the rule is to run the same repository tests against both engines with Testcontainers, including a native query per engine, before a team ships JSON on either one.

6The Guidance I Would Publish

Put core data in relational columns on both engines: anything joined, filtered, constrained or used for money. Use JSON for extension attributes and raw external payloads. Every query path on JSON must have an explicit, reviewed index, which in MySQL means a generated column or multi-valued index and in PostgreSQL an expression or GIN index. Set a size limit per document, for example 16 KB, because large documents hurt both engines and especially PostgreSQL updates. Validate with a JSON schema in the application, with a version key in each document. And test the ORM mapping and native queries on both engines in the build.

7Follow-up questions

Can we write one SQL query that works on both engines?

For simple extraction, the ->> operator exists in both but means different things: in PostgreSQL it takes a key, in MySQL it takes a JSON path such as '$.status'. JSON_TABLE and JSON_VALUE exist in both on PostgreSQL 17 and MySQL 8.4, so the standard functions are the most portable choice, though details still differ.

Why is the lack of statistics a problem on both?

Neither planner keeps statistics per key inside a document, so filters on extracted values get guessed selectivity. A generated column with an index gives MySQL real statistics, and an expression index or extended statistics on an expression does the same in PostgreSQL.

Does MySQL's partial update make it better for JSON?

Only for a narrow case: small changes that do not grow the value. It is a useful optimization, not a reason to keep frequently changing values inside documents.

8Conclusion

In this answer, we compared JSON in PostgreSQL and MySQL 8.4 on the points that matter in production. PostgreSQL's jsonb offers rich operators, jsonpath, SQL/JSON functions and GIN indexes, but every update writes a new row version. MySQL's binary JSON is indexed through generated columns, functional and multi-valued indexes, supports JSON_TABLE and some in-place partial updates. Neither enforces a schema by default, although MySQL can use JSON_SCHEMA_VALID in a CHECK. The guidance is the same on both: relational columns for core data, JSON for extensions, an index for every query path, size limits, and ORM mappings tested on each engine.

What to say in the interviewAbout 70 seconds aloud

Open with

I would compare them on indexing, updates and validation, because storage is similar: both keep a binary format and keep the last duplicate key. PostgreSQL jsonb has the richer toolkit: containment and key operators, jsonpath, the SQL JSON functions in 17, and GIN indexes that cover any path.

Then, if they want more

MySQL has no whole-document index, so we index extracted values through generated columns or functional indexes, and arrays through multi-valued indexes queried with MEMBER OF or JSON_CONTAINS. On updates, PostgreSQL always writes a new row version, so changing one key rewrites the whole document, while MySQL can do some partial updates in place when the new value is not larger.

Neither validates shape by default; MySQL can put JSON_SCHEMA_VALID in a CHECK constraint, PostgreSQL needs CHECKs with jsonb_typeof, an extension, or application validation.

My guidance would be: relational columns for core data, JSON for extension attributes, an explicit reviewed index for every query path, a size limit per document, schema validation with a version key in the application, and Hibernate mappings with JdbcTypeCode SqlTypes JSON tested against both engines in Testcontainers.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 8.4, PostgreSQL 17, Hibernate 6, MySQL 8.0
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.
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 "Your company runs both PostgreSQL and MySQL 8.4 and teams want to store JSON in both....": https://scrivio-interview-library.netlify.app/databases/advanced-sql/46a16f143b/