On this page
  1. JSONB, Arrays & Advanced Types
  2. Contents
  3. JSONB vs. JSON & Containment (@>)
  4. Native Arrays, UUID, TIMESTAMPTZ & Range Types

JSONB, Arrays & Advanced Types

Prev: MVCC, VACUUM & WAL | Up: postgresql | Next: Indexes & EXPLAIN ANALYZE
Rel: SQLite JSON & FTS5, MongoDB BSON Documents
Ref: PostgreSQL — JSON Types
Tags: #public #databases #postgresql #jsonb #types


Contents


JSONB vs. JSON & Containment (@>)

Prefer JSONB over JSON: it stores parsed binary JSON, strips duplicate keys, supports GIN indexing, and enables fast containment (@>) and JSONPath queries:

CREATE TABLE events (
  id         UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  occurred   TIMESTAMPTZ NOT NULL DEFAULT now(),
  tags       TEXT[] NOT NULL DEFAULT '{}',
  metadata   JSONB NOT NULL DEFAULT '{}'::jsonb
);

CREATE INDEX idx_events_meta_gin ON events USING GIN (metadata jsonb_path_ops);

SELECT id FROM events
WHERE metadata @> '{"env": "prod", "region": "us-west"}'::jsonb
  AND 'billing' = ANY(tags);

Native Arrays, UUID, TIMESTAMPTZ & Range Types

Linked from

Related notes