agentleFS
iwritec0deiwritec0de/app-dev/skills/postgresql-expert/SKILL.md

postgresql-expert

Skill3 starsChanged 7 months agoMITView on GitHub

This skill should be used when the user asks to 'write a PostgreSQL query', 'configure postgresql.conf', 'use JSONB', 'set up a PG extension', 'tune Postgres performance', 'optimize a Postgres query', or mentions 'postgresql', 'postgres', 'pg_', 'jsonb', 'array type', 'CTE', 'window function', 'extension', 'postgresql.conf', 'pg_stat', 'vacuum', 'WAL'. Provides PostgreSQL-specific expertise for advanced SQL, types, extensions, and tuning.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

3 rules. About 0 are common, 0 are distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Read reference/advanced-sql.md for recursive CTEs, window functions, JSONB queries, and array operations.

    line 66
  2. Very specific:

    Read reference/extensions.md for setup guides and usage patterns.

    line 79
  3. Very specific:

    Read reference/tuning.md for WAL config, connection pooling, VACUUM strategies, and replication.

    line 93

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

PostgreSQL Expert Skill

You are a PostgreSQL expert specializing in PG-specific features, types, and tuning.

Critical Rules

  • Use JSONB not JSON — JSONB is binary, indexable, and queryable; JSON is just text storage
  • Use arrays for simple lists — prefer TEXT[] over junction tables for tags-like data
  • VACUUM ANALYZE after bulk ops — keep statistics and dead tuple counts current
  • Use pg_stat_statements — the single most important extension for query analysis
  • Prefer gen_random_uuid() — built-in since PG 13, no extension needed
  • CTEs are optimization fences in PG < 12 — use WITH ... AS MATERIALIZED/NOT MATERIALIZED in 12+
  • Use RETURNING — avoid separate SELECT after INSERT/UPDATE/DELETE

PG-Specific Types

TypeUse CaseExample
UUIDDistributed-safe primary keysgen_random_uuid()
JSONBFlexible/schemaless datadata->'key', data @> '{"a":1}'
TEXT[]Simple lists, tagsARRAY['a','b'], ANY(tags)
ENUMFixed small setsCREATE TYPE status AS ENUM (...)
TSTZRANGETime ranges[2024-01-01, 2024-12-31)
TSVECTORFull-text searchto_tsvector('english', body)
INET/CIDRIP addresses'192.168.1.0/24'::cidr

Advanced SQL

-- Recursive CTE: tree traversal
WITH RECURSIVE tree AS (
  SELECT id, name, parent_id, 0 AS depth FROM categories WHERE parent_id IS NULL
  UNION ALL
  SELECT c.id, c.name, c.parent_id, t.depth + 1 FROM categories c JOIN tree t ON c.parent_id = t.id
) SELECT * FROM tree;

-- Window function: running total
SELECT id, amount, SUM(amount) OVER (ORDER BY created_at) AS running_total FROM payments;

-- UPSERT with conflict handling
INSERT INTO settings (key, value) VALUES ('theme', 'dark')
ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = NOW();

-- LATERAL join: top-N per group
SELECT u.*, recent.* FROM users u
CROSS JOIN LATERAL (
  SELECT * FROM orders WHERE user_id = u.id ORDER BY created_at DESC LIMIT 3
) recent;

Read reference/advanced-sql.md for recursive CTEs, window functions, JSONB queries, and array operations.

Extensions

ExtensionPurposeSetup
pg_stat_statementsQuery performance analysisCREATE EXTENSION pg_stat_statements;
pg_trgmFuzzy text search, similarityCREATE INDEX ... USING gin (name gin_trgm_ops);
pgvectorAI embedding similarity searchCREATE INDEX ... USING ivfflat (embedding vector_cosine_ops);
pg_cronScheduled jobs inside PGSELECT cron.schedule('0 3 * * *', $$VACUUM$$);
PostGISGeospatial queriesST_DWithin(geom, point, 1000)
citextCase-insensitive textemail CITEXT UNIQUE

Read reference/extensions.md for setup guides and usage patterns.

Configuration Tuning

Key postgresql.conf settings (adjust for your RAM):

SettingDefaultRecommendation
shared_buffers128MB25% of RAM
work_mem4MB64-256MB (per operation)
effective_cache_size4GB75% of RAM
maintenance_work_mem64MB512MB-1GB
random_page_cost4.01.1 for SSDs

Read reference/tuning.md for WAL config, connection pooling, VACUUM strategies, and replication.

Monitoring

-- Slow queries (requires pg_stat_statements)
SELECT query, calls, mean_exec_time, total_exec_time
FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 20;

-- Table bloat / dead tuples
SELECT relname, n_dead_tup, last_autovacuum FROM pg_stat_user_tables
WHERE n_dead_tup > 1000 ORDER BY n_dead_tup DESC;

-- Active queries and locks
SELECT pid, state, query, wait_event_type FROM pg_stat_activity WHERE state != 'idle';

Related

  • reference/advanced-sql.md — Recursive CTEs, window functions, LATERAL, JSONB, arrays
  • reference/extensions.md — pg_trgm, pg_stat_statements, PostGIS, pgvector, pg_cron
  • reference/tuning.md — postgresql.conf tuning, PgBouncer, VACUUM, WAL, replication

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.