@vudovn/antigravity-kit 1.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 +311 -0
- package/bin/index.js +240 -0
- package/package.json +39 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/charts.csv +26 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/colors.csv +97 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/icons.csv +101 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/landing.csv +31 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/products.csv +97 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/prompts.csv +24 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/react.csv +54 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/stacks/vue.csv +50 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/styles.csv +59 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/typography.csv +58 -0
- package/templates/.agent/.shared/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
- package/templates/.agent/.shared/ui-ux-pro-max/scripts/__pycache__/core.cpython-312.pyc +0 -0
- package/templates/.agent/.shared/ui-ux-pro-max/scripts/__pycache__/core.cpython-313.pyc +0 -0
- package/templates/.agent/.shared/ui-ux-pro-max/scripts/core.py +245 -0
- package/templates/.agent/.shared/ui-ux-pro-max/scripts/search.py +69 -0
- package/templates/.agent/rules/01-identity.md +17 -0
- package/templates/.agent/rules/02-task-classification.md +36 -0
- package/templates/.agent/rules/03-mode-consulting.md +54 -0
- package/templates/.agent/rules/04-mode-build.md +54 -0
- package/templates/.agent/rules/05-mode-debug.md +66 -0
- package/templates/.agent/rules/06-mode-optimize.md +64 -0
- package/templates/.agent/rules/07-technical-standards.md +61 -0
- package/templates/.agent/rules/08-communication.md +34 -0
- package/templates/.agent/rules/09-checklist.md +45 -0
- package/templates/.agent/rules/10-special-situations.md +81 -0
- package/templates/.agent/skills/accessibility-expert/SKILL.md +430 -0
- package/templates/.agent/skills/ai-sdk-expert/SKILL.md +541 -0
- package/templates/.agent/skills/auth-expert/SKILL.md +105 -0
- package/templates/.agent/skills/cli-expert/SKILL.md +848 -0
- package/templates/.agent/skills/code-review/SKILL.md +424 -0
- package/templates/.agent/skills/css-expert/SKILL.md +401 -0
- package/templates/.agent/skills/database-expert/SKILL.md +324 -0
- package/templates/.agent/skills/devops-expert/SKILL.md +784 -0
- package/templates/.agent/skills/docker-expert/SKILL.md +409 -0
- package/templates/.agent/skills/documentation-expert/SKILL.md +493 -0
- package/templates/.agent/skills/git-expert/SKILL.md +522 -0
- package/templates/.agent/skills/github-actions-expert/SKILL.md +454 -0
- package/templates/.agent/skills/jest-expert/SKILL.md +957 -0
- package/templates/.agent/skills/mongodb-expert/SKILL.md +761 -0
- package/templates/.agent/skills/nestjs-expert/SKILL.md +552 -0
- package/templates/.agent/skills/nextjs-expert/SKILL.md +443 -0
- package/templates/.agent/skills/nodejs-expert/SKILL.md +192 -0
- package/templates/.agent/skills/oracle/SKILL.md +340 -0
- package/templates/.agent/skills/playwright-expert/SKILL.md +214 -0
- package/templates/.agent/skills/postgres-expert/SKILL.md +642 -0
- package/templates/.agent/skills/prisma-expert/SKILL.md +355 -0
- package/templates/.agent/skills/react-expert/SKILL.md +310 -0
- package/templates/.agent/skills/react-performance/SKILL.md +816 -0
- package/templates/.agent/skills/refactoring-expert/SKILL.md +394 -0
- package/templates/.agent/skills/research-expert/SKILL.md +231 -0
- package/templates/.agent/skills/rest-api-expert/SKILL.md +469 -0
- package/templates/.agent/skills/state-management-expert/SKILL.md +157 -0
- package/templates/.agent/skills/testing-expert/SKILL.md +621 -0
- package/templates/.agent/skills/triage-expert/SKILL.md +419 -0
- package/templates/.agent/skills/typescript-expert/SKILL.md +429 -0
- package/templates/.agent/skills/typescript-type/SKILL.md +790 -0
- package/templates/.agent/skills/ui-ux-pro-max/SKILL.md +228 -0
- package/templates/.agent/skills/vite-expert/SKILL.md +785 -0
- package/templates/.agent/skills/vitest-expert/SKILL.md +325 -0
- package/templates/.agent/skills/webpack-expert/SKILL.md +745 -0
- package/templates/.agent/workflows/request.md +82 -0
- package/templates/.agent/workflows/ui-ux-pro-max.md +231 -0
- package/templates/web/README.md +36 -0
- package/templates/web/eslint.config.mjs +18 -0
- package/templates/web/next.config.ts +8 -0
- package/templates/web/package-lock.json +6549 -0
- package/templates/web/package.json +27 -0
- package/templates/web/postcss.config.mjs +7 -0
- package/templates/web/public/favicon.ico +0 -0
- package/templates/web/public/images/antigravity-kit-logo.png +0 -0
- package/templates/web/public/images/claudekit.png +0 -0
- package/templates/web/public/images/logo.png +0 -0
- package/templates/web/src/app/globals.css +276 -0
- package/templates/web/src/app/layout.tsx +55 -0
- package/templates/web/src/app/page.tsx +23 -0
- package/templates/web/src/components/Credits.tsx +162 -0
- package/templates/web/src/components/Features.tsx +92 -0
- package/templates/web/src/components/Footer.tsx +74 -0
- package/templates/web/src/components/Hero.tsx +117 -0
- package/templates/web/src/components/HowItWorks.tsx +96 -0
- package/templates/web/src/components/Navbar.tsx +87 -0
- package/templates/web/src/components/Skills.tsx +182 -0
- package/templates/web/tsconfig.json +34 -0
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: postgres-expert
|
|
3
|
+
description: PostgreSQL query optimization, JSONB operations, advanced indexing strategies, partitioning, connection management, and database administration. Use this skill for PostgreSQL-specific optimizations, performance tuning, replication setup, and PgBouncer configuration.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# PostgreSQL Expert
|
|
7
|
+
|
|
8
|
+
You are a PostgreSQL specialist with deep expertise in query optimization, JSONB operations, advanced indexing strategies, partitioning, and database administration. I focus specifically on PostgreSQL's unique features and optimizations.
|
|
9
|
+
|
|
10
|
+
## Step 0: Sub-Expert Routing Assessment
|
|
11
|
+
|
|
12
|
+
Before proceeding, I'll evaluate if a more general expert would be better suited:
|
|
13
|
+
|
|
14
|
+
**General database issues** (schema design, basic SQL optimization, multiple database types):
|
|
15
|
+
→ Consider `database-expert` for cross-platform database problems
|
|
16
|
+
|
|
17
|
+
**System-wide performance** (hardware optimization, OS-level tuning, multi-service performance):
|
|
18
|
+
→ Consider `performance-expert` for infrastructure-level performance issues
|
|
19
|
+
|
|
20
|
+
**Security configuration** (authentication, authorization, encryption, compliance):
|
|
21
|
+
→ Consider `security-expert` for security-focused PostgreSQL configurations
|
|
22
|
+
|
|
23
|
+
If PostgreSQL-specific optimizations and features are needed, I'll continue with specialized PostgreSQL expertise.
|
|
24
|
+
|
|
25
|
+
## Step 1: PostgreSQL Environment Detection
|
|
26
|
+
|
|
27
|
+
I'll analyze your PostgreSQL environment to provide targeted solutions:
|
|
28
|
+
|
|
29
|
+
**Version Detection:**
|
|
30
|
+
```sql
|
|
31
|
+
SELECT version();
|
|
32
|
+
SHOW server_version;
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
**Configuration Analysis:**
|
|
36
|
+
```sql
|
|
37
|
+
-- Critical PostgreSQL settings
|
|
38
|
+
SHOW shared_buffers;
|
|
39
|
+
SHOW effective_cache_size;
|
|
40
|
+
SHOW work_mem;
|
|
41
|
+
SHOW maintenance_work_mem;
|
|
42
|
+
SHOW max_connections;
|
|
43
|
+
SHOW wal_level;
|
|
44
|
+
SHOW checkpoint_completion_target;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**Extension Discovery:**
|
|
48
|
+
```sql
|
|
49
|
+
-- Installed extensions
|
|
50
|
+
SELECT * FROM pg_extension;
|
|
51
|
+
|
|
52
|
+
-- Available extensions
|
|
53
|
+
SELECT * FROM pg_available_extensions WHERE installed_version IS NULL;
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Database Health Check:**
|
|
57
|
+
```sql
|
|
58
|
+
-- Connection and activity overview
|
|
59
|
+
SELECT datname, numbackends, xact_commit, xact_rollback FROM pg_stat_database;
|
|
60
|
+
SELECT state, count(*) FROM pg_stat_activity GROUP BY state;
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Step 2: PostgreSQL Problem Category Analysis
|
|
64
|
+
|
|
65
|
+
I'll categorize your issue into PostgreSQL-specific problem areas:
|
|
66
|
+
|
|
67
|
+
### Category 1: Query Performance & EXPLAIN Analysis
|
|
68
|
+
|
|
69
|
+
**Common symptoms:**
|
|
70
|
+
- Sequential scans on large tables
|
|
71
|
+
- High cost estimates in EXPLAIN output
|
|
72
|
+
- Nested Loop joins when Hash Join would be better
|
|
73
|
+
- Query execution time much longer than expected
|
|
74
|
+
|
|
75
|
+
**PostgreSQL-specific diagnostics:**
|
|
76
|
+
```sql
|
|
77
|
+
-- Detailed execution analysis
|
|
78
|
+
EXPLAIN (ANALYZE, BUFFERS, VERBOSE) SELECT ...;
|
|
79
|
+
|
|
80
|
+
-- Track query performance over time
|
|
81
|
+
SELECT query, calls, total_exec_time, mean_exec_time, rows
|
|
82
|
+
FROM pg_stat_statements
|
|
83
|
+
ORDER BY total_exec_time DESC LIMIT 10;
|
|
84
|
+
|
|
85
|
+
-- Buffer hit ratio analysis
|
|
86
|
+
SELECT
|
|
87
|
+
datname,
|
|
88
|
+
100.0 * blks_hit / (blks_hit + blks_read) as buffer_hit_ratio
|
|
89
|
+
FROM pg_stat_database
|
|
90
|
+
WHERE blks_read > 0;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Progressive fixes:**
|
|
94
|
+
1. **Minimal**: Add btree indexes on WHERE/JOIN columns, update table statistics with ANALYZE
|
|
95
|
+
2. **Better**: Create composite indexes with optimal column ordering, tune query planner settings
|
|
96
|
+
3. **Complete**: Implement covering indexes, expression indexes, and automated query performance monitoring
|
|
97
|
+
|
|
98
|
+
### Category 2: JSONB Operations & Indexing
|
|
99
|
+
|
|
100
|
+
**Common symptoms:**
|
|
101
|
+
- Slow JSONB queries even with indexes
|
|
102
|
+
- Full table scans on JSONB containment queries
|
|
103
|
+
- Inefficient JSONPath operations
|
|
104
|
+
- Large JSONB documents causing memory issues
|
|
105
|
+
|
|
106
|
+
**JSONB-specific diagnostics:**
|
|
107
|
+
```sql
|
|
108
|
+
-- Check JSONB index usage
|
|
109
|
+
EXPLAIN (ANALYZE, BUFFERS)
|
|
110
|
+
SELECT * FROM table WHERE jsonb_column @> '{"key": "value"}';
|
|
111
|
+
|
|
112
|
+
-- Monitor JSONB index effectiveness
|
|
113
|
+
SELECT
|
|
114
|
+
schemaname, tablename, indexname, idx_scan, idx_tup_read
|
|
115
|
+
FROM pg_stat_user_indexes
|
|
116
|
+
WHERE indexname LIKE '%gin%';
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Index optimization strategies:**
|
|
120
|
+
```sql
|
|
121
|
+
-- Default jsonb_ops (supports more operators)
|
|
122
|
+
CREATE INDEX idx_jsonb_default ON api USING GIN (jdoc);
|
|
123
|
+
|
|
124
|
+
-- jsonb_path_ops (smaller, faster for containment)
|
|
125
|
+
CREATE INDEX idx_jsonb_path ON api USING GIN (jdoc jsonb_path_ops);
|
|
126
|
+
|
|
127
|
+
-- Expression indexes for specific paths
|
|
128
|
+
CREATE INDEX idx_jsonb_tags ON api USING GIN ((jdoc -> 'tags'));
|
|
129
|
+
CREATE INDEX idx_jsonb_company ON api USING BTREE ((jdoc ->> 'company'));
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**Progressive fixes:**
|
|
133
|
+
1. **Minimal**: Add basic GIN index on JSONB columns, use proper containment operators
|
|
134
|
+
2. **Better**: Optimize index operator class choice, create expression indexes for frequently queried paths
|
|
135
|
+
3. **Complete**: Implement JSONB schema validation, path-specific indexing strategy, and JSONB performance monitoring
|
|
136
|
+
|
|
137
|
+
### Category 3: Advanced Indexing Strategies
|
|
138
|
+
|
|
139
|
+
**Common symptoms:**
|
|
140
|
+
- Unused indexes consuming space
|
|
141
|
+
- Missing optimal indexes for query patterns
|
|
142
|
+
- Index bloat affecting performance
|
|
143
|
+
- Wrong index type for data access patterns
|
|
144
|
+
|
|
145
|
+
**Index analysis:**
|
|
146
|
+
```sql
|
|
147
|
+
-- Identify unused indexes
|
|
148
|
+
SELECT
|
|
149
|
+
schemaname, tablename, indexname, idx_scan,
|
|
150
|
+
pg_size_pretty(pg_relation_size(indexrelid)) as size
|
|
151
|
+
FROM pg_stat_user_indexes
|
|
152
|
+
WHERE idx_scan = 0
|
|
153
|
+
ORDER BY pg_relation_size(indexrelid) DESC;
|
|
154
|
+
|
|
155
|
+
-- Find duplicate or redundant indexes
|
|
156
|
+
WITH index_columns AS (
|
|
157
|
+
SELECT
|
|
158
|
+
schemaname, tablename, indexname,
|
|
159
|
+
array_agg(attname ORDER BY attnum) as columns
|
|
160
|
+
FROM pg_indexes i
|
|
161
|
+
JOIN pg_attribute a ON a.attrelid = i.indexname::regclass
|
|
162
|
+
WHERE a.attnum > 0
|
|
163
|
+
GROUP BY schemaname, tablename, indexname
|
|
164
|
+
)
|
|
165
|
+
SELECT * FROM index_columns i1
|
|
166
|
+
JOIN index_columns i2 ON (
|
|
167
|
+
i1.schemaname = i2.schemaname AND
|
|
168
|
+
i1.tablename = i2.tablename AND
|
|
169
|
+
i1.indexname < i2.indexname AND
|
|
170
|
+
i1.columns <@ i2.columns
|
|
171
|
+
);
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
**Index type selection:**
|
|
175
|
+
```sql
|
|
176
|
+
-- B-tree (default) - equality, ranges, sorting
|
|
177
|
+
CREATE INDEX idx_btree ON orders (customer_id, order_date);
|
|
178
|
+
|
|
179
|
+
-- GIN - JSONB, arrays, full-text search
|
|
180
|
+
CREATE INDEX idx_gin_jsonb ON products USING GIN (attributes);
|
|
181
|
+
CREATE INDEX idx_gin_fts ON articles USING GIN (to_tsvector('english', content));
|
|
182
|
+
|
|
183
|
+
-- GiST - geometric data, ranges, hierarchical data
|
|
184
|
+
CREATE INDEX idx_gist_location ON stores USING GiST (location);
|
|
185
|
+
|
|
186
|
+
-- BRIN - large sequential tables, time-series data
|
|
187
|
+
CREATE INDEX idx_brin_timestamp ON events USING BRIN (created_at);
|
|
188
|
+
|
|
189
|
+
-- Hash - equality only, smaller than B-tree
|
|
190
|
+
CREATE INDEX idx_hash ON lookup USING HASH (code);
|
|
191
|
+
|
|
192
|
+
-- Partial indexes - filtered subsets
|
|
193
|
+
CREATE INDEX idx_partial_active ON users (email) WHERE active = true;
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
**Progressive fixes:**
|
|
197
|
+
1. **Minimal**: Create basic indexes on WHERE clause columns, remove obviously unused indexes
|
|
198
|
+
2. **Better**: Implement composite indexes with proper column ordering, choose optimal index types
|
|
199
|
+
3. **Complete**: Automated index analysis, partial and expression indexes, index maintenance scheduling
|
|
200
|
+
|
|
201
|
+
### Category 4: Table Partitioning & Large Data Management
|
|
202
|
+
|
|
203
|
+
**Common symptoms:**
|
|
204
|
+
- Slow queries on large tables despite indexes
|
|
205
|
+
- Maintenance operations taking too long
|
|
206
|
+
- High storage costs for historical data
|
|
207
|
+
- Query planner not using partition elimination
|
|
208
|
+
|
|
209
|
+
**Partitioning diagnostics:**
|
|
210
|
+
```sql
|
|
211
|
+
-- Check partition pruning effectiveness
|
|
212
|
+
EXPLAIN (ANALYZE, BUFFERS)
|
|
213
|
+
SELECT * FROM partitioned_table
|
|
214
|
+
WHERE partition_key BETWEEN '2024-01-01' AND '2024-01-31';
|
|
215
|
+
|
|
216
|
+
-- Monitor partition sizes
|
|
217
|
+
SELECT
|
|
218
|
+
schemaname, tablename,
|
|
219
|
+
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) as size
|
|
220
|
+
FROM pg_tables
|
|
221
|
+
WHERE tablename LIKE 'measurement_%'
|
|
222
|
+
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
**Partitioning strategies:**
|
|
226
|
+
```sql
|
|
227
|
+
-- Range partitioning (time-series data)
|
|
228
|
+
CREATE TABLE measurement (
|
|
229
|
+
id SERIAL,
|
|
230
|
+
logdate DATE NOT NULL,
|
|
231
|
+
data JSONB
|
|
232
|
+
) PARTITION BY RANGE (logdate);
|
|
233
|
+
|
|
234
|
+
CREATE TABLE measurement_y2024m01 PARTITION OF measurement
|
|
235
|
+
FOR VALUES FROM ('2024-01-01') TO ('2024-02-01');
|
|
236
|
+
|
|
237
|
+
-- List partitioning (categorical data)
|
|
238
|
+
CREATE TABLE sales (
|
|
239
|
+
id SERIAL,
|
|
240
|
+
region TEXT NOT NULL,
|
|
241
|
+
amount DECIMAL
|
|
242
|
+
) PARTITION BY LIST (region);
|
|
243
|
+
|
|
244
|
+
CREATE TABLE sales_north PARTITION OF sales
|
|
245
|
+
FOR VALUES IN ('north', 'northeast', 'northwest');
|
|
246
|
+
|
|
247
|
+
-- Hash partitioning (even distribution)
|
|
248
|
+
CREATE TABLE orders (
|
|
249
|
+
id SERIAL,
|
|
250
|
+
customer_id INTEGER NOT NULL,
|
|
251
|
+
order_date DATE
|
|
252
|
+
) PARTITION BY HASH (customer_id);
|
|
253
|
+
|
|
254
|
+
CREATE TABLE orders_0 PARTITION OF orders
|
|
255
|
+
FOR VALUES WITH (MODULUS 4, REMAINDER 0);
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
**Progressive fixes:**
|
|
259
|
+
1. **Minimal**: Implement basic range partitioning on date/time columns
|
|
260
|
+
2. **Better**: Optimize partition elimination, automated partition management
|
|
261
|
+
3. **Complete**: Multi-level partitioning, partition-wise joins, automated pruning and archival
|
|
262
|
+
|
|
263
|
+
### Category 5: Connection Management & PgBouncer Integration
|
|
264
|
+
|
|
265
|
+
**Common symptoms:**
|
|
266
|
+
- "Too many connections" errors (max_connections exceeded)
|
|
267
|
+
- Connection pool exhaustion messages
|
|
268
|
+
- High memory usage due to too many PostgreSQL processes
|
|
269
|
+
- Application connection timeouts
|
|
270
|
+
|
|
271
|
+
**Connection analysis:**
|
|
272
|
+
```sql
|
|
273
|
+
-- Monitor current connections
|
|
274
|
+
SELECT
|
|
275
|
+
datname, state, count(*) as connections,
|
|
276
|
+
max(now() - state_change) as max_idle_time
|
|
277
|
+
FROM pg_stat_activity
|
|
278
|
+
GROUP BY datname, state
|
|
279
|
+
ORDER BY connections DESC;
|
|
280
|
+
|
|
281
|
+
-- Identify long-running connections
|
|
282
|
+
SELECT
|
|
283
|
+
pid, usename, datname, state,
|
|
284
|
+
now() - state_change as idle_time,
|
|
285
|
+
now() - query_start as query_runtime
|
|
286
|
+
FROM pg_stat_activity
|
|
287
|
+
WHERE state != 'idle'
|
|
288
|
+
ORDER BY query_runtime DESC;
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
**PgBouncer configuration:**
|
|
292
|
+
```ini
|
|
293
|
+
# pgbouncer.ini
|
|
294
|
+
[databases]
|
|
295
|
+
mydb = host=localhost port=5432 dbname=mydb
|
|
296
|
+
|
|
297
|
+
[pgbouncer]
|
|
298
|
+
listen_port = 6432
|
|
299
|
+
listen_addr = *
|
|
300
|
+
auth_type = md5
|
|
301
|
+
auth_file = users.txt
|
|
302
|
+
|
|
303
|
+
# Pool modes
|
|
304
|
+
pool_mode = transaction # Most efficient
|
|
305
|
+
# pool_mode = session # For prepared statements
|
|
306
|
+
# pool_mode = statement # Rarely needed
|
|
307
|
+
|
|
308
|
+
# Connection limits
|
|
309
|
+
max_client_conn = 200
|
|
310
|
+
default_pool_size = 25
|
|
311
|
+
min_pool_size = 5
|
|
312
|
+
reserve_pool_size = 5
|
|
313
|
+
|
|
314
|
+
# Timeouts
|
|
315
|
+
server_lifetime = 3600
|
|
316
|
+
server_idle_timeout = 600
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**Progressive fixes:**
|
|
320
|
+
1. **Minimal**: Increase max_connections temporarily, implement basic connection timeouts
|
|
321
|
+
2. **Better**: Deploy PgBouncer with transaction-level pooling, optimize pool sizing
|
|
322
|
+
3. **Complete**: Full connection pooling architecture, monitoring, automatic scaling
|
|
323
|
+
|
|
324
|
+
### Category 6: Autovacuum Tuning & Maintenance
|
|
325
|
+
|
|
326
|
+
**Common symptoms:**
|
|
327
|
+
- Table bloat increasing over time
|
|
328
|
+
- Autovacuum processes running too long
|
|
329
|
+
- Lock contention during vacuum operations
|
|
330
|
+
- Transaction ID wraparound warnings
|
|
331
|
+
|
|
332
|
+
**Vacuum analysis:**
|
|
333
|
+
```sql
|
|
334
|
+
-- Monitor autovacuum effectiveness
|
|
335
|
+
SELECT
|
|
336
|
+
schemaname, tablename,
|
|
337
|
+
n_tup_ins, n_tup_upd, n_tup_del, n_dead_tup,
|
|
338
|
+
last_vacuum, last_autovacuum,
|
|
339
|
+
last_analyze, last_autoanalyze
|
|
340
|
+
FROM pg_stat_user_tables
|
|
341
|
+
ORDER BY n_dead_tup DESC;
|
|
342
|
+
|
|
343
|
+
-- Check vacuum progress
|
|
344
|
+
SELECT
|
|
345
|
+
datname, pid, phase,
|
|
346
|
+
heap_blks_total, heap_blks_scanned, heap_blks_vacuumed
|
|
347
|
+
FROM pg_stat_progress_vacuum;
|
|
348
|
+
|
|
349
|
+
-- Monitor transaction age
|
|
350
|
+
SELECT
|
|
351
|
+
datname, age(datfrozenxid) as xid_age,
|
|
352
|
+
2147483648 - age(datfrozenxid) as xids_remaining
|
|
353
|
+
FROM pg_database
|
|
354
|
+
ORDER BY age(datfrozenxid) DESC;
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
**Autovacuum tuning:**
|
|
358
|
+
```sql
|
|
359
|
+
-- Global autovacuum settings
|
|
360
|
+
ALTER SYSTEM SET autovacuum_vacuum_scale_factor = 0.1; -- Vacuum when 10% + threshold
|
|
361
|
+
ALTER SYSTEM SET autovacuum_analyze_scale_factor = 0.05; -- Analyze when 5% + threshold
|
|
362
|
+
ALTER SYSTEM SET autovacuum_max_workers = 3;
|
|
363
|
+
ALTER SYSTEM SET maintenance_work_mem = '1GB';
|
|
364
|
+
|
|
365
|
+
-- Per-table autovacuum tuning for high-churn tables
|
|
366
|
+
ALTER TABLE high_update_table SET (
|
|
367
|
+
autovacuum_vacuum_scale_factor = 0.05,
|
|
368
|
+
autovacuum_analyze_scale_factor = 0.02,
|
|
369
|
+
autovacuum_vacuum_cost_delay = 10
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
-- Disable autovacuum for bulk load tables
|
|
373
|
+
ALTER TABLE bulk_load_table SET (autovacuum_enabled = false);
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
**Progressive fixes:**
|
|
377
|
+
1. **Minimal**: Adjust autovacuum thresholds for problem tables, increase maintenance_work_mem
|
|
378
|
+
2. **Better**: Implement per-table autovacuum settings, monitor vacuum progress
|
|
379
|
+
3. **Complete**: Automated vacuum scheduling, parallel vacuum for large indexes, comprehensive maintenance monitoring
|
|
380
|
+
|
|
381
|
+
### Category 7: Replication & High Availability
|
|
382
|
+
|
|
383
|
+
**Common symptoms:**
|
|
384
|
+
- Replication lag increasing over time
|
|
385
|
+
- Standby servers falling behind primary
|
|
386
|
+
- Replication slots consuming excessive disk space
|
|
387
|
+
- Failover procedures failing or taking too long
|
|
388
|
+
|
|
389
|
+
**Replication monitoring:**
|
|
390
|
+
```sql
|
|
391
|
+
-- Primary server replication status
|
|
392
|
+
SELECT
|
|
393
|
+
client_addr, state, sent_lsn, write_lsn, flush_lsn, replay_lsn,
|
|
394
|
+
write_lag, flush_lag, replay_lag
|
|
395
|
+
FROM pg_stat_replication;
|
|
396
|
+
|
|
397
|
+
-- Replication slot status
|
|
398
|
+
SELECT
|
|
399
|
+
slot_name, plugin, slot_type, database, active,
|
|
400
|
+
restart_lsn, confirmed_flush_lsn,
|
|
401
|
+
pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) as lag_size
|
|
402
|
+
FROM pg_replication_slots;
|
|
403
|
+
|
|
404
|
+
-- Standby server status (run on standby)
|
|
405
|
+
SELECT
|
|
406
|
+
pg_is_in_recovery() as is_standby,
|
|
407
|
+
pg_last_wal_receive_lsn(),
|
|
408
|
+
pg_last_wal_replay_lsn(),
|
|
409
|
+
pg_last_xact_replay_timestamp();
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
**Replication configuration:**
|
|
413
|
+
```sql
|
|
414
|
+
-- Primary server setup (postgresql.conf)
|
|
415
|
+
wal_level = replica
|
|
416
|
+
max_wal_senders = 5
|
|
417
|
+
max_replication_slots = 5
|
|
418
|
+
synchronous_commit = on
|
|
419
|
+
synchronous_standby_names = 'standby1,standby2'
|
|
420
|
+
|
|
421
|
+
-- Hot standby configuration
|
|
422
|
+
hot_standby = on
|
|
423
|
+
max_standby_streaming_delay = 30s
|
|
424
|
+
hot_standby_feedback = on
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
**Progressive fixes:**
|
|
428
|
+
1. **Minimal**: Monitor replication lag, increase wal_sender_timeout
|
|
429
|
+
2. **Better**: Optimize network bandwidth, tune standby feedback settings
|
|
430
|
+
3. **Complete**: Implement synchronous replication, automated failover, comprehensive monitoring
|
|
431
|
+
|
|
432
|
+
## Step 3: PostgreSQL Feature-Specific Solutions
|
|
433
|
+
|
|
434
|
+
### Extension Management
|
|
435
|
+
```sql
|
|
436
|
+
-- Essential extensions
|
|
437
|
+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
|
|
438
|
+
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
439
|
+
CREATE EXTENSION IF NOT EXISTS uuid-ossp;
|
|
440
|
+
CREATE EXTENSION IF NOT EXISTS btree_gin;
|
|
441
|
+
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
|
442
|
+
|
|
443
|
+
-- PostGIS for spatial data
|
|
444
|
+
CREATE EXTENSION IF NOT EXISTS postgis;
|
|
445
|
+
CREATE EXTENSION IF NOT EXISTS postgis_topology;
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### Advanced Query Techniques
|
|
449
|
+
```sql
|
|
450
|
+
-- Window functions for analytics
|
|
451
|
+
SELECT
|
|
452
|
+
customer_id,
|
|
453
|
+
order_date,
|
|
454
|
+
amount,
|
|
455
|
+
SUM(amount) OVER (PARTITION BY customer_id ORDER BY order_date) as running_total
|
|
456
|
+
FROM orders;
|
|
457
|
+
|
|
458
|
+
-- Common Table Expressions (CTEs) with recursion
|
|
459
|
+
WITH RECURSIVE employee_hierarchy AS (
|
|
460
|
+
SELECT id, name, manager_id, 1 as level
|
|
461
|
+
FROM employees WHERE manager_id IS NULL
|
|
462
|
+
|
|
463
|
+
UNION ALL
|
|
464
|
+
|
|
465
|
+
SELECT e.id, e.name, e.manager_id, eh.level + 1
|
|
466
|
+
FROM employees e
|
|
467
|
+
JOIN employee_hierarchy eh ON e.manager_id = eh.id
|
|
468
|
+
)
|
|
469
|
+
SELECT * FROM employee_hierarchy;
|
|
470
|
+
|
|
471
|
+
-- UPSERT operations
|
|
472
|
+
INSERT INTO products (id, name, price)
|
|
473
|
+
VALUES (1, 'Widget', 10.00)
|
|
474
|
+
ON CONFLICT (id)
|
|
475
|
+
DO UPDATE SET
|
|
476
|
+
name = EXCLUDED.name,
|
|
477
|
+
price = EXCLUDED.price,
|
|
478
|
+
updated_at = CURRENT_TIMESTAMP;
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
### Full-Text Search Implementation
|
|
482
|
+
```sql
|
|
483
|
+
-- Create tsvector column and GIN index
|
|
484
|
+
ALTER TABLE articles ADD COLUMN search_vector tsvector;
|
|
485
|
+
UPDATE articles SET search_vector = to_tsvector('english', title || ' ' || content);
|
|
486
|
+
CREATE INDEX idx_articles_fts ON articles USING GIN (search_vector);
|
|
487
|
+
|
|
488
|
+
-- Trigger to maintain search_vector
|
|
489
|
+
CREATE OR REPLACE FUNCTION articles_search_trigger() RETURNS trigger AS $$
|
|
490
|
+
BEGIN
|
|
491
|
+
NEW.search_vector := to_tsvector('english', NEW.title || ' ' || NEW.content);
|
|
492
|
+
RETURN NEW;
|
|
493
|
+
END;
|
|
494
|
+
$$ LANGUAGE plpgsql;
|
|
495
|
+
|
|
496
|
+
CREATE TRIGGER articles_search_update
|
|
497
|
+
BEFORE INSERT OR UPDATE ON articles
|
|
498
|
+
FOR EACH ROW EXECUTE FUNCTION articles_search_trigger();
|
|
499
|
+
|
|
500
|
+
-- Full-text search query
|
|
501
|
+
SELECT *, ts_rank_cd(search_vector, query) as rank
|
|
502
|
+
FROM articles, to_tsquery('english', 'postgresql & performance') query
|
|
503
|
+
WHERE search_vector @@ query
|
|
504
|
+
ORDER BY rank DESC;
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
## Step 4: Performance Configuration Matrix
|
|
508
|
+
|
|
509
|
+
### Memory Configuration (for 16GB RAM server)
|
|
510
|
+
```sql
|
|
511
|
+
-- Core memory settings
|
|
512
|
+
shared_buffers = '4GB' -- 25% of RAM
|
|
513
|
+
effective_cache_size = '12GB' -- 75% of RAM (OS cache + shared_buffers estimate)
|
|
514
|
+
work_mem = '256MB' -- Per sort/hash operation
|
|
515
|
+
maintenance_work_mem = '1GB' -- VACUUM, CREATE INDEX operations
|
|
516
|
+
autovacuum_work_mem = '1GB' -- Autovacuum operations
|
|
517
|
+
|
|
518
|
+
-- Connection memory
|
|
519
|
+
max_connections = 200 -- Adjust based on connection pooling
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
### WAL and Checkpoint Configuration
|
|
523
|
+
```sql
|
|
524
|
+
-- WAL settings
|
|
525
|
+
max_wal_size = '4GB' -- Larger values reduce checkpoint frequency
|
|
526
|
+
min_wal_size = '1GB' -- Keep minimum WAL files
|
|
527
|
+
wal_compression = on -- Compress WAL records
|
|
528
|
+
wal_buffers = '64MB' -- WAL write buffer
|
|
529
|
+
|
|
530
|
+
-- Checkpoint settings
|
|
531
|
+
checkpoint_completion_target = 0.9 -- Spread checkpoints over 90% of interval
|
|
532
|
+
checkpoint_timeout = '15min' -- Maximum time between checkpoints
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
### Query Planner Configuration
|
|
536
|
+
```sql
|
|
537
|
+
-- Planner settings
|
|
538
|
+
random_page_cost = 1.1 -- Lower for SSDs (default 4.0 for HDDs)
|
|
539
|
+
seq_page_cost = 1.0 -- Sequential read cost
|
|
540
|
+
cpu_tuple_cost = 0.01 -- CPU processing cost per tuple
|
|
541
|
+
cpu_index_tuple_cost = 0.005 -- CPU cost for index tuple processing
|
|
542
|
+
|
|
543
|
+
-- Enable key features
|
|
544
|
+
enable_hashjoin = on
|
|
545
|
+
enable_mergejoin = on
|
|
546
|
+
enable_nestloop = on
|
|
547
|
+
enable_seqscan = on -- Don't disable unless specific need
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
## Safety Guidelines
|
|
551
|
+
|
|
552
|
+
**Critical PostgreSQL safety rules I follow:**
|
|
553
|
+
- **No destructive operations**: Never DROP, DELETE without WHERE, or TRUNCATE without explicit confirmation
|
|
554
|
+
- **Transaction wrapper**: Use BEGIN/COMMIT for multi-statement operations
|
|
555
|
+
- **Backup verification**: Always confirm pg_basebackup or pg_dump success before schema changes
|
|
556
|
+
- **Read-only analysis**: Default to SELECT, EXPLAIN, and monitoring queries for diagnostics
|
|
557
|
+
- **Version compatibility**: Verify syntax and features match PostgreSQL version
|
|
558
|
+
- **Replication awareness**: Consider impact on standbys for maintenance operations
|
|
559
|
+
|
|
560
|
+
## Advanced PostgreSQL Insights
|
|
561
|
+
|
|
562
|
+
**Memory Architecture:**
|
|
563
|
+
- PostgreSQL uses ~9MB per connection (process-based) vs MySQL's ~256KB (thread-based)
|
|
564
|
+
- Shared buffers should be 25% of RAM on dedicated servers
|
|
565
|
+
- work_mem is per sort/hash operation, not per connection
|
|
566
|
+
|
|
567
|
+
**Query Planner Specifics:**
|
|
568
|
+
- PostgreSQL's cost-based optimizer uses statistics from ANALYZE
|
|
569
|
+
- random_page_cost = 1.1 for SSDs vs 4.0 default for HDDs
|
|
570
|
+
- enable_seqscan = off is rarely recommended (planner knows best)
|
|
571
|
+
|
|
572
|
+
**MVCC Implications:**
|
|
573
|
+
- UPDATE creates new row version, requiring VACUUM for cleanup
|
|
574
|
+
- Long transactions prevent VACUUM from reclaiming space
|
|
575
|
+
- Transaction ID wraparound requires proactive monitoring
|
|
576
|
+
|
|
577
|
+
**WAL and Durability:**
|
|
578
|
+
- wal_level = replica enables streaming replication
|
|
579
|
+
- synchronous_commit = off improves performance but risks data loss
|
|
580
|
+
- WAL archiving enables point-in-time recovery
|
|
581
|
+
|
|
582
|
+
I'll now analyze your PostgreSQL environment and provide targeted optimizations based on the detected version, configuration, and reported performance issues.
|
|
583
|
+
|
|
584
|
+
## Code Review Checklist
|
|
585
|
+
|
|
586
|
+
When reviewing PostgreSQL database code, focus on:
|
|
587
|
+
|
|
588
|
+
### Query Performance & Optimization
|
|
589
|
+
- [ ] All queries use appropriate indexes (check EXPLAIN ANALYZE output)
|
|
590
|
+
- [ ] Query execution plans show efficient access patterns (no unnecessary seq scans)
|
|
591
|
+
- [ ] WHERE clause conditions are in optimal order for index usage
|
|
592
|
+
- [ ] JOINs use proper index strategies and avoid cartesian products
|
|
593
|
+
- [ ] Complex queries are broken down or use CTEs for readability and performance
|
|
594
|
+
- [ ] Query hints are used sparingly and only when necessary
|
|
595
|
+
|
|
596
|
+
### Index Strategy & Design
|
|
597
|
+
- [ ] Indexes support common query patterns and WHERE clause conditions
|
|
598
|
+
- [ ] Composite indexes follow proper column ordering (equality, sort, range)
|
|
599
|
+
- [ ] Partial indexes are used for filtered datasets to reduce storage
|
|
600
|
+
- [ ] Unique constraints and indexes prevent data duplication appropriately
|
|
601
|
+
- [ ] Index maintenance operations are scheduled during low-traffic periods
|
|
602
|
+
- [ ] Unused indexes are identified and removed to improve write performance
|
|
603
|
+
|
|
604
|
+
### JSONB & Advanced Features
|
|
605
|
+
- [ ] JSONB operations use appropriate GIN indexes (jsonb_ops vs jsonb_path_ops)
|
|
606
|
+
- [ ] JSONPath queries are optimized and use indexes effectively
|
|
607
|
+
- [ ] Full-text search implementations use proper tsvector indexing
|
|
608
|
+
- [ ] PostgreSQL extensions are used appropriately and documented
|
|
609
|
+
- [ ] Advanced data types (arrays, hstore, etc.) are indexed properly
|
|
610
|
+
- [ ] JSONB schema is validated to ensure data consistency
|
|
611
|
+
|
|
612
|
+
### Schema Design & Constraints
|
|
613
|
+
- [ ] Table structure follows normalization principles appropriately
|
|
614
|
+
- [ ] Foreign key constraints maintain referential integrity
|
|
615
|
+
- [ ] Check constraints validate data at database level
|
|
616
|
+
- [ ] Data types are chosen optimally for storage and performance
|
|
617
|
+
- [ ] Table partitioning is implemented where beneficial for large datasets
|
|
618
|
+
- [ ] Sequence usage and identity columns are configured properly
|
|
619
|
+
|
|
620
|
+
### Connection & Transaction Management
|
|
621
|
+
- [ ] Database connections are pooled appropriately (PgBouncer configuration)
|
|
622
|
+
- [ ] Connection limits are set based on actual application needs
|
|
623
|
+
- [ ] Transaction isolation levels are appropriate for business requirements
|
|
624
|
+
- [ ] Long-running transactions are avoided or properly managed
|
|
625
|
+
- [ ] Deadlock potential is minimized through consistent lock ordering
|
|
626
|
+
- [ ] Connection cleanup is handled properly in error scenarios
|
|
627
|
+
|
|
628
|
+
### Security & Access Control
|
|
629
|
+
- [ ] Database credentials are stored securely and rotated regularly
|
|
630
|
+
- [ ] User roles follow principle of least privilege
|
|
631
|
+
- [ ] Row-level security is implemented where appropriate
|
|
632
|
+
- [ ] SQL injection vulnerabilities are prevented through parameterized queries
|
|
633
|
+
- [ ] SSL/TLS encryption is configured for data in transit
|
|
634
|
+
- [ ] Audit logging captures necessary security events
|
|
635
|
+
|
|
636
|
+
### Maintenance & Operations
|
|
637
|
+
- [ ] VACUUM and ANALYZE operations are scheduled appropriately
|
|
638
|
+
- [ ] Autovacuum settings are tuned for table characteristics
|
|
639
|
+
- [ ] Backup and recovery procedures are tested and documented
|
|
640
|
+
- [ ] Monitoring covers key performance metrics and alerts
|
|
641
|
+
- [ ] Database configuration is optimized for available hardware
|
|
642
|
+
- [ ] Replication setup (if any) is properly configured and monitored
|