postgres
Skill33 starsChanged 22 days agoMITView on GitHub
Use when working with PostgreSQL specifically. Covers indexing, MVCC and vacuum, connection pooling, partitioning, JSONB, replication, and the operational realities that separate Postgres from generic SQL. Trigger on "postgres table is bloated", "autovacuum isn't keeping up", "set up pgbouncer", "add a column without locking the table". Do not use for slow queries on any engine — see the database-performance skill.
Files
PostgreSQL
Purpose
Operate PostgreSQL well: understand what MVCC costs you, why the table is bloated, why the connection count matters more than you think, and how to change a large table without locking it.
When to Use
- Designing schemas or indexes for Postgres.
- Diagnosing bloat, slow queries, or lock contention.
- Configuring connection pooling and replication.
- Running a migration on a large table without downtime.
Capabilities
- Index types: B-tree, GIN, GiST, BRIN, partial, expression, covering.
- MVCC, dead tuples, autovacuum tuning, and transaction ID wraparound.
- Connection pooling with PgBouncer and the pooling modes.
- Partitioning: declarative range and list partitions.
- JSONB indexing and query patterns.
- Streaming replication, replicas, and replication lag.
Inputs
- The schema, the table sizes, and the query patterns.
pg_stat_statementsoutput for the slow-query question.- Server configuration and Postgres version.
Outputs
- Indexes that match the access patterns, with no unused ones.
- Autovacuum settings appropriate to the write volume.
- Migrations that acquire only brief locks.
Workflow
- Find the real slow queries —
pg_stat_statementsordered by total time, not by mean. A 30ms query executed a million times costs more than a 4-second query run once. - Index for the access pattern — Equality columns first in a composite index, then the range or sort column. Add partial indexes for the filters that dominate.
- Watch the dead tuples — Every
UPDATEwrites a new row version and leaves the old one dead. A hot table with default autovacuum settings will bloat and slow down. - Pool the connections — Each Postgres connection is a process with real memory cost. Beyond a few hundred, performance degrades. PgBouncer in transaction mode is the standard answer.
- Migrate without locking —
CREATE INDEX CONCURRENTLY. Add columns nullable, backfill in batches, then set defaults and constraints withNOT VALIDfollowed byVALIDATE. - Set a lock timeout — Before any DDL:
SET lock_timeout = '3s'. A migration that waits behind a long transaction will queue every subsequent query behind itself.
Best Practices
ALTER TABLE ... ADD COLUMN ... NOT NULL DEFAULT <value>is safe on Postgres 11+ (no table rewrite). Adding aCHECKconstraint or a foreign key still requires a scan — useNOT VALID, thenVALIDATE CONSTRAINTseparately.- An unused index is a write-amplification tax paid on every insert and update.
pg_stat_user_indexesshows which have never been scanned. Drop them. - Autovacuum defaults are tuned for a small database. On a high-write table, lower
autovacuum_vacuum_scale_factorfor that table specifically. idle in transactionconnections hold locks and block vacuum indefinitely. Setidle_in_transaction_session_timeout.- PgBouncer in transaction mode breaks prepared statements,
LISTEN/NOTIFY, and session-levelSET. Know this before adopting it, not after. - Replicas serve read traffic but lag. A read-after-write on a replica will see stale data — route the read to the primary or accept it.
Examples
A migration on a large table that takes no meaningful lock:
-- Adding a NOT NULL column with a default: safe and instant on PG 11+.
ALTER TABLE orders ADD COLUMN currency text NOT NULL DEFAULT 'USD';
-- A foreign key normally takes a lock and scans the whole table. Split it:
ALTER TABLE orders
ADD CONSTRAINT orders_customer_fk
FOREIGN KEY (customer_id) REFERENCES customers(id)
NOT VALID; -- instant: only new rows are checked
ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_fk;
-- scans, but takes only a SHARE UPDATE
-- EXCLUSIVE lock: writes continue.
-- An index without blocking writes:
SET lock_timeout = '3s'; -- do not queue behind a long transaction
CREATE INDEX CONCURRENTLY idx_orders_customer_created
ON orders (customer_id, created_at DESC)
WHERE deleted_at IS NULL;Finding the queries and the indexes that matter:
-- The queries that actually consume the database, by total time.
SELECT
substring(query, 1, 80) AS query,
calls,
round(total_exec_time::numeric, 0) AS total_ms,
round(mean_exec_time::numeric, 2) AS mean_ms,
rows / GREATEST(calls, 1) AS avg_rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 15;
-- Indexes that have never been used: pure write overhead.
SELECT relname AS table, indexrelname AS index,
pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_stat_user_indexes
WHERE idx_scan = 0 AND indexrelid NOT IN (
SELECT conindid FROM pg_constraint WHERE contype IN ('p','u')
)
ORDER BY pg_relation_size(indexrelid) DESC;Notes
CREATE INDEX CONCURRENTLYcannot run inside a transaction block, and it can fail leaving an invalid index behind. Checkpg_index.indisvalidafterwards and drop any invalid index before retrying.- Transaction ID wraparound is the failure mode that takes a Postgres cluster fully offline. It only happens when autovacuum has been failing for a long time and being ignored — monitor
age(datfrozenxid). - JSONB is excellent for genuinely schemaless data and a poor substitute for columns. A JSONB field that every query filters on should be a column with an index.
Other versions21 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 21
- mfts/papermark★ 9.2ksame name
- papermark/papermark★ 9.2ksame name
- timescale/pg-aiguide★ 1.9ksame name
- planetscale/database-skills★ 711same name
- sanjay3290/ai-skills★ 424same name
- magnus919/agent-skills★ 162same name
- damusix/skills★ 61same name
- nahid-sparktales/agent-dispatcher★ 59same name
- digitalocean-labs/do-app-platform-skills★ 38same name
- mamamou/ai-coding-skills★ 16same name
11 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
Used alongsideProjects with it also use mysql, redis and 2 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
planetscaleSkillmysql
Plan and review MySQL/InnoDB schema, indexing, query tuning, transactions, and operations. Use when creating or modifying MySQL tables, indexes, or queries; diagnosing slow/locking behavior; planning migrations; or troubleshooting replication and connection issues. Load when using a MySQL database.
Also in 5 projects that use this
Sorcha-PlatformSkillredis
Implements Redis caching, session management, and distributed coordination for the Sorcha platform. Use when: Adding caching layers, token revocation, rate limiting, SignalR backplane, or distributed state.
Also in 3 projects that use this
OpenAISkillsentry
Use when the user asks to inspect Sentry issues or events, summarize recent production errors, or pull basic Sentry health data via the Sentry CLI; perform read-only queries using the `sentry` command.
Also in 3 projects that use this
OpenAISkilllinear
Manage issues, projects & team workflows in Linear. Use when the user wants to read, create or updates tickets in Linear.
Also in 3 projects that use this
In this repository137 other files it gives its agents.
- agent-instructionsSkill
- agent-memorySkill
- hooksSkill
- plugin-developmentSkill
- skill-authoringSkill
- skill-reviewSkill
- slash-commandsSkill
- subagentsSkill
- agent-designSkill
- context-engineeringSkill
- fine-tuningSkill
- llm-cost-optimizationSkill
- llm-evaluationSkill
- llm-integrationSkill
- mcp-serverSkill
- ml-pipelineSkill
- model-selectionSkill
- prompt-engineeringSkill
- ragSkill
- structured-outputSkill
- api-designSkill
- background-jobsSkill
- cachingSkill
- djangoSkill
- event-driven-architectureSkill
- fastapiSkill
- graphqlSkill
- microservicesSkill
- nestjsSkill
- realtime-websocketsSkill
- spring-bootSkill
- competitive-analysisSkill
- lead-researchSkill
- product-analysisSkill
- resumeSkill
- user-researchSkill
- database-performanceSkill
- data-modelingSkill
- data-qualitySkill
- pandasSkill
- sparkSkill
- brand-guidelinesSkill
- generative-artSkill
- themingSkill
- ui-reviewSkill
- visual-designSkill
- architecture-decisionsSkill
- bug-fix-protocolSkill
- cli-developmentSkill
- code-reviewSkill
- debuggingSkill
- design-patternsSkill
- domain-modelingSkill
- git-workflowSkill
- legacy-modernizationSkill
- refactoringSkill
- repository-explorationSkill
- simple-designSkill
- system-designSkill
- aws-cdkSkill
- The first 60 shown.
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
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.

