Chuyển đến nội dung
Skillpedia
API và máy chủ (backend)40k · #7331

postgresql-code-review

Skill này làm gì

Công cụ này phân tích mã SQL PostgreSQL của bạn, kiểm tra xem nó có tuân theo thực hành tốt và tránh các cạm bẫy thường gặp của PostgreSQL không. Nó tập trung vào cách bạn dùng các kiểu dữ liệu như JSONB và mảng, thiết kế cơ sở dữ liệu và bảo mật.

Khi nào nên dùng

  • ✓Khi bạn viết các truy vấn hoặc hàm SQL mới cho PostgreSQL.
  • ✓Khi bạn muốn tối ưu hiệu năng cơ sở dữ liệu PostgreSQL.
  • ✓Khi bạn muốn đảm bảo mã PostgreSQL an toàn và tuân theo tiêu chuẩn.

Liên kết skill

https://github.com/github/awesome-copilot/blob/main/skills/postgresql-code-review/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/github/awesome-copilot/blob/main/skills/postgresql-code-review/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Rà soát mã SQL PostgreSQL này để tối ưu và theo thực hành tốt: [mã SQL của bạn ở đây]”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Công cụ so sánh mã của bạn với các ví dụ về thực hành tốt và lỗi thường gặp đặc thù của PostgreSQL. Nó xác định các khu vực như cách dùng index, kiểu dữ liệu (JSONB, mảng, ENUM) và thiết kế bảng để đưa ra gợi ý cải thiện.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

PostgreSQL Code Review Assistant

Expert PostgreSQL code review for ${selection} (or entire project if no selection). Focus on PostgreSQL-specific best practices, anti-patterns, and quality standards that are unique to PostgreSQL.

🎯 PostgreSQL-Specific Review Areas

JSONB Best Practices

-- ❌ BAD: Inefficient JSONB usage
SELECT * FROM orders WHERE data->>'status' = 'shipped';  -- No index support

-- ✅ GOOD: Indexable JSONB queries
CREATE INDEX idx_orders_status ON orders USING gin((data->'status'));
SELECT * FROM orders WHERE data @> '{"status": "shipped"}';

-- ❌ BAD: Deep nesting without consideration
UPDATE orders SET data = data || '{"shipping":{"tracking":{"number":"123"}}}';

-- ✅ GOOD: Structured JSONB with validation
ALTER TABLE orders ADD CONSTRAINT valid_status 
CHECK (data->>'status' IN ('pending', 'shipped', 'delivered'));

Array Operations Review

-- ❌ BAD: Inefficient array operations
SELECT * FROM products WHERE 'electronics' = ANY(categories);  -- No index

-- ✅ GOOD: GIN indexed array queries
CREATE INDEX idx_products_categories ON products USING gin(categories);
SELECT * FROM products WHERE categories @> ARRAY['electronics'];

-- ❌ BAD: Array concatenation in loops
-- This would be inefficient in a function/procedure

-- ✅ GOOD: Bulk array operations
UPDATE products SET categories = categories || ARRAY['new_category']
WHERE id IN (SELECT id FROM products WHERE condition);

PostgreSQL Schema Design Review

-- ❌ BAD: Not using PostgreSQL features
CREATE TABLE users (
    id INTEGER,
    email VARCHAR(255),
    created_at TIMESTAMP
);

-- ✅ GOOD: PostgreSQL-optimized schema
CREATE TABLE users (
    id BIGSERIAL PRIMARY KEY,
    email CITEXT UNIQUE NOT NULL,  -- Case-insensitive email
    created_at TIMESTAMPTZ DEFAULT NOW(),
    metadata JSONB DEFAULT '{}',
    CONSTRAINT valid_email CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$')
);

-- Add JSONB GIN index for metadata queries
CREATE INDEX idx_users_metadata ON users USING gin(metadata);

Custom Types and Domains

-- ❌ BAD: Using generic types for specific data
CREATE TABLE transactions (
    amount DECIMAL(10,2),
    currency VARCHAR(3),
    status VARCHAR(20)
);

-- ✅ GOOD: PostgreSQL custom types
CREATE TYPE currency_code AS ENUM ('USD', 'EUR', 'GBP', 'JPY');
CREATE TYPE transaction_status AS ENUM ('pending', 'completed', 'failed', 'cancelled');
CREATE DOMAIN positive_amount AS DECIMAL(10,2) CHECK (VALUE > 0);

CREATE TABLE transactions (
    amount positive_amount NOT NULL,
    currency currency_code NOT NULL,
    status transaction_status DEFAULT 'pending'
);

🔍 PostgreSQL-Specific Anti-Patterns

Performance Anti-Patterns

  • Avoiding PostgreSQL-specific indexes: Not using GIN/GiST for appropriate data types
  • Misusing JSONB: Treating JSONB like a simple string field
  • Ignoring array operators: Using inefficient array operations
  • Poor partition key selection: Not leveraging PostgreSQL partitioning effectively

Schema Design Issues

  • Not using ENUM types: Using VARCHAR for limited value sets
  • Ignoring constraints: Missing CHECK constraints for data validation
  • Wrong data types: Using VARCHAR instead of TEXT or CITEXT
  • Missing JSONB structure: Unstructured JSONB without validation

Function and Trigger Issues

-- ❌ BAD: Inefficient trigger function
CREATE OR REPLACE FUNCTION update_modified_time()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();  -- Should use TIMESTAMPTZ
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- ✅ GOOD: Optimized trigger function
CREATE OR REPLACE FUNCTION update_modified_time()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Set trigger to fire only when needed
CREATE TRIGGER update_modified_time_trigger
    BEFORE UPDATE ON table_name
    FOR EACH ROW
    WHEN (OLD.* IS DISTINCT FROM NEW.*)
    EXECUTE FUNCTION update_modified_time();

📊 PostgreSQL Extension Usage Review

Extension Best Practices

-- ✅ Check if extension exists before creating
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE EXTENSION IF NOT EXISTS "pg_trgm";

-- ✅ Use extensions appropriately
-- UUID generation
SELECT uuid_generate_v4();

-- Password hashing
SELECT crypt('password', gen_salt('bf'));

-- Fuzzy text matching
SELECT word_similarity('postgres', 'postgre');

…

Câu hỏi thường gặp

“Anti-patterns” là gì?

Anti-patterns là các cách giải quyết vấn đề nghe có vẻ hoạt động được nhưng thực chất gây thêm rắc rối về sau, như hiệu năng kém hoặc khó bảo trì.

JSONB là gì?

JSONB là kiểu dữ liệu trong PostgreSQL lưu dữ liệu theo định dạng JSON, nhưng hiệu quả hơn khi truy vấn và lập chỉ mục, nên phù hợp với dữ liệu bán cấu trúc.

RLS (Row Level Security) là gì?

RLS là tính năng bảo mật của PostgreSQL cho phép bạn kiểm soát những dòng nào trong bảng mà người dùng có thể xem hoặc sửa, dựa trên các quy tắc cụ thể.

Chi tiết

Mô tả gốc của tác giả

PostgreSQL-specific code review assistant focusing on PostgreSQL best practices, anti-patterns, and unique quality standards. Covers JSONB operations, array usage, custom types, schema design, function optimization, and PostgreSQL-exclusive security features like Row Level Security (RLS).

repo: github/awesome-copilot

Độ phổ biến

Sao GitHub
40k
Vị trí xếp hạng
#7331
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự