Postgresql

From iliaal/whetstone · View interactive version

Postgresql is an AI agent skill from the GitHub repository iliaal/whetstone. PostgreSQL schema design, query optimization, indexing, and administration. Use when writing schemas, queries, migrations, or mentions PostgreSQL, Postgres, JSONB, partitioning, RLS, CTEs, window functions, EXPLAIN ANALYZE, or connection pooling.

How to use this skill

Skill file

PostgreSQL

Data Type Defaults

| Need | Use | Avoid |

|------|-----|-------|

| Primary key | BIGINT GENERATED ALWAYS AS IDENTITY | SERIAL, BIGSERIAL |

| Timestamps | TIMESTAMPTZ | TIMESTAMP (loses timezone) |

| Text | TEXT | VARCHAR(n) unless constraint needed |

| Money | NUMERIC(precision, scale) | MONEY, FLOAT |

| Boolean | BOOLEAN with NOT NULL DEFAULT | nullable booleans |

| JSON | JSONB | JSON (no indexing), text JSON |

| UUID | gen_random_uuid() (PG13+) | uuid-ossp extension |

| IP addresses | INET / CIDR | text |

| Ranges | TSTZRANGE, INT4RANGE, etc. | pair of columns |

Schema Rules

Index Strategy

| Type | Use When |

|------|----------|

| B-tree (default) | Equality, range, sorting, LIKE 'prefix%' |

| GIN | JSONB (@>, ?, ?&), arrays, full-text (tsvector) |

| GiST | Geometry, ranges, full-text (smaller but slower than GIN) |

| BRIN | Large tables with natural ordering (timestamps, serial IDs) |

Index rules:

JSONB Patterns

-- GIN index for containment queries
CREATE INDEX ON items USING gin (metadata);
SELECT * FROM items WHERE metadata @> '{"status": "active"}';

-- Expression index for specific key access
CREATE INDEX ON items ((metadata->>'category'));
SELECT * FROM items WHERE metadata->>'category' = 'electronics';

Prefer typed columns over JSONB for frequently queried, well-structured data. Use JSONB for truly dynamic/variable attributes.

Query Optimization

Partitioning

Use when table exceeds ~100M rows or needs TTL purge:

Partition key must be in every unique/PK constraint. Create indexes on partitions, not parent.

Transactions & Locking

Full-Text Search

-- Weighted tsvector column with trigger
ALTER TABLE articles ADD COLUMN search_vector tsvector
  GENERATED ALWAYS AS (
    setweight(to_tsvector('english', coalesce(title,'')), 'A') ||
    setweight(to_tsvector('english', coalesce(body,'')), 'B')
  ) STORED;
CREATE INDEX ON articles USING gin (search_vector);
SELECT * FROM articles WHERE search_vector @@ websearch_to_tsquery('english', $1);

Performance Tuning

Key postgresql.conf parameters (adjust for available RAM):

Connection Pooling

Always pool in production. Direct connections cost ~10MB each.

Maintenance

Anti-Patterns

| Anti-Pattern | Fix |

|-------------|-----|

| SERIAL / BIGSERIAL for PKs | BIGINT GENERATED ALWAYS AS IDENTITY |

| No FK indexes | Add index on every FK column |

| OFFSET pagination | Cursor-based: WHERE id > $last |

| SELECT * | List needed columns |

| TIMESTAMP without timezone | TIMESTAMPTZ |

| Functions in WHERE (lower(col)) | Expression index or citext extension |

| Storing structured data as text | JSONB with GIN index |

| Long-running transactions | Keep txns short, use idle_in_transaction_session_timeout |

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.