@baselane/packs 0.1.4 → 0.1.5
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/dist/aliases.js +1 -0
- package/package.json +2 -2
- package/packs/database-review/pack.json +75 -9
- package/packs/database-review/rendered/.claude/agents/database-reviewer.md +75 -2
- package/packs/database-review/rendered/.claude/commands/migration-review.md +6 -0
- package/packs/database-review/rendered/.claude/settings.json +11 -0
- package/packs/database-review/rendered/.claude/skills/database-migrations/SKILL.md +431 -0
- package/packs/database-review/rendered/.claude/skills/mysql-patterns/SKILL.md +414 -0
- package/packs/database-review/rendered/.claude/skills/postgres-patterns/SKILL.md +149 -0
- package/packs/database-review/rendered/.github/copilot-instructions.md +1096 -14
- package/packs/database-review/rendered/AGENTS.md +45 -16
- package/packs/database-review/rendered/CLAUDE.md +4 -0
- package/packs/database-review/rendered/GEMINI.md +987 -0
- package/packs/go-rules/pack.json +65 -9
- package/packs/go-rules/rendered/.claude/agents/go-build-resolver.md +90 -0
- package/packs/go-rules/rendered/.claude/agents/go-reviewer.md +72 -2
- package/packs/go-rules/rendered/.claude/commands/go-build.md +6 -0
- package/packs/go-rules/rendered/.claude/commands/go-review.md +1 -1
- package/packs/go-rules/rendered/.claude/commands/go-test.md +6 -0
- package/packs/go-rules/rendered/.claude/settings.json +9 -0
- package/packs/go-rules/rendered/.claude/skills/golang-patterns/SKILL.md +673 -0
- package/packs/go-rules/rendered/.claude/skills/golang-testing/SKILL.md +719 -0
- package/packs/go-rules/rendered/.github/copilot-instructions.md +1783 -7
- package/packs/go-rules/rendered/AGENTS.md +244 -8
- package/packs/go-rules/rendered/CLAUDE.md +5 -0
- package/packs/go-rules/rendered/GEMINI.md +1394 -0
- package/packs/python-rules/pack.json +87 -10
- package/packs/python-rules/rendered/.claude/agents/django-reviewer.md +159 -0
- package/packs/python-rules/rendered/.claude/agents/fastapi-reviewer.md +70 -0
- package/packs/python-rules/rendered/.claude/agents/python-reviewer.md +92 -2
- package/packs/python-rules/rendered/.claude/commands/django-review.md +6 -0
- package/packs/python-rules/rendered/.claude/commands/fastapi-review.md +6 -0
- package/packs/python-rules/rendered/.claude/commands/python-review.md +1 -1
- package/packs/python-rules/rendered/.claude/settings.json +1 -1
- package/packs/python-rules/rendered/.claude/skills/django-security/SKILL.md +642 -0
- package/packs/python-rules/rendered/.claude/skills/django-tdd/SKILL.md +728 -0
- package/packs/python-rules/rendered/.claude/skills/fastapi-patterns/SKILL.md +512 -0
- package/packs/python-rules/rendered/.claude/skills/python-patterns/SKILL.md +749 -0
- package/packs/python-rules/rendered/.claude/skills/python-testing/SKILL.md +815 -0
- package/packs/python-rules/rendered/.github/copilot-instructions.md +3815 -12
- package/packs/python-rules/rendered/AGENTS.md +73 -13
- package/packs/python-rules/rendered/CLAUDE.md +9 -0
- package/packs/python-rules/rendered/GEMINI.md +3448 -0
- package/packs/security-review/pack.json +51 -7
- package/packs/security-review/rendered/.claude/agents/security-reviewer.md +29 -2
- package/packs/security-review/rendered/.claude/settings.json +11 -0
- package/packs/security-review/rendered/.claude/skills/security-review/SKILL.md +302 -0
- package/packs/security-review/rendered/.claude/skills/security-review/cloud-infrastructure-security.md +171 -0
- package/packs/security-review/rendered/.claude/skills/security-scan-config/SKILL.md +56 -0
- package/packs/security-review/rendered/.github/copilot-instructions.md +658 -15
- package/packs/security-review/rendered/AGENTS.md +283 -17
- package/packs/security-review/rendered/CLAUDE.md +2 -0
- package/packs/security-review/rendered/GEMINI.md +352 -0
- package/packs/software-engineer-harness/pack.json +249 -13
- package/packs/software-engineer-harness/rendered/.claude/agents/code-explorer.md +67 -0
- package/packs/software-engineer-harness/rendered/.claude/agents/performance-optimizer.md +444 -0
- package/packs/software-engineer-harness/rendered/.claude/agents/pr-test-analyzer.md +43 -0
- package/packs/software-engineer-harness/rendered/.claude/commands/feature-dev.md +6 -0
- package/packs/software-engineer-harness/rendered/.claude/settings.json +29 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/api-design/SKILL.md +522 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/architecture-decision-records/SKILL.md +178 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/backend-patterns/SKILL.md +560 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/codebase-onboarding/SKILL.md +232 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/deployment-patterns/SKILL.md +426 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/docker-patterns/SKILL.md +363 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/e2e-testing/SKILL.md +325 -0
- package/packs/software-engineer-harness/rendered/.claude/skills/tdd-workflow/SKILL.md +580 -0
- package/packs/software-engineer-harness/rendered/.github/copilot-instructions.md +3948 -2
- package/packs/software-engineer-harness/rendered/AGENTS.md +194 -4
- package/packs/software-engineer-harness/rendered/CLAUDE.md +20 -0
- package/packs/software-engineer-harness/rendered/GEMINI.md +3208 -0
- package/packs/typescript-rules/pack.json +68 -5
- package/packs/typescript-rules/rendered/.claude/agents/react-reviewer.md +165 -0
- package/packs/typescript-rules/rendered/.claude/agents/type-design-analyzer.md +48 -0
- package/packs/typescript-rules/rendered/.claude/commands/react-review.md +6 -0
- package/packs/typescript-rules/rendered/.claude/settings.json +9 -0
- package/packs/typescript-rules/rendered/.claude/skills/react-patterns/SKILL.md +340 -0
- package/packs/typescript-rules/rendered/.claude/skills/react-performance/SKILL.md +573 -0
- package/packs/typescript-rules/rendered/.claude/skills/react-testing/SKILL.md +422 -0
- package/packs/typescript-rules/rendered/.github/copilot-instructions.md +2459 -53
- package/packs/typescript-rules/rendered/AGENTS.md +893 -17
- package/packs/typescript-rules/rendered/CLAUDE.md +6 -0
- package/packs/typescript-rules/rendered/GEMINI.md +1335 -0
- package/packs/ecc-essentials/pack.json +0 -93
- package/packs/ecc-essentials/rendered/.claude/settings.json +0 -37
- package/packs/ecc-essentials/rendered/.github/copilot-instructions.md +0 -110
- package/packs/ecc-essentials/rendered/AGENTS.md +0 -74
- package/packs/ecc-essentials/rendered/CLAUDE.md +0 -14
- package/packs/ecc-essentials/rendered/GEMINI.md +0 -25
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/agents/build-error-resolver.md +0 -0
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/agents/refactor-cleaner.md +0 -0
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/agents/silent-failure-hunter.md +0 -0
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/agents/tdd-guide.md +0 -0
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/commands/fix-build.md +0 -0
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/commands/hunt-silent-failures.md +0 -0
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/commands/tdd.md +0 -0
- /package/packs/{ecc-essentials → software-engineer-harness}/rendered/.claude/skills/tdd-red-green-refactor/SKILL.md +0 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: mysql-patterns
|
|
3
|
+
description: MySQL and MariaDB schema, query, indexing, transaction, replication, and connection-pool patterns for production backends. Use when designing MySQL/MariaDB tables and indexes, reviewing migrations before they run on large tables, debugging slow queries/lock waits/deadlocks/connection exhaustion, or adding keyset pagination, upserts, full-text search, JSON columns, or queues.
|
|
4
|
+
license: MIT
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
<!-- adapted from affaan-m/ECC (Everything Claude Code) (MIT) — https://github.com/affaan-m/ECC -->
|
|
8
|
+
|
|
9
|
+
# MySQL Patterns
|
|
10
|
+
|
|
11
|
+
Use this skill when working on MySQL or MariaDB schema design, migrations,
|
|
12
|
+
slow-query investigation, queue-style transactions, connection pools, or
|
|
13
|
+
production database configuration. Prefer exact version checks before applying a
|
|
14
|
+
feature-specific pattern because MySQL and MariaDB have diverged in several SQL
|
|
15
|
+
details.
|
|
16
|
+
|
|
17
|
+
## Activation
|
|
18
|
+
|
|
19
|
+
- Designing MySQL or MariaDB tables, indexes, and constraints
|
|
20
|
+
- Reviewing migrations before they run on large production tables
|
|
21
|
+
- Debugging slow queries, lock waits, deadlocks, or connection exhaustion
|
|
22
|
+
- Adding keyset pagination, upserts, full-text search, JSON columns, or queues
|
|
23
|
+
- Configuring application connection pools, read replicas, TLS, or slow logs
|
|
24
|
+
|
|
25
|
+
## Version Check
|
|
26
|
+
|
|
27
|
+
Start by identifying the engine and version:
|
|
28
|
+
|
|
29
|
+
```sql
|
|
30
|
+
SELECT VERSION();
|
|
31
|
+
SHOW VARIABLES LIKE 'version_comment';
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Keep MySQL and MariaDB guidance separate when syntax differs:
|
|
35
|
+
|
|
36
|
+
- MySQL documents row aliases as the replacement for `VALUES(col)` in
|
|
37
|
+
`ON DUPLICATE KEY UPDATE`; `VALUES(col)` is deprecated there.
|
|
38
|
+
- MariaDB documents `VALUES(col)` as the supported way to reference inserted
|
|
39
|
+
values in `ON DUPLICATE KEY UPDATE`; use it for cross-engine compatibility.
|
|
40
|
+
- `SKIP LOCKED` is appropriate for queue-like work only. It skips locked rows
|
|
41
|
+
and can return an inconsistent view, so do not use it for general accounting
|
|
42
|
+
or integrity-sensitive reads.
|
|
43
|
+
|
|
44
|
+
## Schema Defaults
|
|
45
|
+
|
|
46
|
+
```sql
|
|
47
|
+
CREATE TABLE orders (
|
|
48
|
+
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
49
|
+
account_id BIGINT UNSIGNED NOT NULL,
|
|
50
|
+
status VARCHAR(32) NOT NULL,
|
|
51
|
+
total DECIMAL(15, 2) NOT NULL,
|
|
52
|
+
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
53
|
+
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
54
|
+
deleted_at DATETIME NULL,
|
|
55
|
+
PRIMARY KEY (id),
|
|
56
|
+
KEY idx_orders_account_status_created (account_id, status, created_at),
|
|
57
|
+
KEY idx_orders_active (account_id, deleted_at)
|
|
58
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Default choices:
|
|
62
|
+
|
|
63
|
+
| Use Case | Prefer | Avoid |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| Surrogate primary keys | `BIGINT UNSIGNED AUTO_INCREMENT` | `INT` for tables that can grow beyond 2B rows |
|
|
66
|
+
| UUID lookup keys | `BINARY(16)` with conversion helpers | `VARCHAR(36)` primary keys on hot tables |
|
|
67
|
+
| Money and exact quantities | `DECIMAL(p, s)` | `FLOAT` or `DOUBLE` |
|
|
68
|
+
| User-facing text | `utf8mb4` tables and indexes | MySQL `utf8` / `utf8mb3` defaults |
|
|
69
|
+
| Application timestamps | `DATETIME` with UTC managed by the app | Assuming `DATETIME` stores time zone metadata |
|
|
70
|
+
| Soft deletes | `deleted_at DATETIME NULL` plus scoped indexes | Filtering soft-deleted rows without an index |
|
|
71
|
+
| Extensible status values | lookup table or constrained `VARCHAR` | `ENUM` when values change often |
|
|
72
|
+
|
|
73
|
+
## Indexing
|
|
74
|
+
|
|
75
|
+
Composite index order usually follows equality predicates first, then range or
|
|
76
|
+
sort columns:
|
|
77
|
+
|
|
78
|
+
```sql
|
|
79
|
+
CREATE INDEX idx_orders_account_status_created
|
|
80
|
+
ON orders (account_id, status, created_at);
|
|
81
|
+
|
|
82
|
+
SELECT id, total
|
|
83
|
+
FROM orders
|
|
84
|
+
WHERE account_id = ?
|
|
85
|
+
AND status = 'pending'
|
|
86
|
+
AND created_at >= ?
|
|
87
|
+
ORDER BY created_at DESC
|
|
88
|
+
LIMIT 50;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Use `EXPLAIN` before adding or changing an index:
|
|
92
|
+
|
|
93
|
+
```sql
|
|
94
|
+
EXPLAIN
|
|
95
|
+
SELECT id, total
|
|
96
|
+
FROM orders
|
|
97
|
+
WHERE account_id = 123 AND status = 'pending'
|
|
98
|
+
ORDER BY created_at DESC
|
|
99
|
+
LIMIT 50;
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Signals to investigate:
|
|
103
|
+
|
|
104
|
+
| Field | Risk Signal |
|
|
105
|
+
| --- | --- |
|
|
106
|
+
| `type` | `ALL` on a large table |
|
|
107
|
+
| `key` | `NULL` when a selective predicate exists |
|
|
108
|
+
| `rows` | Very high row estimate for an interactive path |
|
|
109
|
+
| `Extra` | `Using temporary`, `Using filesort`, or broad `Using where` |
|
|
110
|
+
|
|
111
|
+
Avoid adding indexes blindly. Each index increases write cost, migration time,
|
|
112
|
+
backup size, and buffer-pool pressure.
|
|
113
|
+
|
|
114
|
+
## Query Patterns
|
|
115
|
+
|
|
116
|
+
### Upsert
|
|
117
|
+
|
|
118
|
+
Cross-engine-compatible form:
|
|
119
|
+
|
|
120
|
+
```sql
|
|
121
|
+
INSERT INTO user_settings (user_id, setting_key, setting_value)
|
|
122
|
+
VALUES (?, ?, ?)
|
|
123
|
+
ON DUPLICATE KEY UPDATE
|
|
124
|
+
setting_value = VALUES(setting_value),
|
|
125
|
+
updated_at = CURRENT_TIMESTAMP;
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
MySQL row-alias form:
|
|
129
|
+
|
|
130
|
+
```sql
|
|
131
|
+
INSERT INTO user_settings (user_id, setting_key, setting_value)
|
|
132
|
+
VALUES (?, ?, ?) AS new
|
|
133
|
+
ON DUPLICATE KEY UPDATE
|
|
134
|
+
setting_value = new.setting_value,
|
|
135
|
+
updated_at = CURRENT_TIMESTAMP;
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Use the row-alias form only after confirming the target is MySQL. Use
|
|
139
|
+
`VALUES(col)` for MariaDB or mixed MySQL/MariaDB fleets.
|
|
140
|
+
|
|
141
|
+
### Keyset Pagination
|
|
142
|
+
|
|
143
|
+
```sql
|
|
144
|
+
SELECT id, name, created_at
|
|
145
|
+
FROM products
|
|
146
|
+
WHERE (created_at, id) < (?, ?)
|
|
147
|
+
ORDER BY created_at DESC, id DESC
|
|
148
|
+
LIMIT 50;
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Back it with an index that matches the cursor:
|
|
152
|
+
|
|
153
|
+
```sql
|
|
154
|
+
CREATE INDEX idx_products_created_id ON products (created_at, id);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Do not use deep `OFFSET` pagination on large tables; it makes the server scan
|
|
158
|
+
and discard rows before returning the page.
|
|
159
|
+
|
|
160
|
+
### JSON Fields
|
|
161
|
+
|
|
162
|
+
Use JSON columns for extension data, not for fields that need heavy relational
|
|
163
|
+
filtering or constraints.
|
|
164
|
+
|
|
165
|
+
```sql
|
|
166
|
+
CREATE TABLE events (
|
|
167
|
+
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
|
168
|
+
payload JSON NOT NULL,
|
|
169
|
+
event_type VARCHAR(64)
|
|
170
|
+
GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(payload, '$.type'))) STORED,
|
|
171
|
+
KEY idx_events_type (event_type)
|
|
172
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
For frequently queried JSON paths, expose a generated column and index that
|
|
176
|
+
column. Keep foreign keys, ownership, tenancy, and lifecycle fields relational.
|
|
177
|
+
|
|
178
|
+
### Full-Text Search
|
|
179
|
+
|
|
180
|
+
```sql
|
|
181
|
+
ALTER TABLE articles ADD FULLTEXT KEY ft_articles_title_body (title, body);
|
|
182
|
+
|
|
183
|
+
SELECT id, title, MATCH(title, body) AGAINST (? IN NATURAL LANGUAGE MODE) AS score
|
|
184
|
+
FROM articles
|
|
185
|
+
WHERE MATCH(title, body) AGAINST (? IN NATURAL LANGUAGE MODE)
|
|
186
|
+
ORDER BY score DESC
|
|
187
|
+
LIMIT 20;
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Use external search when you need typo tolerance, complex ranking, cross-table
|
|
191
|
+
facets, or language-specific analysis beyond built-in full-text behavior.
|
|
192
|
+
|
|
193
|
+
## Transactions
|
|
194
|
+
|
|
195
|
+
Keep transactions short and lock rows in a consistent order:
|
|
196
|
+
|
|
197
|
+
```sql
|
|
198
|
+
START TRANSACTION;
|
|
199
|
+
|
|
200
|
+
SELECT id, balance
|
|
201
|
+
FROM accounts
|
|
202
|
+
WHERE id IN (?, ?)
|
|
203
|
+
ORDER BY id
|
|
204
|
+
FOR UPDATE;
|
|
205
|
+
|
|
206
|
+
UPDATE accounts SET balance = balance - ? WHERE id = ?;
|
|
207
|
+
UPDATE accounts SET balance = balance + ? WHERE id = ?;
|
|
208
|
+
|
|
209
|
+
COMMIT;
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Deadlock and lock-wait checklist:
|
|
213
|
+
|
|
214
|
+
- Lock rows in a deterministic order across code paths.
|
|
215
|
+
- Do external API calls before opening the transaction, not inside it.
|
|
216
|
+
- Add indexes for predicates used in `UPDATE`, `DELETE`, and locking reads.
|
|
217
|
+
- On deadlock, roll back and retry the whole transaction with a bounded retry
|
|
218
|
+
budget.
|
|
219
|
+
- Capture `SHOW ENGINE INNODB STATUS\G` soon after a deadlock; it is overwritten
|
|
220
|
+
by later events.
|
|
221
|
+
|
|
222
|
+
Queue-style worker claim:
|
|
223
|
+
|
|
224
|
+
```sql
|
|
225
|
+
START TRANSACTION;
|
|
226
|
+
|
|
227
|
+
SELECT id
|
|
228
|
+
FROM jobs
|
|
229
|
+
WHERE status = 'pending'
|
|
230
|
+
ORDER BY created_at
|
|
231
|
+
LIMIT 1
|
|
232
|
+
FOR UPDATE SKIP LOCKED;
|
|
233
|
+
|
|
234
|
+
UPDATE jobs
|
|
235
|
+
SET status = 'processing', started_at = CURRENT_TIMESTAMP
|
|
236
|
+
WHERE id = ?;
|
|
237
|
+
|
|
238
|
+
COMMIT;
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Use `SKIP LOCKED` only for queue-like workloads where skipping a locked row is
|
|
242
|
+
acceptable. It is not a replacement for normal transactional consistency.
|
|
243
|
+
|
|
244
|
+
## Connection Pools
|
|
245
|
+
|
|
246
|
+
SQLAlchemy example:
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
from sqlalchemy import create_engine
|
|
250
|
+
|
|
251
|
+
engine = create_engine(
|
|
252
|
+
"mysql+mysqlconnector://app:secret@db.internal/app",
|
|
253
|
+
pool_size=10,
|
|
254
|
+
max_overflow=5,
|
|
255
|
+
pool_timeout=30,
|
|
256
|
+
pool_recycle=240,
|
|
257
|
+
pool_pre_ping=True,
|
|
258
|
+
connect_args={"connect_timeout": 5},
|
|
259
|
+
)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Node.js `mysql2` example:
|
|
263
|
+
|
|
264
|
+
```javascript
|
|
265
|
+
import mysql from 'mysql2/promise';
|
|
266
|
+
|
|
267
|
+
const pool = mysql.createPool({
|
|
268
|
+
host: process.env.DB_HOST,
|
|
269
|
+
user: process.env.DB_USER,
|
|
270
|
+
password: process.env.DB_PASSWORD,
|
|
271
|
+
database: process.env.DB_NAME,
|
|
272
|
+
waitForConnections: true,
|
|
273
|
+
connectionLimit: 10,
|
|
274
|
+
queueLimit: 0,
|
|
275
|
+
enableKeepAlive: true,
|
|
276
|
+
keepAliveInitialDelay: 30000,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const [rows] = await pool.execute(
|
|
280
|
+
'SELECT id, total FROM orders WHERE account_id = ? LIMIT 50',
|
|
281
|
+
[accountId],
|
|
282
|
+
);
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Keep application pool recycling below the server `wait_timeout`. If the server
|
|
286
|
+
uses `wait_timeout = 300`, a `pool_recycle` around 240 seconds is coherent;
|
|
287
|
+
`pool_pre_ping` still helps recover from network and failover events.
|
|
288
|
+
|
|
289
|
+
## Diagnostics
|
|
290
|
+
|
|
291
|
+
Useful first-pass commands:
|
|
292
|
+
|
|
293
|
+
```sql
|
|
294
|
+
SHOW FULL PROCESSLIST;
|
|
295
|
+
SHOW ENGINE INNODB STATUS\G;
|
|
296
|
+
SHOW VARIABLES LIKE 'slow_query_log';
|
|
297
|
+
SHOW VARIABLES LIKE 'long_query_time';
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
Enable the slow log in a controlled environment:
|
|
301
|
+
|
|
302
|
+
```sql
|
|
303
|
+
SET GLOBAL slow_query_log = 'ON';
|
|
304
|
+
SET GLOBAL long_query_time = 1;
|
|
305
|
+
SET GLOBAL log_queries_not_using_indexes = 'ON';
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
Use `EXPLAIN ANALYZE` only when it is safe to execute the query. It runs the
|
|
309
|
+
statement and can be expensive on production-sized data.
|
|
310
|
+
|
|
311
|
+
## Replication
|
|
312
|
+
|
|
313
|
+
Read replicas can lag. Do not route read-your-own-write paths, checkout flows,
|
|
314
|
+
permission checks, or idempotency-key reads to a replica immediately after a
|
|
315
|
+
write.
|
|
316
|
+
|
|
317
|
+
```sql
|
|
318
|
+
-- MySQL legacy terminology, still common in existing fleets
|
|
319
|
+
SHOW SLAVE STATUS\G;
|
|
320
|
+
|
|
321
|
+
-- Newer terminology where supported
|
|
322
|
+
SHOW REPLICA STATUS\G;
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
Check the engine/version before standardizing on one command. Monitor replica
|
|
326
|
+
SQL thread health, IO thread health, and lag, not just whether the TCP
|
|
327
|
+
connection is alive.
|
|
328
|
+
|
|
329
|
+
## Security
|
|
330
|
+
|
|
331
|
+
```sql
|
|
332
|
+
CREATE USER 'app'@'%' IDENTIFIED BY 'use-a-secret-manager';
|
|
333
|
+
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'app'@'%';
|
|
334
|
+
|
|
335
|
+
ALTER USER 'app'@'%' REQUIRE SSL;
|
|
336
|
+
|
|
337
|
+
SELECT user, host
|
|
338
|
+
FROM mysql.user
|
|
339
|
+
WHERE user = '';
|
|
340
|
+
|
|
341
|
+
DROP USER IF EXISTS ''@'localhost';
|
|
342
|
+
DROP USER IF EXISTS ''@'%';
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
Security review points:
|
|
346
|
+
|
|
347
|
+
- Do not grant `ALL PRIVILEGES` or `*.*` to application users.
|
|
348
|
+
- Require TLS for application users when traffic crosses hosts or networks.
|
|
349
|
+
- Store credentials in the platform secret manager, not in examples, scripts, or
|
|
350
|
+
repository files.
|
|
351
|
+
- Separate migration/admin users from runtime application users.
|
|
352
|
+
- Audit public network exposure and bind addresses before tuning performance.
|
|
353
|
+
|
|
354
|
+
## Configuration
|
|
355
|
+
|
|
356
|
+
Example starting point for a dedicated database host:
|
|
357
|
+
|
|
358
|
+
```ini
|
|
359
|
+
[mysqld]
|
|
360
|
+
innodb_buffer_pool_size = 4G
|
|
361
|
+
innodb_flush_log_at_trx_commit = 1
|
|
362
|
+
sync_binlog = 1
|
|
363
|
+
|
|
364
|
+
max_connections = 300
|
|
365
|
+
thread_cache_size = 50
|
|
366
|
+
|
|
367
|
+
wait_timeout = 300
|
|
368
|
+
interactive_timeout = 300
|
|
369
|
+
innodb_lock_wait_timeout = 10
|
|
370
|
+
|
|
371
|
+
slow_query_log = ON
|
|
372
|
+
long_query_time = 1
|
|
373
|
+
log_queries_not_using_indexes = ON
|
|
374
|
+
|
|
375
|
+
log_bin = mysql-bin
|
|
376
|
+
binlog_format = ROW
|
|
377
|
+
binlog_expire_logs_seconds = 604800
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
Treat configuration values as a prompt for review, not a universal preset. Size
|
|
381
|
+
memory, connections, log retention, and durability settings from workload,
|
|
382
|
+
hardware, backup policy, and recovery objectives.
|
|
383
|
+
|
|
384
|
+
## Anti-Patterns
|
|
385
|
+
|
|
386
|
+
| Anti-Pattern | Risk | Better Pattern |
|
|
387
|
+
| --- | --- | --- |
|
|
388
|
+
| `SELECT *` in hot paths | Over-fetching and brittle clients | Select explicit columns |
|
|
389
|
+
| Deep `OFFSET` pagination | Linear scans and slow pages | Keyset pagination |
|
|
390
|
+
| No index on foreign-key joins | Slow joins and lock-heavy deletes | Index FK columns intentionally |
|
|
391
|
+
| Long transactions | Lock waits and large undo history | Commit small units of work |
|
|
392
|
+
| Direct DML against `mysql.user` | Grant-table corruption risk | Use `CREATE USER`, `ALTER USER`, `DROP USER` |
|
|
393
|
+
| Application user with admin grants | High blast radius | Least-privilege runtime user |
|
|
394
|
+
| Pool recycle above `wait_timeout` | Stale pooled connections | Recycle below timeout and pre-ping |
|
|
395
|
+
| Replica reads after writes | Stale user-facing state | Pin read-after-write flows to primary |
|
|
396
|
+
|
|
397
|
+
## Output Expectations
|
|
398
|
+
|
|
399
|
+
When this skill is used for review, return:
|
|
400
|
+
|
|
401
|
+
1. Engine/version assumptions.
|
|
402
|
+
2. Highest-risk correctness, lock, security, and migration issues.
|
|
403
|
+
3. Exact SQL or code changes for the safe path.
|
|
404
|
+
4. Validation plan: `EXPLAIN`, migration dry run, lock/deadlock check, and
|
|
405
|
+
rollback criteria.
|
|
406
|
+
5. Any MySQL/MariaDB syntax differences that affect the recommendation.
|
|
407
|
+
|
|
408
|
+
## Related
|
|
409
|
+
|
|
410
|
+
- Skill: `postgres-patterns` - PostgreSQL-specific schema and query patterns
|
|
411
|
+
- Skill: `database-migrations` - migration planning and rollout safety
|
|
412
|
+
- Skill: `backend-patterns` - API and service-layer patterns
|
|
413
|
+
- Skill: `security-review` - secret handling, auth, and least privilege
|
|
414
|
+
- Agent: `database-reviewer` - broader database review workflow
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: postgres-patterns
|
|
3
|
+
description: PostgreSQL indexing, schema, RLS, pagination, and configuration patterns. Use when writing SQL queries or migrations, designing schemas, troubleshooting slow queries, implementing Row Level Security, or setting up connection pooling.
|
|
4
|
+
license: MIT
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
<!-- adapted from affaan-m/ECC (Everything Claude Code) (MIT) — https://github.com/affaan-m/ECC -->
|
|
8
|
+
|
|
9
|
+
# PostgreSQL Patterns
|
|
10
|
+
|
|
11
|
+
Quick reference for PostgreSQL best practices. For detailed guidance, use the `database-reviewer` agent.
|
|
12
|
+
|
|
13
|
+
## When to Activate
|
|
14
|
+
|
|
15
|
+
- Writing SQL queries or migrations
|
|
16
|
+
- Designing database schemas
|
|
17
|
+
- Troubleshooting slow queries
|
|
18
|
+
- Implementing Row Level Security
|
|
19
|
+
- Setting up connection pooling
|
|
20
|
+
|
|
21
|
+
## Quick Reference
|
|
22
|
+
|
|
23
|
+
### Index Cheat Sheet
|
|
24
|
+
|
|
25
|
+
| Query Pattern | Index Type | Example |
|
|
26
|
+
|--------------|------------|---------|
|
|
27
|
+
| `WHERE col = value` | B-tree (default) | `CREATE INDEX idx ON t (col)` |
|
|
28
|
+
| `WHERE col > value` | B-tree | `CREATE INDEX idx ON t (col)` |
|
|
29
|
+
| `WHERE a = x AND b > y` | Composite | `CREATE INDEX idx ON t (a, b)` |
|
|
30
|
+
| `WHERE jsonb @> '{}'` | GIN | `CREATE INDEX idx ON t USING gin (col)` |
|
|
31
|
+
| `WHERE tsv @@ query` | GIN | `CREATE INDEX idx ON t USING gin (col)` |
|
|
32
|
+
| Time-series ranges | BRIN | `CREATE INDEX idx ON t USING brin (col)` |
|
|
33
|
+
|
|
34
|
+
### Data Type Quick Reference
|
|
35
|
+
|
|
36
|
+
| Use Case | Correct Type | Avoid |
|
|
37
|
+
|----------|-------------|-------|
|
|
38
|
+
| IDs | `bigint` | `int`, random UUID |
|
|
39
|
+
| Strings | `text` | `varchar(255)` |
|
|
40
|
+
| Timestamps | `timestamptz` | `timestamp` |
|
|
41
|
+
| Money | `numeric(10,2)` | `float` |
|
|
42
|
+
| Flags | `boolean` | `varchar`, `int` |
|
|
43
|
+
|
|
44
|
+
### Common Patterns
|
|
45
|
+
|
|
46
|
+
**Composite Index Order:**
|
|
47
|
+
```sql
|
|
48
|
+
-- Equality columns first, then range columns
|
|
49
|
+
CREATE INDEX idx ON orders (status, created_at);
|
|
50
|
+
-- Works for: WHERE status = 'pending' AND created_at > '2024-01-01'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**Covering Index:**
|
|
54
|
+
```sql
|
|
55
|
+
CREATE INDEX idx ON users (email) INCLUDE (name, created_at);
|
|
56
|
+
-- Avoids table lookup for SELECT email, name, created_at
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**Partial Index:**
|
|
60
|
+
```sql
|
|
61
|
+
CREATE INDEX idx ON users (email) WHERE deleted_at IS NULL;
|
|
62
|
+
-- Smaller index, only includes active users
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**RLS Policy (Optimized):**
|
|
66
|
+
```sql
|
|
67
|
+
CREATE POLICY policy ON orders
|
|
68
|
+
USING ((SELECT auth.uid()) = user_id); -- Wrap in SELECT!
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**UPSERT:**
|
|
72
|
+
```sql
|
|
73
|
+
INSERT INTO settings (user_id, key, value)
|
|
74
|
+
VALUES (123, 'theme', 'dark')
|
|
75
|
+
ON CONFLICT (user_id, key)
|
|
76
|
+
DO UPDATE SET value = EXCLUDED.value;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**Cursor Pagination:**
|
|
80
|
+
```sql
|
|
81
|
+
SELECT * FROM products WHERE id > $last_id ORDER BY id LIMIT 20;
|
|
82
|
+
-- O(1) vs OFFSET which is O(n)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Queue Processing:**
|
|
86
|
+
```sql
|
|
87
|
+
UPDATE jobs SET status = 'processing'
|
|
88
|
+
WHERE id = (
|
|
89
|
+
SELECT id FROM jobs WHERE status = 'pending'
|
|
90
|
+
ORDER BY created_at LIMIT 1
|
|
91
|
+
FOR UPDATE SKIP LOCKED
|
|
92
|
+
) RETURNING *;
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Anti-Pattern Detection
|
|
96
|
+
|
|
97
|
+
```sql
|
|
98
|
+
-- Find unindexed foreign keys
|
|
99
|
+
SELECT conrelid::regclass, a.attname
|
|
100
|
+
FROM pg_constraint c
|
|
101
|
+
JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY(c.conkey)
|
|
102
|
+
WHERE c.contype = 'f'
|
|
103
|
+
AND NOT EXISTS (
|
|
104
|
+
SELECT 1 FROM pg_index i
|
|
105
|
+
WHERE i.indrelid = c.conrelid AND a.attnum = ANY(i.indkey)
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
-- Find slow queries
|
|
109
|
+
SELECT query, mean_exec_time, calls
|
|
110
|
+
FROM pg_stat_statements
|
|
111
|
+
WHERE mean_exec_time > 100
|
|
112
|
+
ORDER BY mean_exec_time DESC;
|
|
113
|
+
|
|
114
|
+
-- Check table bloat
|
|
115
|
+
SELECT relname, n_dead_tup, last_vacuum
|
|
116
|
+
FROM pg_stat_user_tables
|
|
117
|
+
WHERE n_dead_tup > 1000
|
|
118
|
+
ORDER BY n_dead_tup DESC;
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Configuration Template
|
|
122
|
+
|
|
123
|
+
```sql
|
|
124
|
+
-- Connection limits (adjust for RAM)
|
|
125
|
+
ALTER SYSTEM SET max_connections = 100;
|
|
126
|
+
ALTER SYSTEM SET work_mem = '8MB';
|
|
127
|
+
|
|
128
|
+
-- Timeouts
|
|
129
|
+
ALTER SYSTEM SET idle_in_transaction_session_timeout = '30s';
|
|
130
|
+
ALTER SYSTEM SET statement_timeout = '30s';
|
|
131
|
+
|
|
132
|
+
-- Monitoring
|
|
133
|
+
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
|
|
134
|
+
|
|
135
|
+
-- Security defaults
|
|
136
|
+
REVOKE ALL ON SCHEMA public FROM public;
|
|
137
|
+
|
|
138
|
+
SELECT pg_reload_conf();
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Related
|
|
142
|
+
|
|
143
|
+
- Agent: `database-reviewer` - Full database review workflow
|
|
144
|
+
- Skill: `clickhouse-io` - ClickHouse analytics patterns
|
|
145
|
+
- Skill: `backend-patterns` - API and backend patterns
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
*Based on Supabase Agent Skills (credit: Supabase team) (MIT License)*
|