postgres-patterns
Skill1 starsChanged 56 days agoMITView on GitHub
PostgreSQL working reference for index selection, data types, row-level security, cursor pagination, queue processing, anti-pattern detection queries, server configuration, replica routing, alert thresholds, backups, and erasure. Use when you say "which index does this query need", "why is this query slow", "write an RLS policy for this table", "should this be numeric or float", or "find our unindexed foreign keys". Not for applying a schema change safely, use `database-migrations`.
28 other projects use a version of this, 25 of them edited.
Adopted by
Most recently taken up by
- productionx-in/Core-Skillsedited, first seen Sep 29, 2026
- templetongroup/radiantedited, first seen Sep 17, 2026
- socialadsmentor/sam-agentic-harnessedited, first seen Sep 3, 2026
- pekral/cursor-rulestheir own version, first seen Aug 5, 2026
- SamuelNDCE/claude-super-skill-libraryedited, first seen Aug 1, 2026
- Lukasbrujula/claude-code-frameworkedited, first seen Jul 6, 2026
Used with
Projects with it also use kotlin-testing, content-hash-cache-pattern and 8 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.
tpavanipradeepSkillkotlin-testing
Kotlin testing patterns with Kotest, MockK, coroutine testing, property-based testing, and Kover coverage. Follows TDD methodology with idiomatic Kotlin practices.
Also in 6 projects that use this
InfopibeSkillcontent-hash-cache-pattern
Cache expensive file processing results using SHA-256 content hashes — path-independent, auto-invalidating, with service layer separation.
Also in 12 projects that use this
mryeastcodeSkillnuxt4-patterns
Nuxt 4 app patterns for hydration safety, performance, route rules, lazy loading, and SSR-safe data fetching with useFetch and useAsyncData.
Also in 5 projects that use this
aragaobrunoSkillvite-patterns
Vite build tool patterns including config, plugins, HMR, env variables, proxy setup, SSR, library mode, dependency pre-bundling, and build optimization. Activate when working with vite.config.ts, Vite plugins, or Vite-based projects.
Also in 5 projects that use this
InfopibeSkillralphinho-rfc-pipeline
RFC-driven multi-agent DAG execution pattern with quality gates, merge queues, and work unit orchestration.
Also in 9 projects that use this
saxon-ySkillconfigure-ecc
Interactive installer for Everything Claude Code — guides users through selecting and installing skills and rules to user-level or project-level directories, verifies paths, and optionally optimizes installed files.
Also in 10 projects that use this
- +3 more from Infopibe
Other versions28 projects use a version of this.
Word for word · 0
None found.
Edited · 25
- xu-xiang/everything-claude-code-zh★ 2k43% same
- loulanyue/awesome-claude-notes★ 27245% same
- tpavanipradeep/everything-claude-code★ 13445% same
- templetongroup/radiant★ 11343% same
- Jamkris/everything-gemini-code★ 8744% same
- tranhieutt/software_development_department★ 7241% same
- codelably/harmony-claude-code★ 4343% same
- joneqian/claude-skills-suite★ 3244% same
- skateddu/claude-code-python-setup★ 2845% same
- FaisalAlqarni/sp-ecc★ 944% same
15 more not listed
Wrote their own version · 3
- noah-sheldon/ai-dev-kit★ 13same name
- pekral/cursor-rules★ 10same name
- diego-tobalina/vibe-coding★ 1same name
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.
In this repository59 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .claude/CLAUDE.mdCLAUDE.md
- agentic-engineeringSkill
- ai-regression-testingSkill
- angularSkill
- ansibleSkill
- api-designSkill
- architecture-decision-recordsSkill
- ascend-memorySkill
- ascend-web-hunterSkill
- audio-scribeSkill
- automation-inventorySkill
- backend-patternsSkill
- bashSkill
- build-dependency-managementSkill
- code-formatterSkill
- code-reviewerSkill
- coding-standardsSkill
- dart-flutter-patternsSkill
- database-migrationsSkill
- deployment-patternsSkill
- design-systemSkill
- docker-patternsSkill
- e2e-runbooksSkill
- e2e-testingSkill
- embedded-c-arduinoSkill
- frontend-designSkill
- g-code-3d-printingSkill
- github-opsSkill
- git-workflowSkill
- golang-patternsSkill
- hexagonal-architectureSkill
- home-assistantSkill
- java-coding-standardsSkill
- jetbrains-ide-opsSkill
- jira-integrationSkill
- keycloak-patternsSkill
- kicadSkill
- markdown-writerSkill
- mongodb-patternsSkill
- nextjs-app-router-patternsSkill
- node-backend-patternsSkill
- observability-and-loggingSkill
- obsidianSkill
- performance-optimizationSkill
- powershellSkill
- project-trackingSkill
- python-patternsSkill
- pytorch-patternsSkill
- react-patternsSkill
- researchSkill
- review-duplicationSkill
- security-reviewSkill
- seoSkill
- soap-webservicesSkill
- springboot-patternsSkill
- tdd-workflowSkill
- unitySkill
- web-accessibilitySkill
The file
We split this skill into the rules it gives an agent
1 rule. 0 are common, 0 are distinctive, and 1 is very specific to this project.
0
Common
Most of the 3 edited versions we read keep these.
0
Distinctive
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 254
Never hard-delete before checking cascade behaviour and audit-log retention, because a DELETE that cascades into an audit trail destroys …
“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
PostgreSQL Patterns
A working reference for PostgreSQL decisions that come up while writing queries and shaping schemas: which index, which
type, which lock, and which query will tell you what the database is actually doing. For a full review of an existing
data layer, hand the work to the database-expert subagent.
Baseline version, current as of September 2026: PostgreSQL 18. PostgreSQL 14 is the oldest release still supported, until November 12, 2026, so treat anything older as a version to upgrade rather than a version to target.
Based on Supabase Agent Skills (credit: Supabase team), MIT License.
When to activate
- Choosing an index for a query, or deciding whether one is even needed.
- Picking a column type for identifiers, money, timestamps, or flags.
- Writing or reviewing a row-level security policy.
- Diagnosing a slow query, table bloat, or a missing foreign-key index.
- Setting connection limits, statement timeouts, or monitoring thresholds.
When not to activate
- Applying a schema change safely on a live table: use
database-migrations. - JPA and Hibernate entity mapping and fetch strategy: use
springboot-patterns. - MongoDB modelling and indexing: use
mongodb-patterns. - Application-level caching, retries, and layering: use
backend-patterns. - Profiling an endpoint end to end before blaming the database: use
performance-optimization.
Pick the index that matches the predicate
| Query pattern | Index type | Example |
WHERE col = value | B-tree, the default | CREATE INDEX idx ON t (col) |
WHERE col > value | B-tree | CREATE INDEX idx ON t (col) |
WHERE a = x AND b > y | Composite | CREATE INDEX idx ON t (a, b) |
WHERE jsonb_col @> '{}' | GIN | CREATE INDEX idx ON t USING gin (col) |
WHERE tsv @@ query | GIN | CREATE INDEX idx ON t USING gin (col) |
| Time-series range scans on an append-only table | BRIN | CREATE INDEX idx ON t USING brin (col) |
Pass: equality columns first in a composite index, then the range column.
CREATE INDEX idx_orders_status_created ON orders (status, created_at);Fail: CREATE INDEX idx ON orders (created_at, status) for WHERE status = 'pending' AND created_at > $1, which
cannot use the leading column for the equality and scans far more of the index than it needs.
Two variants worth reaching for:
CREATE INDEX idx_users_email_cover ON users (email) INCLUDE (name, created_at);CREATE INDEX idx_users_email_active ON users (email) WHERE deleted_at IS NULL;The first avoids a heap lookup when the query selects only the included columns. The second indexes only live rows, so it stays small on a table dominated by soft-deleted ones.
Choose the type that cannot be wrong
| Use case | Correct type | Avoid |
| Identifiers | bigint, or uuid v7 when it must be client-generated | int, random uuid v4 as a primary key |
| Strings | text | varchar(255) |
| Timestamps | timestamptz | timestamp |
| Money | numeric(12,2), or an integer count of minor units | float, real |
| Flags | boolean | varchar, int |
Pass: amount_minor bigint NOT NULL for money, so no rounding exists to get wrong.
Fail: price float, which cannot represent 0.1 exactly and silently drifts across arithmetic.
varchar(n) buys no performance over text in PostgreSQL, and the length limit becomes a migration the first time the
business changes its mind.
Wrap the auth call in a row-level security policy
-- PASS: the subquery is evaluated once per statement
CREATE POLICY orders_owner ON orders
USING ((SELECT auth.uid()) = user_id);
-- FAIL: the function is re-evaluated for every row scanned
CREATE POLICY orders_owner ON orders
USING (auth.uid() = user_id);Index the column the policy filters on, or the policy turns every read into a sequential scan.
Use the right statement for the job
Upsert without a race between the check and the insert:
INSERT INTO settings (user_id, key, value)
VALUES (123, 'theme', 'dark')
ON CONFLICT (user_id, key) DO UPDATE SET value = EXCLUDED.value;Paginate by cursor, which is constant cost, rather than by offset, which is linear in the rows skipped:
SELECT * FROM products WHERE id > $1 ORDER BY id LIMIT 20;Claim a queue row without two workers taking the same one:
UPDATE jobs SET status = 'processing'
WHERE id = (
SELECT id FROM jobs WHERE status = 'pending'
ORDER BY created_at LIMIT 1
FOR UPDATE SKIP LOCKED
)
RETURNING *;Detect the common anti-patterns with a query
Unindexed foreign keys, which make every parent delete scan the child table:
SELECT conrelid::regclass, a.attname
FROM pg_constraint c
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY(c.conkey)
WHERE c.contype = 'f'
AND NOT EXISTS (
SELECT 1 FROM pg_index i
WHERE i.indrelid = c.conrelid AND a.attnum = ANY(i.indkey)
);Slow statements by mean execution time:
SELECT query, mean_exec_time, calls
FROM pg_stat_statements
WHERE mean_exec_time > 100
ORDER BY mean_exec_time DESC;Tables carrying dead rows that autovacuum is not keeping up with:
SELECT relname, n_dead_tup, last_vacuum
FROM pg_stat_user_tables
WHERE n_dead_tup > 1000
ORDER BY n_dead_tup DESC;Set the timeouts before you need them
ALTER SYSTEM SET max_connections = 100;
ALTER SYSTEM SET work_mem = '8MB';
ALTER SYSTEM SET idle_in_transaction_session_timeout = '30s';
ALTER SYSTEM SET statement_timeout = '30s';
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
REVOKE ALL ON SCHEMA public FROM public;
SELECT pg_reload_conf();work_mem is per sort node, not per connection, so a single complex query can use several multiples of it. Size
max_connections for the machine and put a pooler in front rather than raising it.
Route reads to replicas, except read-your-own-writes
- Writes go to the primary.
- Reads that tolerate eventual consistency go to a replica.
- A read in the same request as a preceding write goes to the primary, or the user sees their own change missing.
Alert on these before a user reports them:
| Metric | Alert threshold |
| Query duration | over 500ms |
| Connection pool utilization | over 80% for more than 30s |
| Replication lag | over 30s |
| Disk usage | over 80% |
Run EXPLAIN ANALYZE before the change lands
Two triggers, either one of which requires the plan in the change description:
- Any query expected to touch more than 10,000 rows.
- Any migration that touches data in an existing table, meaning
UPDATE,DELETE,INSERT ... SELECT, or a concurrent index build.
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM orders WHERE created_at > '2024-01-01';EXPLAIN ANALYZE executes the statement it plans, so on an UPDATE, DELETE or INSERT it changes the data. Run
it inside a transaction and roll back:
BEGIN;
EXPLAIN ANALYZE UPDATE orders SET status = 'active' WHERE created_at > '2024-01-01';
ROLLBACK;This is the same gate database-migrations states for schema changes, deliberately worded identically so a change that
crosses both skills is reviewed once against one rule.
Back up on a schedule you have actually tested
- Daily full backup plus continuous WAL archiving.
- Backups stored in a separate cloud account from the application.
- AES-256 encryption at rest.
- Quarterly restore drills that measure the real recovery time, not the intended one.
- RTO and RPO written in the runbook, next to the measured numbers from the last drill.
A backup nobody has restored is a hypothesis.
Erase personal data within the legal window
GDPR right to erasure runs to 30 days. Two workable shapes:
- Soft delete then purge: set
deleted_at, exclude the row everywhere, delete it for real after the window. - Pseudonymize in place: overwrite the identifying columns with a non-reversible value, keeping the row for referential integrity and aggregates.
Never hard-delete before checking cascade behaviour and audit-log retention, because a DELETE that cascades into an
audit trail destroys the evidence that the erasure was lawful.
Related skills
database-migrationsfor applying any of these changes safely on a live table.backend-patternsandnode-backend-patternsfor connection pooling and caching above the database.springboot-patternswhen the queries are generated by Hibernate rather than written by hand.performance-optimizationfor measuring an endpoint before assuming the database is the bottleneck.security-reviewfor access control and personal-data handling beyond row-level security.- The
database-expertsubagent for a full review of an existing data layer.
Checklist
- Every predicate that runs often has an index whose leading columns match it.
- Every foreign key has an index on the referencing side.
- Identifiers, money, and timestamps use the correct type, never
floatfor money. - Row-level security policies wrap the auth call in a subquery and filter on an indexed column.
- Pagination is cursor-based on anything unbounded.
statement_timeoutandidle_in_transaction_session_timeoutare set.pg_stat_statementsis enabled and someone reads it.EXPLAIN ANALYZEoutput is attached when either trigger above applies.- Backups are encrypted, off-account, and restored on a schedule.
- Erasure requests have a documented path that respects audit retention.
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.

