🛠️ Postgresql Table Design
PostgreSQLに特化したスキーマ設計において、ベストプラクティスやデータ型、インデックス、制約、パフォーマンスパターン、高度な機能などを網羅的に解説するSkill。
📜 元の英語説明(参考)
Design a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features
🇯🇵 日本人クリエイター向け解説
PostgreSQLに特化したスキーマ設計において、ベストプラクティスやデータ型、インデックス、制約、パフォーマンスパターン、高度な機能などを網羅的に解説するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o postgresql-table-design.zip https://jpskill.com/download/5236.zip && unzip -o postgresql-table-design.zip && rm postgresql-table-design.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/5236.zip -OutFile "$d\postgresql-table-design.zip"; Expand-Archive "$d\postgresql-table-design.zip" -DestinationPath $d -Force; ri "$d\postgresql-table-design.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
postgresql-table-design.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
postgresql-table-designフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-17
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
💬 こう話しかけるだけ — サンプルプロンプト
- › Postgresql Table Design を使って、最小構成のサンプルコードを示して
- › Postgresql Table Design の主な使い方と注意点を教えて
- › Postgresql Table Design を既存プロジェクトに組み込む方法を教えて
これをClaude Code に貼るだけで、このSkillが自動発動します。
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
PostgreSQL テーブル設計
コアとなるルール
- 参照テーブル(ユーザー、注文など)には主キー(PRIMARY KEY)を定義してください。時系列データ、イベントデータ、ログデータには常に必要とは限りません。使用する場合は、
BIGINT GENERATED ALWAYS AS IDENTITYを推奨します。グローバルな一意性や不透明性が必要な場合にのみUUIDを使用してください。 - データ冗長性と更新異常を排除するために、まず正規化(3NFまで)してください。結合パフォーマンスに問題があることが証明されている、測定可能でROIの高い読み取りの場合にのみ非正規化してください。時期尚早な非正規化はメンテナンスの負担を増やします。
- 意味的に必要な箇所にはすべてNOT NULLを追加し、一般的な値にはDEFAULTを使用してください。
- 実際にクエリするアクセスパスにインデックスを作成してください。主キー/ユニークキー(自動)、外部キー列(手動!)、頻繁なフィルタリング/ソート、結合キーなどです。
- イベント時間にはTIMESTAMPTZを、金額にはNUMERICを、文字列にはTEXTを、整数値にはBIGINTを、浮動小数点数にはDOUBLE PRECISION(または正確な10進数演算には
NUMERIC)を推奨します。
PostgreSQL の「落とし穴」
- 識別子: クォートなし → 小文字になります。クォートされた名前や大文字小文字が混在した名前は避けてください。慣例として、テーブル名/列名には
snake_caseを使用してください。 - ユニーク + NULL: UNIQUE は複数の NULL を許可します。NULL を1つに制限するには
UNIQUE (...) NULLS NOT DISTINCT(PG15+) を使用してください。 - 外部キーインデックス: PostgreSQL は外部キー列を自動インデックスしません。手動で追加してください。
- 暗黙の型変換なし: 長さ/精度オーバーフローはエラーになります(切り捨てなし)。例:
NUMERIC(2,0)に 999 を挿入すると、一部のデータベースのように黙って切り捨てたり丸めたりせず、エラーになります。 - シーケンス/IDENTITY にはギャップがあります(正常な動作であり、「修正」しないでください)。ロールバック、クラッシュ、同時実行トランザクションは、IDシーケンスにギャップ(1, 2, 5, 6...)を作成します。これは予期される動作であり、IDを連続させようとしないでください。
- ヒープストレージ: デフォルトではクラスタ化された主キーはありません(SQL Server/MySQL InnoDB とは異なります)。
CLUSTERは一度限りの再編成であり、その後の挿入では維持されません。ディスク上の行の順序は、明示的にクラスタ化されていない限り、挿入順序です。 - MVCC: 更新/削除はデッドタプルを残します。VACUUM がそれらを処理します。ホットなワイド行の頻繁な更新を避けるように設計してください。
データ型
- ID:
BIGINT GENERATED ALWAYS AS IDENTITYを推奨します(GENERATED BY DEFAULTも問題ありません)。マージ、フェデレーション、分散システムでの使用、または不透明なIDにはUUIDを使用してください。uuidv7()(PG18+ を使用している場合推奨) またはgen_random_uuid()(古い PG バージョンを使用している場合) で生成してください。 - 整数: ストレージ容量が非常に重要でない限り
BIGINTを推奨します。より小さい範囲にはINTEGERを、制約がない限りSMALLINTは避けてください。 - 浮動小数点数: ストレージ容量が非常に重要でない限り
REALよりもDOUBLE PRECISIONを推奨します。正確な10進数演算にはNUMERICを使用してください。 - 文字列:
TEXTを推奨します。長さ制限が必要な場合はVARCHAR(n)の代わりにCHECK (LENGTH(col) <= n)を使用し、CHAR(n)は避けてください。バイナリデータにはBYTEAを使用してください。大きな文字列/バイナリ(デフォルトのしきい値は2KB)は、圧縮されて自動的にTOASTに格納されます。TOASTストレージ:PLAIN(TOASTなし)、EXTENDED(圧縮 + 外部格納)、EXTERNAL(外部格納、圧縮なし)、MAIN(圧縮、可能であればインライン格納)。デフォルトのEXTENDEDが通常最適です。ALTER TABLE tbl ALTER COLUMN col SET STORAGE strategyとALTER TABLE tbl SET (toast_tuple_target = 4096)でしきい値を制御できます。大文字小文字を区別しない場合: ロケール/アクセント処理には非決定論的照合順序を使用し、プレーンなASCIIにはLOWER(col)の式インデックス(列に大文字小文字を区別しない主キー/外部キー/UNIQUEが必要な場合を除き推奨)またはCITEXTを使用してください。 - 金額:
NUMERIC(p,s)(浮動小数点数は絶対に使用しないでください)。 - 時間: タイムスタンプには
TIMESTAMPTZを、日付のみにはDATEを、期間にはINTERVALを使用してください。TIMESTAMP(タイムゾーンなし)は避けてください。トランザクション開始時刻にはnow()を、現在の実時間にはclock_timestamp()を使用してください。 - ブール値: 3値が必要な場合を除き、
NOT NULL制約付きのBOOLEANを使用してください。 - Enum: 小さく安定したセット(例: 米国の州、曜日)には
CREATE TYPE ... AS ENUMを使用してください。ビジネスロジックによって駆動され、進化する値(例: 注文ステータス)には、TEXT(または INT)+ CHECK またはルックアップテーブルを使用してください。 - 配列:
TEXT[]、INTEGER[]など。要素をクエリする順序付きリストに使用してください。包含 (@>,<@) および重複 (&&) クエリには GIN でインデックスを作成してください。アクセス:arr[1](1-インデックス)、arr[1:3](スライス)。タグ、カテゴリに適していますが、リレーションには避けてください。代わりに結合テーブルを使用してください。リテラル構文:'{val1,val2}'またはARRAY[val1,val2]。 - 範囲型:
daterange、numrange、tstzrangeは間隔に使用します。重複 (&&)、包含 (@>)、演算子をサポートします。GiST でインデックスを作成してください。スケジューリング、バージョン管理、数値範囲に適しています。境界スキームを選択し、一貫して使用してください。デフォルトでは[)(包含/排他的) を推奨します。 - ネットワーク型: IPアドレスには
INET、ネットワーク範囲にはCIDR、MACアドレスにはMACADDRを使用してください。ネットワーク演算子 (<<、>>、&&) をサポートします。 - 幾何型: 2D空間データには
POINT、LINE、POLYGON、CIRCLEを使用してください。GiST でインデックスを作成してください。高度な空間機能には PostGIS を検討してください。 - 全文検索: 全文検索ドキュメントには
TSVECTOR、検索クエリにはTSQUERYを使用してください。tsvectorには GIN でインデックスを作成してください。常に言語を指定してください:to_tsvector('english', col)とto_tsquery('english', 'query')。引数が1つのバージョンは絶対に使用しないでください。これはインデックス式とクエリの両方に適用されます。 - ドメイン型:
CREATE DOMAIN email AS TEXT CHECK (VALUE ~ '^[^@]+@[^@]+$')は、検証付きの再利用可能なカスタム型に使用します。テーブル間で制約を強制します。 - 複合型:
CREATE TYPE address AS (street TEXT, city TEXT, zip TEXT)は、列内の構造化データに使用します。(col).field構文でアクセスします。 - JSONB: JSON よりも推奨されます。GIN でインデックスを作成してください。オプション/半構造化属性にのみ使用してください。コンテンツの元の順序を必ず保持する必要がある場合にのみ JSON を使用してください。
- ベクトル型:
pgvectorによるvector型は、埋め込みのベクトル類似性検索に使用します。
以下のデータ型は使用しないでください
timestamp(タイムゾーンなし)は使用しないでください。代わりにtimestamptzを使用してください。char(n)またはvarchar(n)は使用しないでください。代わりにtextを使用してください。money型は使用しないでください。代わりにnumericを使用してください。- 使用しないでください
(原文はここで切り詰められています)
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
PostgreSQL Table Design
Core Rules
- Define a PRIMARY KEY for reference tables (users, orders, etc.). Not always needed for time-series/event/log data. When used, prefer
BIGINT GENERATED ALWAYS AS IDENTITY; useUUIDonly when global uniqueness/opacity is needed. - Normalize first (to 3NF) to eliminate data redundancy and update anomalies; denormalize only for measured, high-ROI reads where join performance is proven problematic. Premature denormalization creates maintenance burden.
- Add NOT NULL everywhere it’s semantically required; use DEFAULTs for common values.
- Create indexes for access paths you actually query: PK/unique (auto), FK columns (manual!), frequent filters/sorts, and join keys.
- Prefer TIMESTAMPTZ for event time; NUMERIC for money; TEXT for strings; BIGINT for integer values, DOUBLE PRECISION for floats (or
NUMERICfor exact decimal arithmetic).
PostgreSQL “Gotchas”
- Identifiers: unquoted → lowercased. Avoid quoted/mixed-case names. Convention: use
snake_casefor table/column names. - Unique + NULLs: UNIQUE allows multiple NULLs. Use
UNIQUE (...) NULLS NOT DISTINCT(PG15+) to restrict to one NULL. - FK indexes: PostgreSQL does not auto-index FK columns. Add them.
- No silent coercions: length/precision overflows error out (no truncation). Example: inserting 999 into
NUMERIC(2,0)fails with error, unlike some databases that silently truncate or round. - Sequences/identity have gaps (normal; don't "fix"). Rollbacks, crashes, and concurrent transactions create gaps in ID sequences (1, 2, 5, 6...). This is expected behavior—don't try to make IDs consecutive.
- Heap storage: no clustered PK by default (unlike SQL Server/MySQL InnoDB);
CLUSTERis one-off reorganization, not maintained on subsequent inserts. Row order on disk is insertion order unless explicitly clustered. - MVCC: updates/deletes leave dead tuples; vacuum handles them—design to avoid hot wide-row churn.
Data Types
- IDs:
BIGINT GENERATED ALWAYS AS IDENTITYpreferred (GENERATED BY DEFAULTalso fine);UUIDwhen merging/federating/used in a distributed system or for opaque IDs. Generate withuuidv7()(preferred if using PG18+) orgen_random_uuid()(if using an older PG version). - Integers: prefer
BIGINTunless storage space is critical;INTEGERfor smaller ranges; avoidSMALLINTunless constrained. - Floats: prefer
DOUBLE PRECISIONoverREALunless storage space is critical. UseNUMERICfor exact decimal arithmetic. - Strings: prefer
TEXT; if length limits needed, useCHECK (LENGTH(col) <= n)instead ofVARCHAR(n); avoidCHAR(n). UseBYTEAfor binary data. Large strings/binary (>2KB default threshold) automatically stored in TOAST with compression. TOAST storage:PLAIN(no TOAST),EXTENDED(compress + out-of-line),EXTERNAL(out-of-line, no compress),MAIN(compress, keep in-line if possible). DefaultEXTENDEDusually optimal. Control withALTER TABLE tbl ALTER COLUMN col SET STORAGE strategyandALTER TABLE tbl SET (toast_tuple_target = 4096)for threshold. Case-insensitive: for locale/accent handling use non-deterministic collations; for plain ASCII use expression indexes onLOWER(col)(preferred unless column needs case-insensitive PK/FK/UNIQUE) orCITEXT. - Money:
NUMERIC(p,s)(never float). - Time:
TIMESTAMPTZfor timestamps;DATEfor date-only;INTERVALfor durations. AvoidTIMESTAMP(without timezone). Usenow()for transaction start time,clock_timestamp()for current wall-clock time. - Booleans:
BOOLEANwithNOT NULLconstraint unless tri-state values are required. - Enums:
CREATE TYPE ... AS ENUMfor small, stable sets (e.g. US states, days of week). For business-logic-driven and evolving values (e.g. order statuses) → use TEXT (or INT) + CHECK or lookup table. - Arrays:
TEXT[],INTEGER[], etc. Use for ordered lists where you query elements. Index with GIN for containment (@>,<@) and overlap (&&) queries. Access:arr[1](1-indexed),arr[1:3](slicing). Good for tags, categories; avoid for relations—use junction tables instead. Literal syntax:'{val1,val2}'orARRAY[val1,val2]. - Range types:
daterange,numrange,tstzrangefor intervals. Support overlap (&&), containment (@>), operators. Index with GiST. Good for scheduling, versioning, numeric ranges. Pick a bounds scheme and use it consistently; prefer[)(inclusive/exclusive) by default. - Network types:
INETfor IP addresses,CIDRfor network ranges,MACADDRfor MAC addresses. Support network operators (<<,>>,&&). - Geometric types:
POINT,LINE,POLYGON,CIRCLEfor 2D spatial data. Index with GiST. Consider PostGIS for advanced spatial features. - Text search:
TSVECTORfor full-text search documents,TSQUERYfor search queries. Indextsvectorwith GIN. Always specify language:to_tsvector('english', col)andto_tsquery('english', 'query'). Never use single-argument versions. This applies to both index expressions and queries. - Domain types:
CREATE DOMAIN email AS TEXT CHECK (VALUE ~ '^[^@]+@[^@]+$')for reusable custom types with validation. Enforces constraints across tables. - Composite types:
CREATE TYPE address AS (street TEXT, city TEXT, zip TEXT)for structured data within columns. Access with(col).fieldsyntax. - JSONB: preferred over JSON; index with GIN. Use only for optional/semi-structured attrs. ONLY use JSON if the original ordering of the contents MUST be preserved.
- Vector types:
vectortype bypgvectorfor vector similarity search for embeddings.
Do not use the following data types
- DO NOT use
timestamp(without time zone); DO usetimestamptzinstead. - DO NOT use
char(n)orvarchar(n); DO usetextinstead. - DO NOT use
moneytype; DO usenumericinstead. - DO NOT use
timetztype; DO usetimestamptzinstead. - DO NOT use
timestamptz(0)or any other precision specification; DO usetimestamptzinstead - DO NOT use
serialtype; DO usegenerated always as identityinstead.
Table Types
- Regular: default; fully durable, logged.
- TEMPORARY: session-scoped, auto-dropped, not logged. Faster for scratch work.
- UNLOGGED: persistent but not crash-safe. Faster writes; good for caches/staging.
Row-Level Security
Enable with ALTER TABLE tbl ENABLE ROW LEVEL SECURITY. Create policies: CREATE POLICY user_access ON orders FOR SELECT TO app_users USING (user_id = current_user_id()). Built-in user-based access control at the row level.
Constraints
- PK: implicit UNIQUE + NOT NULL; creates a B-tree index.
- FK: specify
ON DELETE/UPDATEaction (CASCADE,RESTRICT,SET NULL,SET DEFAULT). Add explicit index on referencing column—speeds up joins and prevents locking issues on parent deletes/updates. UseDEFERRABLE INITIALLY DEFERREDfor circular FK dependencies checked at transaction end. - UNIQUE: creates a B-tree index; allows multiple NULLs unless
NULLS NOT DISTINCT(PG15+). Standard behavior:(1, NULL)and(1, NULL)are allowed. WithNULLS NOT DISTINCT: only one(1, NULL)allowed. PreferNULLS NOT DISTINCTunless you specifically need duplicate NULLs. - CHECK: row-local constraints; NULL values pass the check (three-valued logic). Example:
CHECK (price > 0)allows NULL prices. Combine withNOT NULLto enforce:price NUMERIC NOT NULL CHECK (price > 0). - EXCLUDE: prevents overlapping values using operators.
EXCLUDE USING gist (room_id WITH =, booking_period WITH &&)prevents double-booking rooms. Requires appropriate index type (often GiST).
Indexing
- B-tree: default for equality/range queries (
=,<,>,BETWEEN,ORDER BY) - Composite: order matters—index used if equality on leftmost prefix (
WHERE a = ? AND b > ?uses index on(a,b), butWHERE b = ?does not). Put most selective/frequently filtered columns first. - Covering:
CREATE INDEX ON tbl (id) INCLUDE (name, email)- includes non-key columns for index-only scans without visiting table. - Partial: for hot subsets (
WHERE status = 'active'→CREATE INDEX ON tbl (user_id) WHERE status = 'active'). Any query withstatus = 'active'can use this index. - Expression: for computed search keys (
CREATE INDEX ON tbl (LOWER(email))). Expression must match exactly in WHERE clause:WHERE LOWER(email) = 'user@example.com'. - GIN: JSONB containment/existence, arrays (
@>,?), full-text search (@@) - GiST: ranges, geometry, exclusion constraints
- BRIN: very large, naturally ordered data (time-series)—minimal storage overhead. Effective when row order on disk correlates with indexed column (insertion order or after
CLUSTER).
Partitioning
- Use for very large tables (>100M rows) where queries consistently filter on partition key (often time/date).
- Alternate use: use for tables where data maintenance tasks dictates e.g. data pruned or bulk replaced periodically
- RANGE: common for time-series (
PARTITION BY RANGE (created_at)). Create partitions:CREATE TABLE logs_2024_01 PARTITION OF logs FOR VALUES FROM ('2024-01-01') TO ('2024-02-01'). TimescaleDB automates time-based or ID-based partitioning with retention policies and compression. - LIST: for discrete values (
PARTITION BY LIST (region)). Example:FOR VALUES IN ('us-east', 'us-west'). - HASH: for even distribution when no natural key (
PARTITION BY HASH (user_id)). Creates N partitions with modulus. - Constraint exclusion: requires
CHECKconstraints on partitions for query planner to prune. Auto-created for declarative partitioning (PG10+). - Prefer declarative partitioning or hypertables. Do NOT use table inheritance.
- Limitations: no global UNIQUE constraints—include partition key in PK/UNIQUE. FKs from partitioned tables not supported; use triggers.
Special Considerations
Update-Heavy Tables
- Separate hot/cold columns—put frequently updated columns in separate table to minimize bloat.
- Use
fillfactor=90to leave space for HOT updates that avoid index maintenance. - Avoid updating indexed columns—prevents beneficial HOT updates.
- Partition by update patterns—separate frequently updated rows in a different partition from stable data.
Insert-Heavy Workloads
- Minimize indexes—only create what you query; every index slows inserts.
- Use
COPYor multi-rowINSERTinstead of single-row inserts. - UNLOGGED tables for rebuildable staging data—much faster writes.
- Defer index creation for bulk loads—>drop index, load data, recreate indexes.
- Partition by time/hash to distribute load. TimescaleDB automates partitioning and compression of insert-heavy data.
- Use a natural key for primary key such as a (timestamp, device_id) if enforcing global uniqueness is important many insert-heavy tables don't need a primary key at all.
- If you do need a surrogate key, Prefer
BIGINT GENERATED ALWAYS AS IDENTITYoverUUID.
Upsert-Friendly Design
- Requires UNIQUE index on conflict target columns—
ON CONFLICT (col1, col2)needs exact matching unique index (partial indexes don't work). - Use
EXCLUDED.columnto reference would-be-inserted values; only update columns that actually changed to reduce write overhead. DO NOTHINGfaster thanDO UPDATEwhen no actual update needed.
Safe Schema Evolution
- Transactional DDL: most DDL operations can run in transactions and be rolled back—
BEGIN; ALTER TABLE...; ROLLBACK;for safe testing. - Concurrent index creation:
CREATE INDEX CONCURRENTLYavoids blocking writes but can't run in transactions. - Volatile defaults cause rewrites: adding
NOT NULLcolumns with volatile defaults (e.g.,now(),gen_random_uuid()) rewrites entire table. Non-volatile defaults are fast. - Drop constraints before columns:
ALTER TABLE DROP CONSTRAINTthenDROP COLUMNto avoid dependency issues. - Function signature changes:
CREATE OR REPLACEwith different arguments creates overloads, not replacements. DROP old version if no overload desired.
Generated Columns
... GENERATED ALWAYS AS (<expr>) STOREDfor computed, indexable fields. PG18+ addsVIRTUALcolumns (computed on read, not stored).
Extensions
pgcrypto:crypt()for password hashing.uuid-ossp: alternative UUID functions; preferpgcryptofor new projects.pg_trgm: fuzzy text search with%operator,similarity()function. Index with GIN forLIKE '%pattern%'acceleration.citext: case-insensitive text type. Prefer expression indexes onLOWER(col)unless you need case-insensitive constraints.btree_gin/btree_gist: enable mixed-type indexes (e.g., GIN index on both JSONB and text columns).hstore: key-value pairs; mostly superseded by JSONB but useful for simple string mappings.timescaledb: essential for time-series—automated partitioning, retention, compression, continuous aggregates.postgis: comprehensive geospatial support beyond basic geometric types—essential for location-based applications.pgvector: vector similarity search for embeddings.pgaudit: audit logging for all database activity.
JSONB Guidance
- Prefer
JSONBwith GIN index. - Default:
CREATE INDEX ON tbl USING GIN (jsonb_col);→ accelerates:- Containment
jsonb_col @> '{"k":"v"}' - Key existence
jsonb_col ? 'k', any/all keys?\|,?& - Path containment on nested docs
- Disjunction
jsonb_col @> ANY(ARRAY['{"status":"active"}', '{"status":"pending"}'])
- Containment
- Heavy
@>workloads: consider opclassjsonb_path_opsfor smaller/faster containment-only indexes:CREATE INDEX ON tbl USING GIN (jsonb_col jsonb_path_ops);- Trade-off: loses support for key existence (
?,?|,?&) queries—only supports containment (@>)
- Equality/range on a specific scalar field: extract and index with B-tree (generated column or expression):
ALTER TABLE tbl ADD COLUMN price INT GENERATED ALWAYS AS ((jsonb_col->>'price')::INT) STORED;CREATE INDEX ON tbl (price);- Prefer queries like
WHERE price BETWEEN 100 AND 500(uses B-tree) overWHERE (jsonb_col->>'price')::INT BETWEEN 100 AND 500without index.
- Arrays inside JSONB: use GIN +
@>for containment (e.g., tags). Considerjsonb_path_opsif only doing containment. - Keep core relations in tables; use JSONB for optional/variable attributes.
- Use constraints to limit allowed JSONB values in a column e.g.
config JSONB NOT NULL CHECK(jsonb_typeof(config) = 'object')
Examples
Users
CREATE TABLE users (
user_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX ON users (LOWER(email));
CREATE INDEX ON users (created_at);
Orders
CREATE TABLE orders (
order_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(user_id),
status TEXT NOT NULL DEFAULT 'PENDING' CHECK (status IN ('PENDING','PAID','CANCELED')),
total NUMERIC(10,2) NOT NULL CHECK (total > 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX ON orders (user_id);
CREATE INDEX ON orders (created_at);
JSONB
CREATE TABLE profiles (
user_id BIGINT PRIMARY KEY REFERENCES users(user_id),
attrs JSONB NOT NULL DEFAULT '{}',
theme TEXT GENERATED ALWAYS AS (attrs->>'theme') STORED
);
CREATE INDEX profiles_attrs_gin ON profiles USING GIN (attrs);