redline
LowPerformancejsonb-hot-read

Reads one key out of a JSON column

Reads one key out of a JSON column. Pulls one key out of a JSON column (->>). On a hot path, Postgres reads (and detoasts) the whole document to get it.

This is how the comment reads inline, under the flagged line.

Why it matters

A large jsonb value is stored out of line. Reading one key decompresses all of it, so a query that returns a few KB can read gigabytes.

What to do

If this runs often, promote the key to a real column (or a generated column) and index it.

Examples

Flagged
SELECT data->>'status' FROM jobs WHERE id = $1
Better
SELECT status FROM jobs WHERE id = $1

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: Any file. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

db.query("SELECT data->>'status' FROM jobs");
db/q.sql
select data->>'k' from t;

Left alone

db.query("SELECT status FROM jobs");

Research

Reading one key out of a large JSON column on a hot path. The database decompresses the whole document to return one field.

  • owner's notes

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "jsonb-hot-read"
    ],
    "severity": {
      "jsonb-hot-read": "medium"
    }
  }
}

Read the source or report a false positive.