mcpbeat Sign in

Postgres Patterns Skill for Claude

PostgreSQL database patterns for query optimization, schema design, indexing, and security. Based on Supabase best practices.

1k tokens
context cost
the whole folder, loaded on every use
1
files
instructions only
0
copies elsewhere
how many repositories repackaged it
265
stars on the repo
on the repository, not the skill itself

Install

one command, takes just this skill from the repository
npx skills add https://github.com/loulanyue/awesome-claude-notes --skill postgres-patterns

The instruction itself

1 sections, as written by the author

PostgreSQL パターン

PostgreSQLベストプラクティスのクイックリファレンス。詳細なガイダンスについては、database-reviewer エージェントを使用してください。

起動タイミング

  • SQLクエリまたはマイグレーションの作成時
  • データベーススキーマの設計時
  • 低速クエリのトラブルシューティング時
  • Row Level Securityの実装時
  • コネクションプーリングの設定時

クイックリファレンス

インデックスチートシート

| クエリパターン | インデックスタイプ | 例 |

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

| WHERE col = value | B-tree(デフォルト) | CREATE INDEX idx ON t (col) |

| WHERE col > value | B-tree | CREATE INDEX idx ON t (col) |

| WHERE a = x AND b > y | 複合 | CREATE INDEX idx ON t (a, b) |

| WHERE jsonb @> '{}' | GIN | CREATE INDEX idx ON t USING gin (col) |

| WHERE tsv @@ query | GIN | CREATE INDEX idx ON t USING gin (col) |

| 時系列範囲 | BRIN | CREATE INDEX idx ON t USING brin (col) |

データタイプクイックリファレンス

| 用途 | 正しいタイプ | 避けるべき |

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

| ID | bigint | int、ランダムUUID |

| 文字列 | text | varchar(255) |

| タイムスタンプ | timestamptz | timestamp |

| 金額 | numeric(10,2) | float |

| フラグ | boolean | varcharint |

一般的なパターン

複合インデックスの順序:

-- 等価列を最初に、次に範囲列
CREATE INDEX idx ON orders (status, created_at);
-- 次の場合に機能: WHERE status = 'pending' AND created_at > '2024-01-01'

カバリングインデックス:

CREATE INDEX idx ON users (email) INCLUDE (name, created_at);
-- SELECT email, name, created_at のテーブル検索を回避

部分インデックス:

CREATE INDEX idx ON users (email) WHERE deleted_at IS NULL;
-- より小さなインデックス、アクティブユーザーのみを含む

RLSポリシー(最適化):

CREATE POLICY policy ON orders
  USING ((SELECT auth.uid()) = user_id);  -- SELECTでラップ!

UPSERT:

INSERT INTO settings (user_id, key, value)
VALUES (123, 'theme', 'dark')
ON CONFLICT (user_id, key)
DO UPDATE SET value = EXCLUDED.value;

カーソルページネーション:

SELECT * FROM products WHERE id > $last_id ORDER BY id LIMIT 20;
-- O(1) vs OFFSET は O(n)

キュー処理:

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 *;

アンチパターン検出

-- インデックスのない外部キーを検索
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)
  );

-- 低速クエリを検索
SELECT query, mean_exec_time, calls
FROM pg_stat_statements
WHERE mean_exec_time > 100
ORDER BY mean_exec_time DESC;

-- テーブル肥大化をチェック
SELECT relname, n_dead_tup, last_vacuum
FROM pg_stat_user_tables
WHERE n_dead_tup > 1000
ORDER BY n_dead_tup DESC;

設定テンプレート

-- 接続制限(RAMに応じて調整)
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();

関連

  • Agent: database-reviewer - 完全なデータベースレビューワークフロー
  • Skill: clickhouse-io - ClickHouse分析パターン
  • Skill: backend-patterns - APIとバックエンドパターン

*Supabase Agent Skills)(MITライセンス)に基づく*

原文

  • 英語版の原文

ナビゲーション

  • 日本語ドキュメント一覧
  • skills/README.md
  • 貢献ガイド

Other skills for the same job

different authors, same section of the catalogue
Biorxiv Database
by christophacham
×4

Efficient database search tool for bioRxiv preprint server. Use this skill when searching for life sciences preprints by keywords, authors, date ranges, or categories, retrieving paper metadata, downloading PDFs, or conducting literature reviews.

9k tokens scripts
Brenda Database
by christophacham
×4

Access BRENDA enzyme database via SOAP API. Retrieve kinetic parameters (Km, kcat), reaction equations, organism data, and substrate-specific enzyme information for biochemical research and metabolic pathway analysis.

36k tokens scripts
Clinpgx Database
by christophacham
×4

Access ClinPGx pharmacogenomics data (successor to PharmGKB). Query gene-drug interactions, CPIC guidelines, allele functions, for precision medicine and genotype-guided dosing decisions.

13k tokens scripts
Clinvar Database
by christophacham
×4

Query NCBI ClinVar for variant clinical significance. Search by gene/position, interpret pathogenicity classifications, access via E-utilities API or FTP, annotate VCFs, for genomic medicine.

10k tokens
Cosmic Database
by christophacham
×4

Access COSMIC cancer mutation database. Query somatic mutations, Cancer Gene Census, mutational signatures, gene fusions, for cancer research and precision oncology. Requires authentication.

6k tokens scripts
Ensembl Database
by christophacham
×4

Query Ensembl genome database REST API for 250+ species. Gene lookups, sequence retrieval, variant analysis, comparative genomics, orthologs, VEP predictions, for genomic research.

8k tokens scripts
Fda Database
by christophacham
×4

Query openFDA API for drugs, devices, adverse events, recalls, regulatory submissions (510k, PMA), substance identification (UNII), for FDA regulatory data analysis and safety research.

32k tokens scripts
Gene Database
by christophacham
×4

Query NCBI Gene via E-utilities/Datasets API. Search by symbol/ID, retrieve gene info (RefSeqs, GO, locations, phenotypes), batch lookups, for gene annotation and functional analysis.

13k tokens scripts

How to use it

Copy the folder

Take loulanyue/postgres-patterns from the repository into ~/.claude/skills for personal use, or into .claude/skills inside a project.

Check the name does not clash

The agent identifies a skill by the name field in its header. Two skills with the same name cannot sit side by side — one of them will be ignored.