Postgresql table design

From mtecoYY/ai-baike · View interactive version

Postgresql table design is an AI agent skill from the GitHub repository mtecoYY/ai-baike. Use this skill when designing or reviewing a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features

How to use this skill

Skill file

PostgreSQL Table Design

When to Use

The rules and decision points for a PostgreSQL schema. The full data-type catalog, workload

patterns (update-heavy, insert-heavy, upsert, schema evolution), extensions, JSONB indexing,

and worked DDL examples are in references/details.md; open it when a section below points there.

Core Rules

PostgreSQL Gotchas

Data Types

Types to avoid

| Avoid | Use instead |

|---|---|

| timestamp (without time zone) | timestamptz |

| char(n), varchar(n) | text (+ CHECK on length if needed) |

| money | numeric |

| timetz | timestamptz |

| timestamptz(0) or any precision | timestamptz |

| serial | generated always as identity |

Constraints

Indexing

Partitioning

Examples

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);
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 attributes with a generated, indexable scalar
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);

Going deeper

references/details.md holds the material this file only names:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.