Skip to content

SQL · performance

prefer-jsonb

sql:prefer-jsonb

JSON column type or ::json cast — use JSONB.

Code
SARJ106
Default
error
Fix
none
Languages
sql

Why

JSONB supports indexing and containment operators and avoids reparsing the stored document on every read.

Fix

Declare JSONB columns and use jsonb casts for JSON document values.

Before / after

Executed by this rule’s unit tests.

Before

Plain JSON column

migrations/001_documents.sql · focus
CREATE TABLE document (metadata JSON NOT NULL);

After

Indexable JSONB column

migrations/001_documents.sql · focus
CREATE TABLE document (metadata JSONB NOT NULL DEFAULT '{}'::jsonb);

Limits

  • PostgreSQL dump files are excluded.
  • JSON tokens inside comments, string literals, and longer identifiers are ignored.