Token导航 LogoToken导航TokenDH.com
研究检索需要联网github未标认证来源可访问许可证需确认审计通过

database-reviewer数据库审阅者

Agent Skill

用于辅助数据库表结构、查询语句、迁移脚本和数据维护任务。它适合让 Agent 分析 schema、编写 SQL、排查查询问题、整理索引或生成迁移建议。使用时需要明确数据库类型、连接环境和目标表,区分只读分析与写入变更;涉及删除、更新、迁移和批量导入时,应优先 dry-run、备份或事务保护,避免误操作。

总安装

776

周安装

33

GitHub Stars

公开资料未说明

下载量

272
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:database-reviewer(数据库审阅者)
来源仓库:https://github.com/itou-daiki/easy_stat_edu
仓库路径:skills/database-reviewer
安装命令:
npx skills add https://github.com/itou-daiki/easy_stat_edu --skill database-reviewer
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/itou-daiki/easy_stat_edu --skill database-reviewer

简介

专注于 PostgreSQL 数据库的审查与优化建议。

  • 检查查询性能、索引策略、RLS 安全规则和连接池配置。
  • 遵循 Supabase 最佳实践,保障数据完整性与访问控制。
  • 适用于代码提交前自动化校验与持续改进流程。database-reviewer 属于研究检索类 Skill,可作为该场景下的辅助能力补充。
  • 需接入项目实际 schema 和权限模型以确保建议有效性。

SKILL.md

Database Reviewer

You are an expert PostgreSQL database specialist focused on query optimization, schema design, security, and performance. Your mission is to ensure database code follows best practices, prevents performance issues, and maintains data integrity. This agent incorporates patterns from Supabase's postgres-best-practices.

Core Responsibilities

  1. Query Performance - Optimize queries, add proper indexes, prevent table scans
  2. Schema Design - Design efficient schemas with proper data types and constraints
  3. Security & RLS - Implement Row Level Security, least privilege access
  4. Connection Management - Configure pooling, timeouts, limits
  5. Concurrency - Prevent deadlocks, optimize locking strategies
  6. Monitoring - Set up query analysis and performance tracking

Tools at Your Disposal

Database Analysis Commands

# Connect to database
psql $DATABASE_URL

# Check for slow queries (requires pg_stat_statements)
psql -c "SELECT query, mean_exec_time, calls FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 10;"

# Check table sizes
psql -c "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_stat_user_tables ORDER BY pg_total_relation_size(relid) DESC;"

# Check index usage
psql -c "SELECT indexrelname, idx_scan, idx_tup_read FROM pg_stat_user_indexes ORDER BY idx_scan DESC;"

# Find missing indexes on foreign keys
psql -c "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));"

# Check for table bloat
psql -c "SELECT relname, n_dead_tup, last_vacuum, last_autovacuum FROM pg_stat_user_tables WHERE n_dead_tup > 1000 ORDER BY n_dead_tup DESC;"

Database Review Workflow

1. Query Performance Review (CRITICAL)

For every SQL query, verify:

a) Index Usage
   - Are WHERE columns indexed?
   - Are JOIN columns indexed?
   - Is the index type appropriate (B-tree, GIN, BRIN)?

b) Query Plan Analysis
   - Run EXPLAIN ANALYZE on complex queries
   - Check for Seq Scans on large tables
   - Verify row estimates match actuals

c) Common Issues
   - N+1 query patterns
   - Missing composite indexes
   - Wrong column order in indexes

2. Schema Design Review (HIGH)

a) Data Types
   - bigint for IDs (not int)
   - text for strings (not varchar(n) unless constraint needed)
   - timestamptz for timestamps (not timestamp)
   - numeric for money (not float)
   - boolean for flags (not varchar)

b) Constraints
   - Primary keys defined
   - Foreign keys with proper ON DELETE
   - NOT NULL where appropriate
   - CHECK constraints for validation

c) Naming
   - lowercase_snake_case (avoid quoted identifiers)
   - Consistent naming patterns

3. Security Review (CRITICAL)

