@totaland/create-starter-kit 1.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +130 -18
- package/bin/index.js +109 -21
- package/package.json +2 -2
- package/templates/backend/.github/agents/ /360/237/216/255 planner.agent.md" +92 -0
- package/templates/backend/.github/agents/api-architect.agent.md +41 -0
- package/templates/backend/.github/agents/code-reviewer.agent.md +26 -0
- package/templates/backend/.github/agents/code-simplifier.agent.md +41 -0
- package/templates/backend/.github/agents/code-skeptic.agent.md +74 -0
- package/templates/backend/.github/agents/docs-specialist.agent.md +25 -0
- package/templates/backend/.github/agents/implementation-plan.agent.md +160 -0
- package/templates/backend/.github/agents/janitor.agent.md +90 -0
- package/templates/backend/.github/agents/ms-sql-dba.agent.md +25 -0
- package/templates/backend/.github/agents/plan.agent.md +115 -0
- package/templates/backend/.github/agents/planner.agent.md +15 -0
- package/templates/backend/.github/agents/playwright-tester.agent.md +13 -0
- package/templates/backend/.github/agents/postgresql-dba.agent.md +18 -0
- package/templates/backend/.github/agents/prd.agent.md +202 -0
- package/templates/backend/.github/agents/research-technical-spike.agent.md +170 -0
- package/templates/backend/.github/agents/software-engineer-agent-v1.agent.md +165 -0
- package/templates/backend/.github/agents/task-planner.agent.md +375 -0
- package/templates/backend/.github/agents/task-researcher.agent.md +255 -0
- package/templates/backend/.github/agents/tdd-green.agent.md +60 -0
- package/templates/backend/.github/agents/tdd-red.agent.md +60 -0
- package/templates/backend/.github/agents/tdd-refactor.agent.md +85 -0
- package/templates/backend/.github/agents/tech-debt-remediation-plan.agent.md +50 -0
- package/templates/backend/.github/agents/typescript-mcp-expert.agent.md +91 -0
- package/templates/backend/.github/agents/ultimate-thinking-beastmode.agent.md +644 -0
- package/templates/backend/.github/agents//360/237/216/255 generator.agent.md" +264 -0
- package/templates/backend/.github/agents//360/237/216/255 healer.agent.md" +44 -0
- package/templates/backend/.github/agents//360/237/216/255 pom.agent.md" +308 -0
- package/templates/backend/.github/instructions/a11y.instructions.md +369 -0
- package/templates/backend/.github/instructions/ms-sql-dba.instructions.md +25 -0
- package/templates/backend/.github/instructions/object-calisthenics.instructions.md +302 -0
- package/templates/backend/.github/instructions/performance-optimization.instructions.md +420 -0
- package/templates/backend/.github/instructions/playwright-python.instructions.md +62 -0
- package/templates/backend/.github/instructions/playwright-typescript.instructions.md +86 -0
- package/templates/backend/.github/instructions/security-and-owasp.instructions.md +51 -0
- package/templates/backend/.github/instructions/self-explanatory-code-commenting.instructions.md +162 -0
- package/templates/backend/.github/instructions/spec-driven-workflow-v1.instructions.md +323 -0
- package/templates/backend/.github/instructions/sql-sp-generation.instructions.md +74 -0
- package/templates/backend/.github/instructions/task-implementation.instructions.md +190 -0
- package/templates/backend/.github/instructions/typescript-mcp-server.instructions.md +228 -0
- package/templates/backend/.github/prompts/ai-prompt-engineering-safety-review.prompt.md +229 -0
- package/templates/backend/.github/prompts/architecture-blueprint-generator.prompt.md +321 -0
- package/templates/backend/.github/prompts/breakdown-epic-arch.prompt.md +65 -0
- package/templates/backend/.github/prompts/breakdown-epic-pm.prompt.md +57 -0
- package/templates/backend/.github/prompts/breakdown-feature-implementation.prompt.md +127 -0
- package/templates/backend/.github/prompts/breakdown-feature-prd.prompt.md +60 -0
- package/templates/backend/.github/prompts/breakdown-plan.prompt.md +508 -0
- package/templates/backend/.github/prompts/copilot-instructions-blueprint-generator.prompt.md +293 -0
- package/templates/backend/.github/prompts/create-agentsmd.prompt.md +248 -0
- package/templates/backend/.github/prompts/create-github-issue-feature-from-specification.prompt.md +27 -0
- package/templates/backend/.github/prompts/create-github-issues-feature-from-implementation-plan.prompt.md +27 -0
- package/templates/backend/.github/prompts/create-github-issues-for-unmet-specification-requirements.prompt.md +35 -0
- package/templates/backend/.github/prompts/create-implementation-plan.prompt.md +156 -0
- package/templates/backend/.github/prompts/create-llms.prompt.md +209 -0
- package/templates/backend/.github/prompts/create-specification.prompt.md +126 -0
- package/templates/backend/.github/prompts/create-technical-spike.prompt.md +230 -0
- package/templates/backend/.github/prompts/csharp-nunit.prompt.md +71 -0
- package/templates/backend/.github/prompts/ef-core.prompt.md +76 -0
- package/templates/backend/.github/prompts/folder-structure-blueprint-generator.prompt.md +404 -0
- package/templates/backend/.github/prompts/gen-specs-as-issues.prompt.md +165 -0
- package/templates/backend/.github/prompts/generate-custom-instructions-from-codebase.prompt.md +239 -0
- package/templates/backend/.github/prompts/java-junit.prompt.md +64 -0
- package/templates/backend/.github/prompts/memory-merger.prompt.md +107 -0
- package/templates/backend/.github/prompts/multi-stage-dockerfile.prompt.md +46 -0
- package/templates/backend/.github/prompts/playwright-automation-fill-in-form.prompt.md +29 -0
- package/templates/backend/.github/prompts/playwright-explore-website.prompt.md +19 -0
- package/templates/backend/.github/prompts/playwright-generate-test.prompt.md +19 -0
- package/templates/backend/.github/prompts/postgresql-code-review.prompt.md +214 -0
- package/templates/backend/.github/prompts/postgresql-optimization.prompt.md +406 -0
- package/templates/backend/.github/prompts/project-workflow-analysis-blueprint-generator.prompt.md +294 -0
- package/templates/backend/.github/prompts/prompt-builder.prompt.md +142 -0
- package/templates/backend/.github/prompts/remember.prompt.md +125 -0
- package/templates/backend/.github/prompts/review-and-refactor.prompt.md +15 -0
- package/templates/backend/.github/prompts/sql-code-review.prompt.md +303 -0
- package/templates/backend/.github/prompts/sql-optimization.prompt.md +298 -0
- package/templates/backend/.github/prompts/suggest-awesome-github-copilot-agents.prompt.md +72 -0
- package/templates/backend/.github/prompts/suggest-awesome-github-copilot-chatmodes.prompt.md +71 -0
- package/templates/backend/.github/prompts/suggest-awesome-github-copilot-collections.prompt.md +149 -0
- package/templates/backend/.github/prompts/suggest-awesome-github-copilot-instructions.prompt.md +88 -0
- package/templates/backend/.github/prompts/suggest-awesome-github-copilot-prompts.prompt.md +71 -0
- package/templates/backend/.github/prompts/technology-stack-blueprint-generator.prompt.md +242 -0
- package/templates/backend/.github/prompts/typescript-mcp-server-generator.prompt.md +90 -0
- package/templates/backend/.github/prompts/update-implementation-plan.prompt.md +157 -0
- package/templates/backend/.github/prompts/update-markdown-file-index.prompt.md +76 -0
- package/templates/backend/Project_Folders_Structure_Blueprint.md +529 -0
- package/templates/backend/README.md +99 -0
- package/templates/backend/biome.json +45 -0
- package/{template → templates/backend}/drizzle.config.ts +1 -1
- package/{template → templates/backend}/package.json +9 -4
- package/templates/backend/pnpm-lock.yaml +4240 -0
- package/templates/backend/src/features/health/health.test.ts +23 -0
- package/{template → templates/backend}/src/features/health/index.ts +1 -0
- package/templates/backend/src/features/health/schemas.ts +29 -0
- package/templates/backend/src/features/orders/controller.ts +18 -0
- package/templates/backend/src/features/orders/index.ts +10 -0
- package/templates/backend/src/features/orders/schemas.ts +62 -0
- package/{template → templates/backend}/src/index.ts +3 -36
- package/templates/backend/src/lib/openapi.ts +25 -0
- package/templates/backend/src/lib/validateRequest.test.ts +84 -0
- package/templates/backend/src/lib/validateRequest.ts +72 -0
- package/{template → templates/backend}/tsconfig.json +2 -2
- package/{template → templates/backend}/vitest.config.ts +8 -1
- package/templates/frontend/.env.example +7 -0
- package/templates/frontend/.github/agents/ /360/237/216/255 planner.agent.md" +92 -0
- package/templates/frontend/.github/agents/api-architect.agent.md +41 -0
- package/templates/frontend/.github/agents/code-reviewer.agent.md +26 -0
- package/templates/frontend/.github/agents/code-simplifier.agent.md +41 -0
- package/templates/frontend/.github/agents/code-skeptic.agent.md +74 -0
- package/templates/frontend/.github/agents/docs-specialist.agent.md +25 -0
- package/templates/frontend/.github/agents/implementation-plan.agent.md +160 -0
- package/templates/frontend/.github/agents/janitor.agent.md +90 -0
- package/templates/frontend/.github/agents/ms-sql-dba.agent.md +25 -0
- package/templates/frontend/.github/agents/plan.agent.md +115 -0
- package/templates/frontend/.github/agents/planner.agent.md +15 -0
- package/templates/frontend/.github/agents/playwright-tester.agent.md +13 -0
- package/templates/frontend/.github/agents/postgresql-dba.agent.md +18 -0
- package/templates/frontend/.github/agents/prd.agent.md +202 -0
- package/templates/frontend/.github/agents/research-technical-spike.agent.md +170 -0
- package/templates/frontend/.github/agents/software-engineer-agent-v1.agent.md +165 -0
- package/templates/frontend/.github/agents/task-planner.agent.md +375 -0
- package/templates/frontend/.github/agents/task-researcher.agent.md +255 -0
- package/templates/frontend/.github/agents/tdd-green.agent.md +60 -0
- package/templates/frontend/.github/agents/tdd-red.agent.md +60 -0
- package/templates/frontend/.github/agents/tdd-refactor.agent.md +85 -0
- package/templates/frontend/.github/agents/tech-debt-remediation-plan.agent.md +50 -0
- package/templates/frontend/.github/agents/typescript-mcp-expert.agent.md +91 -0
- package/templates/frontend/.github/agents/ultimate-thinking-beastmode.agent.md +644 -0
- package/templates/frontend/.github/agents//360/237/216/255 generator.agent.md" +264 -0
- package/templates/frontend/.github/agents//360/237/216/255 healer.agent.md" +44 -0
- package/templates/frontend/.github/agents//360/237/216/255 pom.agent.md" +308 -0
- package/templates/frontend/.github/instructions/a11y.instructions.md +369 -0
- package/templates/frontend/.github/instructions/ms-sql-dba.instructions.md +25 -0
- package/templates/frontend/.github/instructions/object-calisthenics.instructions.md +302 -0
- package/templates/frontend/.github/instructions/performance-optimization.instructions.md +420 -0
- package/templates/frontend/.github/instructions/playwright-python.instructions.md +62 -0
- package/templates/frontend/.github/instructions/playwright-typescript.instructions.md +86 -0
- package/templates/frontend/.github/instructions/security-and-owasp.instructions.md +51 -0
- package/templates/frontend/.github/instructions/self-explanatory-code-commenting.instructions.md +162 -0
- package/templates/frontend/.github/instructions/spec-driven-workflow-v1.instructions.md +323 -0
- package/templates/frontend/.github/instructions/sql-sp-generation.instructions.md +74 -0
- package/templates/frontend/.github/instructions/task-implementation.instructions.md +190 -0
- package/templates/frontend/.github/instructions/typescript-mcp-server.instructions.md +228 -0
- package/templates/frontend/.github/prompts/ai-prompt-engineering-safety-review.prompt.md +229 -0
- package/templates/frontend/.github/prompts/architecture-blueprint-generator.prompt.md +321 -0
- package/templates/frontend/.github/prompts/breakdown-epic-arch.prompt.md +65 -0
- package/templates/frontend/.github/prompts/breakdown-epic-pm.prompt.md +57 -0
- package/templates/frontend/.github/prompts/breakdown-feature-implementation.prompt.md +127 -0
- package/templates/frontend/.github/prompts/breakdown-feature-prd.prompt.md +60 -0
- package/templates/frontend/.github/prompts/breakdown-plan.prompt.md +508 -0
- package/templates/frontend/.github/prompts/copilot-instructions-blueprint-generator.prompt.md +293 -0
- package/templates/frontend/.github/prompts/create-agentsmd.prompt.md +248 -0
- package/templates/frontend/.github/prompts/create-github-issue-feature-from-specification.prompt.md +27 -0
- package/templates/frontend/.github/prompts/create-github-issues-feature-from-implementation-plan.prompt.md +27 -0
- package/templates/frontend/.github/prompts/create-github-issues-for-unmet-specification-requirements.prompt.md +35 -0
- package/templates/frontend/.github/prompts/create-implementation-plan.prompt.md +156 -0
- package/templates/frontend/.github/prompts/create-llms.prompt.md +209 -0
- package/templates/frontend/.github/prompts/create-specification.prompt.md +126 -0
- package/templates/frontend/.github/prompts/create-technical-spike.prompt.md +230 -0
- package/templates/frontend/.github/prompts/csharp-nunit.prompt.md +71 -0
- package/templates/frontend/.github/prompts/ef-core.prompt.md +76 -0
- package/templates/frontend/.github/prompts/folder-structure-blueprint-generator.prompt.md +404 -0
- package/templates/frontend/.github/prompts/gen-specs-as-issues.prompt.md +165 -0
- package/templates/frontend/.github/prompts/generate-custom-instructions-from-codebase.prompt.md +239 -0
- package/templates/frontend/.github/prompts/java-junit.prompt.md +64 -0
- package/templates/frontend/.github/prompts/memory-merger.prompt.md +107 -0
- package/templates/frontend/.github/prompts/multi-stage-dockerfile.prompt.md +46 -0
- package/templates/frontend/.github/prompts/playwright-automation-fill-in-form.prompt.md +29 -0
- package/templates/frontend/.github/prompts/playwright-explore-website.prompt.md +19 -0
- package/templates/frontend/.github/prompts/playwright-generate-test.prompt.md +19 -0
- package/templates/frontend/.github/prompts/postgresql-code-review.prompt.md +214 -0
- package/templates/frontend/.github/prompts/postgresql-optimization.prompt.md +406 -0
- package/templates/frontend/.github/prompts/project-workflow-analysis-blueprint-generator.prompt.md +294 -0
- package/templates/frontend/.github/prompts/prompt-builder.prompt.md +142 -0
- package/templates/frontend/.github/prompts/remember.prompt.md +125 -0
- package/templates/frontend/.github/prompts/review-and-refactor.prompt.md +15 -0
- package/templates/frontend/.github/prompts/sql-code-review.prompt.md +303 -0
- package/templates/frontend/.github/prompts/sql-optimization.prompt.md +298 -0
- package/templates/frontend/.github/prompts/suggest-awesome-github-copilot-agents.prompt.md +72 -0
- package/templates/frontend/.github/prompts/suggest-awesome-github-copilot-chatmodes.prompt.md +71 -0
- package/templates/frontend/.github/prompts/suggest-awesome-github-copilot-collections.prompt.md +149 -0
- package/templates/frontend/.github/prompts/suggest-awesome-github-copilot-instructions.prompt.md +88 -0
- package/templates/frontend/.github/prompts/suggest-awesome-github-copilot-prompts.prompt.md +71 -0
- package/templates/frontend/.github/prompts/technology-stack-blueprint-generator.prompt.md +242 -0
- package/templates/frontend/.github/prompts/typescript-mcp-server-generator.prompt.md +90 -0
- package/templates/frontend/.github/prompts/update-implementation-plan.prompt.md +157 -0
- package/templates/frontend/.github/prompts/update-markdown-file-index.prompt.md +76 -0
- package/templates/frontend/README.md +199 -0
- package/templates/frontend/biome.json +45 -0
- package/templates/frontend/components.json +22 -0
- package/templates/frontend/index.html +13 -0
- package/templates/frontend/package.json +44 -0
- package/templates/frontend/pnpm-lock.yaml +2011 -0
- package/templates/frontend/postcss.config.js +6 -0
- package/templates/frontend/public/vite.svg +1 -0
- package/templates/frontend/src/App.css +42 -0
- package/templates/frontend/src/App.tsx +17 -0
- package/templates/frontend/src/assets/react.svg +1 -0
- package/templates/frontend/src/components/layout/layout.component.tsx +31 -0
- package/templates/frontend/src/components/menu-toggle-icon.tsx +53 -0
- package/templates/frontend/src/components/ui/button.tsx +57 -0
- package/templates/frontend/src/docs/RECOMMENDED_LIBRARIES.md +226 -0
- package/templates/frontend/src/docs/SETUP_SUMMARY.md +162 -0
- package/templates/frontend/src/hooks/use-scroll.ts +21 -0
- package/templates/frontend/src/index.css +121 -0
- package/templates/frontend/src/lib/api-client.ts +46 -0
- package/templates/frontend/src/lib/utils.ts +6 -0
- package/templates/frontend/src/main.tsx +30 -0
- package/templates/frontend/src/pages/about/about.page.tsx +50 -0
- package/templates/frontend/src/pages/home/home.page.tsx +43 -0
- package/templates/frontend/tailwind.config.js +59 -0
- package/templates/frontend/tsconfig.app.json +41 -0
- package/templates/frontend/tsconfig.json +13 -0
- package/templates/frontend/tsconfig.node.json +26 -0
- package/templates/frontend/vite.config.ts +14 -0
- package/template/.env.example +0 -8
- package/template/AGENTS.md +0 -23
- package/template/ARCHITECTURE.md +0 -53
- package/template/ORDER_SYSTEM.md +0 -93
- package/template/biome.json +0 -3
- package/template/src/features/orders/controller.ts +0 -13
- package/template/src/features/orders/index.ts +0 -7
- /package/{template → templates/backend}/knip.json +0 -0
- /package/{template → templates/backend}/playwright.config.ts +0 -0
- /package/{template → templates/backend}/pnpm-workspace.yaml +0 -0
- /package/{template → templates/backend}/src/features/health/controller.ts +0 -0
- /package/{template → templates/backend}/tsconfig.build.json +0 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
---
|
|
2
|
+
mode: 'agent'
|
|
3
|
+
tools: ['changes', 'search/codebase', 'edit/editFiles', 'problems']
|
|
4
|
+
description: '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).'
|
|
5
|
+
tested_with: 'GitHub Copilot Chat (GPT-4o) - Validated July 20, 2025'
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# PostgreSQL Code Review Assistant
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
## 🎯 PostgreSQL-Specific Review Areas
|
|
13
|
+
|
|
14
|
+
### JSONB Best Practices
|
|
15
|
+
```sql
|
|
16
|
+
-- ❌ BAD: Inefficient JSONB usage
|
|
17
|
+
SELECT * FROM orders WHERE data->>'status' = 'shipped'; -- No index support
|
|
18
|
+
|
|
19
|
+
-- ✅ GOOD: Indexable JSONB queries
|
|
20
|
+
CREATE INDEX idx_orders_status ON orders USING gin((data->'status'));
|
|
21
|
+
SELECT * FROM orders WHERE data @> '{"status": "shipped"}';
|
|
22
|
+
|
|
23
|
+
-- ❌ BAD: Deep nesting without consideration
|
|
24
|
+
UPDATE orders SET data = data || '{"shipping":{"tracking":{"number":"123"}}}';
|
|
25
|
+
|
|
26
|
+
-- ✅ GOOD: Structured JSONB with validation
|
|
27
|
+
ALTER TABLE orders ADD CONSTRAINT valid_status
|
|
28
|
+
CHECK (data->>'status' IN ('pending', 'shipped', 'delivered'));
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Array Operations Review
|
|
32
|
+
```sql
|
|
33
|
+
-- ❌ BAD: Inefficient array operations
|
|
34
|
+
SELECT * FROM products WHERE 'electronics' = ANY(categories); -- No index
|
|
35
|
+
|
|
36
|
+
-- ✅ GOOD: GIN indexed array queries
|
|
37
|
+
CREATE INDEX idx_products_categories ON products USING gin(categories);
|
|
38
|
+
SELECT * FROM products WHERE categories @> ARRAY['electronics'];
|
|
39
|
+
|
|
40
|
+
-- ❌ BAD: Array concatenation in loops
|
|
41
|
+
-- This would be inefficient in a function/procedure
|
|
42
|
+
|
|
43
|
+
-- ✅ GOOD: Bulk array operations
|
|
44
|
+
UPDATE products SET categories = categories || ARRAY['new_category']
|
|
45
|
+
WHERE id IN (SELECT id FROM products WHERE condition);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### PostgreSQL Schema Design Review
|
|
49
|
+
```sql
|
|
50
|
+
-- ❌ BAD: Not using PostgreSQL features
|
|
51
|
+
CREATE TABLE users (
|
|
52
|
+
id INTEGER,
|
|
53
|
+
email VARCHAR(255),
|
|
54
|
+
created_at TIMESTAMP
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
-- ✅ GOOD: PostgreSQL-optimized schema
|
|
58
|
+
CREATE TABLE users (
|
|
59
|
+
id BIGSERIAL PRIMARY KEY,
|
|
60
|
+
email CITEXT UNIQUE NOT NULL, -- Case-insensitive email
|
|
61
|
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
|
62
|
+
metadata JSONB DEFAULT '{}',
|
|
63
|
+
CONSTRAINT valid_email CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$')
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
-- Add JSONB GIN index for metadata queries
|
|
67
|
+
CREATE INDEX idx_users_metadata ON users USING gin(metadata);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Custom Types and Domains
|
|
71
|
+
```sql
|
|
72
|
+
-- ❌ BAD: Using generic types for specific data
|
|
73
|
+
CREATE TABLE transactions (
|
|
74
|
+
amount DECIMAL(10,2),
|
|
75
|
+
currency VARCHAR(3),
|
|
76
|
+
status VARCHAR(20)
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
-- ✅ GOOD: PostgreSQL custom types
|
|
80
|
+
CREATE TYPE currency_code AS ENUM ('USD', 'EUR', 'GBP', 'JPY');
|
|
81
|
+
CREATE TYPE transaction_status AS ENUM ('pending', 'completed', 'failed', 'cancelled');
|
|
82
|
+
CREATE DOMAIN positive_amount AS DECIMAL(10,2) CHECK (VALUE > 0);
|
|
83
|
+
|
|
84
|
+
CREATE TABLE transactions (
|
|
85
|
+
amount positive_amount NOT NULL,
|
|
86
|
+
currency currency_code NOT NULL,
|
|
87
|
+
status transaction_status DEFAULT 'pending'
|
|
88
|
+
);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 🔍 PostgreSQL-Specific Anti-Patterns
|
|
92
|
+
|
|
93
|
+
### Performance Anti-Patterns
|
|
94
|
+
- **Avoiding PostgreSQL-specific indexes**: Not using GIN/GiST for appropriate data types
|
|
95
|
+
- **Misusing JSONB**: Treating JSONB like a simple string field
|
|
96
|
+
- **Ignoring array operators**: Using inefficient array operations
|
|
97
|
+
- **Poor partition key selection**: Not leveraging PostgreSQL partitioning effectively
|
|
98
|
+
|
|
99
|
+
### Schema Design Issues
|
|
100
|
+
- **Not using ENUM types**: Using VARCHAR for limited value sets
|
|
101
|
+
- **Ignoring constraints**: Missing CHECK constraints for data validation
|
|
102
|
+
- **Wrong data types**: Using VARCHAR instead of TEXT or CITEXT
|
|
103
|
+
- **Missing JSONB structure**: Unstructured JSONB without validation
|
|
104
|
+
|
|
105
|
+
### Function and Trigger Issues
|
|
106
|
+
```sql
|
|
107
|
+
-- ❌ BAD: Inefficient trigger function
|
|
108
|
+
CREATE OR REPLACE FUNCTION update_modified_time()
|
|
109
|
+
RETURNS TRIGGER AS $$
|
|
110
|
+
BEGIN
|
|
111
|
+
NEW.updated_at = NOW(); -- Should use TIMESTAMPTZ
|
|
112
|
+
RETURN NEW;
|
|
113
|
+
END;
|
|
114
|
+
$$ LANGUAGE plpgsql;
|
|
115
|
+
|
|
116
|
+
-- ✅ GOOD: Optimized trigger function
|
|
117
|
+
CREATE OR REPLACE FUNCTION update_modified_time()
|
|
118
|
+
RETURNS TRIGGER AS $$
|
|
119
|
+
BEGIN
|
|
120
|
+
NEW.updated_at = CURRENT_TIMESTAMP;
|
|
121
|
+
RETURN NEW;
|
|
122
|
+
END;
|
|
123
|
+
$$ LANGUAGE plpgsql;
|
|
124
|
+
|
|
125
|
+
-- Set trigger to fire only when needed
|
|
126
|
+
CREATE TRIGGER update_modified_time_trigger
|
|
127
|
+
BEFORE UPDATE ON table_name
|
|
128
|
+
FOR EACH ROW
|
|
129
|
+
WHEN (OLD.* IS DISTINCT FROM NEW.*)
|
|
130
|
+
EXECUTE FUNCTION update_modified_time();
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## 📊 PostgreSQL Extension Usage Review
|
|
134
|
+
|
|
135
|
+
### Extension Best Practices
|
|
136
|
+
```sql
|
|
137
|
+
-- ✅ Check if extension exists before creating
|
|
138
|
+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
|
139
|
+
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
|
140
|
+
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
|
|
141
|
+
|
|
142
|
+
-- ✅ Use extensions appropriately
|
|
143
|
+
-- UUID generation
|
|
144
|
+
SELECT uuid_generate_v4();
|
|
145
|
+
|
|
146
|
+
-- Password hashing
|
|
147
|
+
SELECT crypt('password', gen_salt('bf'));
|
|
148
|
+
|
|
149
|
+
-- Fuzzy text matching
|
|
150
|
+
SELECT word_similarity('postgres', 'postgre');
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## 🛡️ PostgreSQL Security Review
|
|
154
|
+
|
|
155
|
+
### Row Level Security (RLS)
|
|
156
|
+
```sql
|
|
157
|
+
-- ✅ GOOD: Implementing RLS
|
|
158
|
+
ALTER TABLE sensitive_data ENABLE ROW LEVEL SECURITY;
|
|
159
|
+
|
|
160
|
+
CREATE POLICY user_data_policy ON sensitive_data
|
|
161
|
+
FOR ALL TO application_role
|
|
162
|
+
USING (user_id = current_setting('app.current_user_id')::INTEGER);
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Privilege Management
|
|
166
|
+
```sql
|
|
167
|
+
-- ❌ BAD: Overly broad permissions
|
|
168
|
+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO app_user;
|
|
169
|
+
|
|
170
|
+
-- ✅ GOOD: Granular permissions
|
|
171
|
+
GRANT SELECT, INSERT, UPDATE ON specific_table TO app_user;
|
|
172
|
+
GRANT USAGE ON SEQUENCE specific_table_id_seq TO app_user;
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## 🎯 PostgreSQL Code Quality Checklist
|
|
176
|
+
|
|
177
|
+
### Schema Design
|
|
178
|
+
- [ ] Using appropriate PostgreSQL data types (CITEXT, JSONB, arrays)
|
|
179
|
+
- [ ] Leveraging ENUM types for constrained values
|
|
180
|
+
- [ ] Implementing proper CHECK constraints
|
|
181
|
+
- [ ] Using TIMESTAMPTZ instead of TIMESTAMP
|
|
182
|
+
- [ ] Defining custom domains for reusable constraints
|
|
183
|
+
|
|
184
|
+
### Performance Considerations
|
|
185
|
+
- [ ] Appropriate index types (GIN for JSONB/arrays, GiST for ranges)
|
|
186
|
+
- [ ] JSONB queries using containment operators (@>, ?)
|
|
187
|
+
- [ ] Array operations using PostgreSQL-specific operators
|
|
188
|
+
- [ ] Proper use of window functions and CTEs
|
|
189
|
+
- [ ] Efficient use of PostgreSQL-specific functions
|
|
190
|
+
|
|
191
|
+
### PostgreSQL Features Utilization
|
|
192
|
+
- [ ] Using extensions where appropriate
|
|
193
|
+
- [ ] Implementing stored procedures in PL/pgSQL when beneficial
|
|
194
|
+
- [ ] Leveraging PostgreSQL's advanced SQL features
|
|
195
|
+
- [ ] Using PostgreSQL-specific optimization techniques
|
|
196
|
+
- [ ] Implementing proper error handling in functions
|
|
197
|
+
|
|
198
|
+
### Security and Compliance
|
|
199
|
+
- [ ] Row Level Security (RLS) implementation where needed
|
|
200
|
+
- [ ] Proper role and privilege management
|
|
201
|
+
- [ ] Using PostgreSQL's built-in encryption functions
|
|
202
|
+
- [ ] Implementing audit trails with PostgreSQL features
|
|
203
|
+
|
|
204
|
+
## 📝 PostgreSQL-Specific Review Guidelines
|
|
205
|
+
|
|
206
|
+
1. **Data Type Optimization**: Ensure PostgreSQL-specific types are used appropriately
|
|
207
|
+
2. **Index Strategy**: Review index types and ensure PostgreSQL-specific indexes are utilized
|
|
208
|
+
3. **JSONB Structure**: Validate JSONB schema design and query patterns
|
|
209
|
+
4. **Function Quality**: Review PL/pgSQL functions for efficiency and best practices
|
|
210
|
+
5. **Extension Usage**: Verify appropriate use of PostgreSQL extensions
|
|
211
|
+
6. **Performance Features**: Check utilization of PostgreSQL's advanced features
|
|
212
|
+
7. **Security Implementation**: Review PostgreSQL-specific security features
|
|
213
|
+
|
|
214
|
+
Focus on PostgreSQL's unique capabilities and ensure the code leverages what makes PostgreSQL special rather than treating it as a generic SQL database.
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
---
|
|
2
|
+
mode: 'agent'
|
|
3
|
+
tools: ['changes', 'search/codebase', 'edit/editFiles', 'problems']
|
|
4
|
+
description: 'PostgreSQL-specific development assistant focusing on unique PostgreSQL features, advanced data types, and PostgreSQL-exclusive capabilities. Covers JSONB operations, array types, custom types, range/geometric types, full-text search, window functions, and PostgreSQL extensions ecosystem.'
|
|
5
|
+
tested_with: 'GitHub Copilot Chat (GPT-4o) - Validated July 20, 2025'
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# PostgreSQL Development Assistant
|
|
9
|
+
|
|
10
|
+
Expert PostgreSQL guidance for ${selection} (or entire project if no selection). Focus on PostgreSQL-specific features, optimization patterns, and advanced capabilities.
|
|
11
|
+
|
|
12
|
+
## � PostgreSQL-Specific Features
|
|
13
|
+
|
|
14
|
+
### JSONB Operations
|
|
15
|
+
```sql
|
|
16
|
+
-- Advanced JSONB queries
|
|
17
|
+
CREATE TABLE events (
|
|
18
|
+
id SERIAL PRIMARY KEY,
|
|
19
|
+
data JSONB NOT NULL,
|
|
20
|
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
-- GIN index for JSONB performance
|
|
24
|
+
CREATE INDEX idx_events_data_gin ON events USING gin(data);
|
|
25
|
+
|
|
26
|
+
-- JSONB containment and path queries
|
|
27
|
+
SELECT * FROM events
|
|
28
|
+
WHERE data @> '{"type": "login"}'
|
|
29
|
+
AND data #>> '{user,role}' = 'admin';
|
|
30
|
+
|
|
31
|
+
-- JSONB aggregation
|
|
32
|
+
SELECT jsonb_agg(data) FROM events WHERE data ? 'user_id';
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Array Operations
|
|
36
|
+
```sql
|
|
37
|
+
-- PostgreSQL arrays
|
|
38
|
+
CREATE TABLE posts (
|
|
39
|
+
id SERIAL PRIMARY KEY,
|
|
40
|
+
tags TEXT[],
|
|
41
|
+
categories INTEGER[]
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
-- Array queries and operations
|
|
45
|
+
SELECT * FROM posts WHERE 'postgresql' = ANY(tags);
|
|
46
|
+
SELECT * FROM posts WHERE tags && ARRAY['database', 'sql'];
|
|
47
|
+
SELECT * FROM posts WHERE array_length(tags, 1) > 3;
|
|
48
|
+
|
|
49
|
+
-- Array aggregation
|
|
50
|
+
SELECT array_agg(DISTINCT category) FROM posts, unnest(categories) as category;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Window Functions & Analytics
|
|
54
|
+
```sql
|
|
55
|
+
-- Advanced window functions
|
|
56
|
+
SELECT
|
|
57
|
+
product_id,
|
|
58
|
+
sale_date,
|
|
59
|
+
amount,
|
|
60
|
+
-- Running totals
|
|
61
|
+
SUM(amount) OVER (PARTITION BY product_id ORDER BY sale_date) as running_total,
|
|
62
|
+
-- Moving averages
|
|
63
|
+
AVG(amount) OVER (PARTITION BY product_id ORDER BY sale_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) as moving_avg,
|
|
64
|
+
-- Rankings
|
|
65
|
+
DENSE_RANK() OVER (PARTITION BY EXTRACT(month FROM sale_date) ORDER BY amount DESC) as monthly_rank,
|
|
66
|
+
-- Lag/Lead for comparisons
|
|
67
|
+
LAG(amount, 1) OVER (PARTITION BY product_id ORDER BY sale_date) as prev_amount
|
|
68
|
+
FROM sales;
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Full-Text Search
|
|
72
|
+
```sql
|
|
73
|
+
-- PostgreSQL full-text search
|
|
74
|
+
CREATE TABLE documents (
|
|
75
|
+
id SERIAL PRIMARY KEY,
|
|
76
|
+
title TEXT,
|
|
77
|
+
content TEXT,
|
|
78
|
+
search_vector tsvector
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
-- Update search vector
|
|
82
|
+
UPDATE documents
|
|
83
|
+
SET search_vector = to_tsvector('english', title || ' ' || content);
|
|
84
|
+
|
|
85
|
+
-- GIN index for search performance
|
|
86
|
+
CREATE INDEX idx_documents_search ON documents USING gin(search_vector);
|
|
87
|
+
|
|
88
|
+
-- Search queries
|
|
89
|
+
SELECT * FROM documents
|
|
90
|
+
WHERE search_vector @@ plainto_tsquery('english', 'postgresql database');
|
|
91
|
+
|
|
92
|
+
-- Ranking results
|
|
93
|
+
SELECT *, ts_rank(search_vector, plainto_tsquery('postgresql')) as rank
|
|
94
|
+
FROM documents
|
|
95
|
+
WHERE search_vector @@ plainto_tsquery('postgresql')
|
|
96
|
+
ORDER BY rank DESC;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## � PostgreSQL Performance Tuning
|
|
100
|
+
|
|
101
|
+
### Query Optimization
|
|
102
|
+
```sql
|
|
103
|
+
-- EXPLAIN ANALYZE for performance analysis
|
|
104
|
+
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
|
105
|
+
SELECT u.name, COUNT(o.id) as order_count
|
|
106
|
+
FROM users u
|
|
107
|
+
LEFT JOIN orders o ON u.id = o.user_id
|
|
108
|
+
WHERE u.created_at > '2024-01-01'::date
|
|
109
|
+
GROUP BY u.id, u.name;
|
|
110
|
+
|
|
111
|
+
-- Identify slow queries from pg_stat_statements
|
|
112
|
+
SELECT query, calls, total_time, mean_time, rows,
|
|
113
|
+
100.0 * shared_blks_hit / nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
|
|
114
|
+
FROM pg_stat_statements
|
|
115
|
+
ORDER BY total_time DESC
|
|
116
|
+
LIMIT 10;
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Index Strategies
|
|
120
|
+
```sql
|
|
121
|
+
-- Composite indexes for multi-column queries
|
|
122
|
+
CREATE INDEX idx_orders_user_date ON orders(user_id, order_date);
|
|
123
|
+
|
|
124
|
+
-- Partial indexes for filtered queries
|
|
125
|
+
CREATE INDEX idx_active_users ON users(created_at) WHERE status = 'active';
|
|
126
|
+
|
|
127
|
+
-- Expression indexes for computed values
|
|
128
|
+
CREATE INDEX idx_users_lower_email ON users(lower(email));
|
|
129
|
+
|
|
130
|
+
-- Covering indexes to avoid table lookups
|
|
131
|
+
CREATE INDEX idx_orders_covering ON orders(user_id, status) INCLUDE (total, created_at);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Connection & Memory Management
|
|
135
|
+
```sql
|
|
136
|
+
-- Check connection usage
|
|
137
|
+
SELECT count(*) as connections, state
|
|
138
|
+
FROM pg_stat_activity
|
|
139
|
+
GROUP BY state;
|
|
140
|
+
|
|
141
|
+
-- Monitor memory usage
|
|
142
|
+
SELECT name, setting, unit
|
|
143
|
+
FROM pg_settings
|
|
144
|
+
WHERE name IN ('shared_buffers', 'work_mem', 'maintenance_work_mem');
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## �️ PostgreSQL Advanced Data Types
|
|
148
|
+
|
|
149
|
+
### Custom Types & Domains
|
|
150
|
+
```sql
|
|
151
|
+
-- Create custom types
|
|
152
|
+
CREATE TYPE address_type AS (
|
|
153
|
+
street TEXT,
|
|
154
|
+
city TEXT,
|
|
155
|
+
postal_code TEXT,
|
|
156
|
+
country TEXT
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
CREATE TYPE order_status AS ENUM ('pending', 'processing', 'shipped', 'delivered', 'cancelled');
|
|
160
|
+
|
|
161
|
+
-- Use domains for data validation
|
|
162
|
+
CREATE DOMAIN email_address AS TEXT
|
|
163
|
+
CHECK (VALUE ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$');
|
|
164
|
+
|
|
165
|
+
-- Table using custom types
|
|
166
|
+
CREATE TABLE customers (
|
|
167
|
+
id SERIAL PRIMARY KEY,
|
|
168
|
+
email email_address NOT NULL,
|
|
169
|
+
address address_type,
|
|
170
|
+
status order_status DEFAULT 'pending'
|
|
171
|
+
);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Range Types
|
|
175
|
+
```sql
|
|
176
|
+
-- PostgreSQL range types
|
|
177
|
+
CREATE TABLE reservations (
|
|
178
|
+
id SERIAL PRIMARY KEY,
|
|
179
|
+
room_id INTEGER,
|
|
180
|
+
reservation_period tstzrange,
|
|
181
|
+
price_range numrange
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
-- Range queries
|
|
185
|
+
SELECT * FROM reservations
|
|
186
|
+
WHERE reservation_period && tstzrange('2024-07-20', '2024-07-25');
|
|
187
|
+
|
|
188
|
+
-- Exclude overlapping ranges
|
|
189
|
+
ALTER TABLE reservations
|
|
190
|
+
ADD CONSTRAINT no_overlap
|
|
191
|
+
EXCLUDE USING gist (room_id WITH =, reservation_period WITH &&);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Geometric Types
|
|
195
|
+
```sql
|
|
196
|
+
-- PostgreSQL geometric types
|
|
197
|
+
CREATE TABLE locations (
|
|
198
|
+
id SERIAL PRIMARY KEY,
|
|
199
|
+
name TEXT,
|
|
200
|
+
coordinates POINT,
|
|
201
|
+
coverage CIRCLE,
|
|
202
|
+
service_area POLYGON
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
-- Geometric queries
|
|
206
|
+
SELECT name FROM locations
|
|
207
|
+
WHERE coordinates <-> point(40.7128, -74.0060) < 10; -- Within 10 units
|
|
208
|
+
|
|
209
|
+
-- GiST index for geometric data
|
|
210
|
+
CREATE INDEX idx_locations_coords ON locations USING gist(coordinates);
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## 📊 PostgreSQL Extensions & Tools
|
|
214
|
+
|
|
215
|
+
### Useful Extensions
|
|
216
|
+
```sql
|
|
217
|
+
-- Enable commonly used extensions
|
|
218
|
+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; -- UUID generation
|
|
219
|
+
CREATE EXTENSION IF NOT EXISTS "pgcrypto"; -- Cryptographic functions
|
|
220
|
+
CREATE EXTENSION IF NOT EXISTS "unaccent"; -- Remove accents from text
|
|
221
|
+
CREATE EXTENSION IF NOT EXISTS "pg_trgm"; -- Trigram matching
|
|
222
|
+
CREATE EXTENSION IF NOT EXISTS "btree_gin"; -- GIN indexes for btree types
|
|
223
|
+
|
|
224
|
+
-- Using extensions
|
|
225
|
+
SELECT uuid_generate_v4(); -- Generate UUIDs
|
|
226
|
+
SELECT crypt('password', gen_salt('bf')); -- Hash passwords
|
|
227
|
+
SELECT similarity('postgresql', 'postgersql'); -- Fuzzy matching
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Monitoring & Maintenance
|
|
231
|
+
```sql
|
|
232
|
+
-- Database size and growth
|
|
233
|
+
SELECT pg_size_pretty(pg_database_size(current_database())) as db_size;
|
|
234
|
+
|
|
235
|
+
-- Table and index sizes
|
|
236
|
+
SELECT schemaname, tablename,
|
|
237
|
+
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) as size
|
|
238
|
+
FROM pg_tables
|
|
239
|
+
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
|
|
240
|
+
|
|
241
|
+
-- Index usage statistics
|
|
242
|
+
SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read, idx_tup_fetch
|
|
243
|
+
FROM pg_stat_user_indexes
|
|
244
|
+
WHERE idx_scan = 0; -- Unused indexes
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### PostgreSQL-Specific Optimization Tips
|
|
248
|
+
- **Use EXPLAIN (ANALYZE, BUFFERS)** for detailed query analysis
|
|
249
|
+
- **Configure postgresql.conf** for your workload (OLTP vs OLAP)
|
|
250
|
+
- **Use connection pooling** (pgbouncer) for high-concurrency applications
|
|
251
|
+
- **Regular VACUUM and ANALYZE** for optimal performance
|
|
252
|
+
- **Partition large tables** using PostgreSQL 10+ declarative partitioning
|
|
253
|
+
- **Use pg_stat_statements** for query performance monitoring
|
|
254
|
+
|
|
255
|
+
## 📊 Monitoring and Maintenance
|
|
256
|
+
|
|
257
|
+
### Query Performance Monitoring
|
|
258
|
+
```sql
|
|
259
|
+
-- Identify slow queries
|
|
260
|
+
SELECT query, calls, total_time, mean_time, rows
|
|
261
|
+
FROM pg_stat_statements
|
|
262
|
+
ORDER BY total_time DESC
|
|
263
|
+
LIMIT 10;
|
|
264
|
+
|
|
265
|
+
-- Check index usage
|
|
266
|
+
SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read, idx_tup_fetch
|
|
267
|
+
FROM pg_stat_user_indexes
|
|
268
|
+
WHERE idx_scan = 0;
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Database Maintenance
|
|
272
|
+
- **VACUUM and ANALYZE**: Regular maintenance for performance
|
|
273
|
+
- **Index Maintenance**: Monitor and rebuild fragmented indexes
|
|
274
|
+
- **Statistics Updates**: Keep query planner statistics current
|
|
275
|
+
- **Log Analysis**: Regular review of PostgreSQL logs
|
|
276
|
+
|
|
277
|
+
## 🛠️ Common Query Patterns
|
|
278
|
+
|
|
279
|
+
### Pagination
|
|
280
|
+
```sql
|
|
281
|
+
-- ❌ BAD: OFFSET for large datasets
|
|
282
|
+
SELECT * FROM products ORDER BY id OFFSET 10000 LIMIT 20;
|
|
283
|
+
|
|
284
|
+
-- ✅ GOOD: Cursor-based pagination
|
|
285
|
+
SELECT * FROM products
|
|
286
|
+
WHERE id > $last_id
|
|
287
|
+
ORDER BY id
|
|
288
|
+
LIMIT 20;
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Aggregation
|
|
292
|
+
```sql
|
|
293
|
+
-- ❌ BAD: Inefficient grouping
|
|
294
|
+
SELECT user_id, COUNT(*)
|
|
295
|
+
FROM orders
|
|
296
|
+
WHERE order_date >= '2024-01-01'
|
|
297
|
+
GROUP BY user_id;
|
|
298
|
+
|
|
299
|
+
-- ✅ GOOD: Optimized with partial index
|
|
300
|
+
CREATE INDEX idx_orders_recent ON orders(user_id)
|
|
301
|
+
WHERE order_date >= '2024-01-01';
|
|
302
|
+
|
|
303
|
+
SELECT user_id, COUNT(*)
|
|
304
|
+
FROM orders
|
|
305
|
+
WHERE order_date >= '2024-01-01'
|
|
306
|
+
GROUP BY user_id;
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### JSON Queries
|
|
310
|
+
```sql
|
|
311
|
+
-- ❌ BAD: Inefficient JSON querying
|
|
312
|
+
SELECT * FROM users WHERE data::text LIKE '%admin%';
|
|
313
|
+
|
|
314
|
+
-- ✅ GOOD: JSONB operators and GIN index
|
|
315
|
+
CREATE INDEX idx_users_data_gin ON users USING gin(data);
|
|
316
|
+
|
|
317
|
+
SELECT * FROM users WHERE data @> '{"role": "admin"}';
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## 📋 Optimization Checklist
|
|
321
|
+
|
|
322
|
+
### Query Analysis
|
|
323
|
+
- [ ] Run EXPLAIN ANALYZE for expensive queries
|
|
324
|
+
- [ ] Check for sequential scans on large tables
|
|
325
|
+
- [ ] Verify appropriate join algorithms
|
|
326
|
+
- [ ] Review WHERE clause selectivity
|
|
327
|
+
- [ ] Analyze sort and aggregation operations
|
|
328
|
+
|
|
329
|
+
### Index Strategy
|
|
330
|
+
- [ ] Create indexes for frequently queried columns
|
|
331
|
+
- [ ] Use composite indexes for multi-column searches
|
|
332
|
+
- [ ] Consider partial indexes for filtered queries
|
|
333
|
+
- [ ] Remove unused or duplicate indexes
|
|
334
|
+
- [ ] Monitor index bloat and fragmentation
|
|
335
|
+
|
|
336
|
+
### Security Review
|
|
337
|
+
- [ ] Use parameterized queries exclusively
|
|
338
|
+
- [ ] Implement proper access controls
|
|
339
|
+
- [ ] Enable row-level security where needed
|
|
340
|
+
- [ ] Audit sensitive data access
|
|
341
|
+
- [ ] Use secure connection methods
|
|
342
|
+
|
|
343
|
+
### Performance Monitoring
|
|
344
|
+
- [ ] Set up query performance monitoring
|
|
345
|
+
- [ ] Configure appropriate log settings
|
|
346
|
+
- [ ] Monitor connection pool usage
|
|
347
|
+
- [ ] Track database growth and maintenance needs
|
|
348
|
+
- [ ] Set up alerting for performance degradation
|
|
349
|
+
|
|
350
|
+
## 🎯 Optimization Output Format
|
|
351
|
+
|
|
352
|
+
### Query Analysis Results
|
|
353
|
+
```
|
|
354
|
+
## Query Performance Analysis
|
|
355
|
+
|
|
356
|
+
**Original Query**:
|
|
357
|
+
[Original SQL with performance issues]
|
|
358
|
+
|
|
359
|
+
**Issues Identified**:
|
|
360
|
+
- Sequential scan on large table (Cost: 15000.00)
|
|
361
|
+
- Missing index on frequently queried column
|
|
362
|
+
- Inefficient join order
|
|
363
|
+
|
|
364
|
+
**Optimized Query**:
|
|
365
|
+
[Improved SQL with explanations]
|
|
366
|
+
|
|
367
|
+
**Recommended Indexes**:
|
|
368
|
+
```sql
|
|
369
|
+
CREATE INDEX idx_table_column ON table(column);
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
**Performance Impact**: Expected 80% improvement in execution time
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## 🚀 Advanced PostgreSQL Features
|
|
376
|
+
|
|
377
|
+
### Window Functions
|
|
378
|
+
```sql
|
|
379
|
+
-- Running totals and rankings
|
|
380
|
+
SELECT
|
|
381
|
+
product_id,
|
|
382
|
+
order_date,
|
|
383
|
+
amount,
|
|
384
|
+
SUM(amount) OVER (PARTITION BY product_id ORDER BY order_date) as running_total,
|
|
385
|
+
ROW_NUMBER() OVER (PARTITION BY product_id ORDER BY amount DESC) as rank
|
|
386
|
+
FROM sales;
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### Common Table Expressions (CTEs)
|
|
390
|
+
```sql
|
|
391
|
+
-- Recursive queries for hierarchical data
|
|
392
|
+
WITH RECURSIVE category_tree AS (
|
|
393
|
+
SELECT id, name, parent_id, 1 as level
|
|
394
|
+
FROM categories
|
|
395
|
+
WHERE parent_id IS NULL
|
|
396
|
+
|
|
397
|
+
UNION ALL
|
|
398
|
+
|
|
399
|
+
SELECT c.id, c.name, c.parent_id, ct.level + 1
|
|
400
|
+
FROM categories c
|
|
401
|
+
JOIN category_tree ct ON c.parent_id = ct.id
|
|
402
|
+
)
|
|
403
|
+
SELECT * FROM category_tree ORDER BY level, name;
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
Focus on providing specific, actionable PostgreSQL optimizations that improve query performance, security, and maintainability while leveraging PostgreSQL's advanced features.
|