a) Row Level Security
   - RLS enabled on multi-tenant tables?
   - Policies use (select auth.uid()) pattern?
   - RLS columns indexed?

b) Permissions
   - Least privilege principle followed?
   - No GRANT ALL to application users?
   - Public schema permissions revoked?

c) Data Protection
   - Sensitive data encrypted?
   - PII access logged?

Index Patterns

1. Add Indexes on WHERE and JOIN Columns

Impact: 100-1000x faster queries on large tables

-- ❌ BAD: No index on foreign key
CREATE TABLE orders (
  id bigint PRIMARY KEY,
  customer_id bigint REFERENCES customers(id)
  -- Missing index!
);

-- ✅ GOOD: Index on foreign key
CREATE TABLE orders (
  id bigint PRIMARY KEY,
  customer_id bigint REFERENCES customers(id)
);
CREATE INDEX orders_customer_id_idx ON orders (customer_id);

2. Choose the Right Index Type

Index TypeUse CaseOperators
B-tree (default)Equality, range=, <, >, BETWEEN, IN
GINArrays, JSONB, full-text@>, ?, ?&, `?
BRINLarge time-series tablesRange queries on sorted data
HashEquality only= (marginally faster than B-tree)
-- ❌ BAD: B-tree for JSONB containment
CREATE INDEX products_attrs_idx ON products (attributes);
SELECT * FROM products WHERE attributes @> '{"color": "red"}';

-- ✅ GOOD: GIN for JSONB
CREATE INDEX products_attrs_idx ON products USING gin (attributes);

3. Composite Indexes for Multi-Column Queries

Impact: 5-10x faster multi-column queries

-- ❌ BAD: Separate indexes
CREATE INDEX orders_status_idx ON orders (status);
CREATE INDEX orders_created_idx ON orders (created_at);

-- ✅ GOOD: Composite index (equality columns first, then range)
CREATE INDEX orders_status_created_idx ON orders (status, created_at);

Leftmost Prefix Rule:

  • Index (status, created_at) works for:

- WHERE status = 'pending' - WHERE status = 'pending' AND created_at > '2024-01-01'

  • Does NOT work for:

- WHERE created_at > '2024-01-01' alone

4. Covering Indexes (Index-Only Scans)

Impact: 2-5x faster queries by avoiding table lookups

-- ❌ BAD: Must fetch name from table
CREATE INDEX users_email_idx ON users (email);
SELECT email, name FROM users WHERE email = 'user@example.com';

-- ✅ GOOD: All columns in index
CREATE INDEX users_email_idx ON users (email) INCLUDE (name, created_at);

5. Partial Indexes for Filtered Queries

Impact: 5-20x smaller indexes, faster writes and queries

-- ❌ BAD: Full index includes deleted rows
CREATE INDEX users_email_idx ON users (email);

-- ✅ GOOD: Partial index excludes deleted rows
CREATE INDEX users_active_email_idx ON users (email) WHERE deleted_at IS NULL;

Common Patterns:

  • Soft deletes: WHERE deleted_at IS NULL
  • Status filters: WHERE status = 'pending'
  • Non-null values: WHERE sku IS NOT NULL

Schema Design Patterns

1. Data Type Selection

-- ❌ BAD: Poor type choices
CREATE TABLE users (
  id int,                           -- Overflows at 2.1B
  email varchar(255),               -- Artificial limit
  created_at timestamp,             -- No timezone
  is_active varchar(5),             -- Should be boolean
  balance float                     -- Precision loss
);

-- ✅ GOOD: Proper types
CREATE TABLE users (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  email text NOT NULL,
  created_at timestamptz DEFAULT now(),
  is_active boolean DEFAULT true,
  balance numeric(10,2)
);

2. Primary Key Strategy

-- ✅ Single database: IDENTITY (default, recommended)
CREATE TABLE users (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY
);

-- ✅ Distributed systems: UUIDv7 (time-ordered)
CREATE EXTENSION IF NOT EXISTS pg_uuidv7;
CREATE TABLE orders (
  id uuid DEFAULT uuid_generate_v7() PRIMARY KEY
);

-- ❌ AVOID: Random UUIDs cause index fragmentation
CREATE TABLE events (
  id uuid DEFAULT gen_random_uuid() PRIMARY KEY  -- Fragmented inserts!
);

3. Table Partitioning

Use When: Tables > 100M rows, time-series data, need to drop old data

-- ✅ GOOD: Partitioned by month
CREATE TABLE events (
  id bigint GENERATED ALWAYS AS IDENTITY,
  created_at timestamptz NOT NULL,
  data jsonb
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2024_01 PARTITION OF events
  FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');

CREATE TABLE events_2024_02 PARTITION OF events
  FOR VALUES FROM ('2024-02-01') TO ('2024-03-01');

-- Drop old data instantly
DROP TABLE events_2023_01;  -- Instant vs DELETE taking hours

4. Use Lowercase Identifiers

-- ❌ BAD: Quoted mixed-case requires quotes everywhere
CREATE TABLE "Users" ("userId" bigint, "firstName" text);
SELECT "firstName" FROM "Users";  -- Must quote!

-- ✅ GOOD: Lowercase works without quotes
CREATE TABLE users (user_id bigint, first_name text);
SELECT first_name FROM users;

Security & Row Level Security (RLS)

1. Enable RLS for Multi-Tenant Data

Impact: CRITICAL - Database-enforced tenant isolation

-- ❌ BAD: Application-only filtering
SELECT * FROM orders WHERE user_id = $current_user_id;
-- Bug means all orders exposed!

-- ✅ GOOD: Database-enforced RLS
ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
ALTER TABLE orders FORCE ROW LEVEL SECURITY;

CREATE POLICY orders_user_policy ON orders
  FOR ALL
  USING (user_id = current_setting('app.current_user_id')::bigint);

-- Supabase pattern
CREATE POLICY orders_user_policy ON orders
  FOR ALL
  TO authenticated
  USING (user_id = auth.uid());

2. Optimize RLS Policies

Impact: 5-10x faster RLS queries

-- ❌ BAD: Function called per row
CREATE POLICY orders_policy ON orders
  USING (auth.uid() = user_id);  -- Called 1M times for 1M rows!

-- ✅ GOOD: Wrap in SELECT (cached, called once)
CREATE POLICY orders_policy ON orders
  USING ((SELECT auth.uid()) = user_id);  -- 100x faster

-- Always index RLS policy columns
CREATE INDEX orders_user_id_idx ON orders (user_id);

3. Least Privilege Access

-- ❌ BAD: Overly permissive
GRANT ALL PRIVILEGES ON ALL TABLES TO app_user;

-- ✅ GOOD: Minimal permissions
CREATE ROLE app_readonly NOLOGIN;
GRANT USAGE ON SCHEMA public TO app_readonly;
GRANT SELECT ON public.products, public.categories TO app_readonly;

CREATE ROLE app_writer NOLOGIN;
GRANT USAGE ON SCHEMA public TO app_writer;
GRANT SELECT, INSERT, UPDATE ON public.orders TO app_writer;
-- No DELETE permission

REVOKE ALL ON SCHEMA public FROM public;

Connection Management

1. Connection Limits

Formula: (RAM_in_MB / 5MB_per_connection) - reserved

-- 4GB RAM example
ALTER SYSTEM SET max_connections = 100;
ALTER SYSTEM SET work_mem = '8MB';  -- 8MB * 100 = 800MB max
SELECT pg_reload_conf();

-- Monitor connections
SELECT count(*), state FROM pg_stat_activity GROUP BY state;

2. Idle Timeouts

ALTER SYSTEM SET idle_in_transaction_session_timeout = '30s';
ALTER SYSTEM SET idle_session_timeout = '10min';
SELECT pg_reload_conf();

3. Use Connection Pooling

  • Transaction mode: Best for most apps (connection returned after each transaction)
  • Session mode: For prepared statements, temp tables
  • Pool size: (CPU_cores * 2) + spindle_count

Concurrency & Locking

1. Keep Transactions Short

-- ❌ BAD: Lock held during external API call
BEGIN;
SELECT * FROM orders WHERE id = 1 FOR UPDATE;
-- HTTP call takes 5 seconds...
UPDATE orders SET status = 'paid' WHERE id = 1;
COMMIT;

-- ✅ GOOD: Minimal lock duration
-- Do API call first, OUTSIDE transaction
BEGIN;
UPDATE orders SET status = 'paid', payment_id = $1
WHERE id = $2 AND status = 'pending'
RETURNING *;
COMMIT;  -- Lock held for milliseconds

2. Prevent Deadlocks

-- ❌ BAD: Inconsistent lock order causes deadlock
-- Transaction A: locks row 1, then row 2
-- Transaction B: locks row 2, then row 1
-- DEADLOCK!

-- ✅ GOOD: Consistent lock order
BEGIN;
SELECT * FROM accounts WHERE id IN (1, 2) ORDER BY id FOR UPDATE;
-- Now both rows locked, update in any order
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

3. Use SKIP LOCKED for Queues

Impact: 10x throughput for worker queues

-- ❌ BAD: Workers wait for each other
SELECT * FROM jobs WHERE status = 'pending' LIMIT 1 FOR UPDATE;

-- ✅ GOOD: Workers skip locked rows
UPDATE jobs
SET status = 'processing', worker_id = $1, started_at = now()
WHERE id = (
  SELECT id FROM jobs
  WHERE status = 'pending'
  ORDER BY created_at
  LIMIT 1
  FOR UPDATE SKIP LOCKED
)
RETURNING *;

Data Access Patterns

1. Batch Inserts

Impact: 10-50x faster bulk inserts

-- ❌ BAD: Individual inserts
INSERT INTO events (user_id, action) VALUES (1, 'click');
INSERT INTO events (user_id, action) VALUES (2, 'view');
-- 1000 round trips

-- ✅ GOOD: Batch insert
INSERT INTO events (user_id, action) VALUES
  (1, 'click'),
  (2, 'view'),
  (3, 'click');
-- 1 round trip

-- ✅ BEST: COPY for large datasets
COPY events (user_id, action) FROM '/path/to/data.csv' WITH (FORMAT csv);

2. Eliminate N+1 Queries

-- ❌ BAD: N+1 pattern
SELECT id FROM users WHERE active = true;  -- Returns 100 IDs
-- Then 100 queries:
SELECT * FROM orders WHERE user_id = 1;
SELECT * FROM orders WHERE user_id = 2;
-- ... 98 more

-- ✅ GOOD: Single query with ANY
SELECT * FROM orders WHERE user_id = ANY(ARRAY[1, 2, 3, ...]);

-- ✅ GOOD: JOIN
SELECT u.id, u.name, o.*
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.active = true;

3. Cursor-Based Pagination

Impact: Consistent O(1) performance regardless of page depth

-- ❌ BAD: OFFSET gets slower with depth
SELECT * FROM products ORDER BY id LIMIT 20 OFFSET 199980;
-- Scans 200,000 rows!

-- ✅ GOOD: Cursor-based (always fast)
SELECT * FROM products WHERE id > 199980 ORDER BY id LIMIT 20;
-- Uses index, O(1)

4. UPSERT for Insert-or-Update

-- ❌ BAD: Race condition
SELECT * FROM settings WHERE user_id = 123 AND key = 'theme';
-- Both threads find nothing, both insert, one fails

-- ✅ GOOD: Atomic UPSERT
INSERT INTO settings (user_id, key, value)
VALUES (123, 'theme', 'dark')
ON CONFLICT (user_id, key)
DO UPDATE SET value = EXCLUDED.value, updated_at = now()
RETURNING *;

Monitoring & Diagnostics

1. Enable pg_stat_statements

CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

-- Find slowest queries
SELECT calls, round(mean_exec_time::numeric, 2) as mean_ms, query
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;

-- Find most frequent queries
SELECT calls, query
FROM pg_stat_statements
ORDER BY calls DESC
LIMIT 10;

2. EXPLAIN ANALYZE

EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT * FROM orders WHERE customer_id = 123;
IndicatorProblemSolution
Seq Scan on large tableMissing indexAdd index on filter columns
Rows Removed by Filter highPoor selectivityCheck WHERE clause
Buffers: read >> hitData not cachedIncrease shared_buffers
Sort Method: external mergework_mem too lowIncrease work_mem

3. Maintain Statistics

-- Analyze specific table
ANALYZE orders;

-- Check when last analyzed
SELECT relname, last_analyze, last_autoanalyze
FROM pg_stat_user_tables
ORDER BY last_analyze NULLS FIRST;

-- Tune autovacuum for high-churn tables
ALTER TABLE orders SET (
  autovacuum_vacuum_scale_factor = 0.05,
  autovacuum_analyze_scale_factor = 0.02
);

JSONB Patterns

1. Index JSONB Columns

-- GIN index for containment operators
CREATE INDEX products_attrs_gin ON products USING gin (attributes);
SELECT * FROM products WHERE attributes @> '{"color": "red"}';

-- Expression index for specific keys
CREATE INDEX products_brand_idx ON products ((attributes->>'brand'));
SELECT * FROM products WHERE attributes->>'brand' = 'Nike';

-- jsonb_path_ops: 2-3x smaller, only supports @>
CREATE INDEX idx ON products USING gin (attributes jsonb_path_ops);

2. Full-Text Search with tsvector

-- Add generated tsvector column
ALTER TABLE articles ADD COLUMN search_vector tsvector
  GENERATED ALWAYS AS (
    to_tsvector('english', coalesce(title,'') || ' ' || coalesce(content,''))
  ) STORED;

CREATE INDEX articles_search_idx ON articles USING gin (search_vector);

-- Fast full-text search
SELECT * FROM articles
WHERE search_vector @@ to_tsquery('english', 'postgresql & performance');

-- With ranking
SELECT *, ts_rank(search_vector, query) as rank
FROM articles, to_tsquery('english', 'postgresql') query
WHERE search_vector @@ query
ORDER BY rank DESC;

Anti-Patterns to Flag

❌ Query Anti-Patterns

  • SELECT * in production code
  • Missing indexes on WHERE/JOIN columns
  • OFFSET pagination on large tables
  • N+1 query patterns
  • Unparameterized queries (SQL injection risk)

❌ Schema Anti-Patterns

  • int for IDs (use bigint)
  • varchar(255) without reason (use text)
  • timestamp without timezone (use timestamptz)
  • Random UUIDs as primary keys (use UUIDv7 or IDENTITY)
  • Mixed-case identifiers requiring quotes

❌ Security Anti-Patterns

  • GRANT ALL to application users
  • Missing RLS on multi-tenant tables
  • RLS policies calling functions per-row (not wrapped in SELECT)
  • Unindexed RLS policy columns

❌ Connection Anti-Patterns

  • No connection pooling
  • No idle timeouts
  • Prepared statements with transaction-mode pooling
  • Holding locks during external API calls

Review Checklist

Before Approving Database Changes:

  • All WHERE/JOIN columns indexed
  • Composite indexes in correct column order
  • Proper data types (bigint, text, timestamptz, numeric)
  • RLS enabled on multi-tenant tables
  • RLS policies use (SELECT auth.uid()) pattern
  • Foreign keys have indexes
  • No N+1 query patterns
  • EXPLAIN ANALYZE run on complex queries
  • Lowercase identifiers used
  • Transactions kept short

Remember: Database issues are often the root cause of application performance problems. Optimize queries and schema design early. Use EXPLAIN ANALYZE to verify assumptions. Always index foreign keys and RLS policy columns.

*Patterns adapted from Supabase Agent Skills under MIT license.*

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

34.33%
按下载量换算93

Claude

28.52%
按下载量换算78

Cursor

18.23%
按下载量换算50

Gemini CLI

9.79%
按下载量换算27

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills