universal-dev-standards 5.0.0-rc.8 → 5.0.0
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/bin/uds.js +5 -3
- package/bundled/ai/standards/acceptance-criteria-traceability.ai.yaml +178 -0
- package/bundled/ai/standards/agent-dispatch.ai.yaml +69 -0
- package/bundled/ai/standards/ai-command-behavior.ai.yaml +45 -0
- package/bundled/ai/standards/api-design-standards.ai.yaml +192 -0
- package/bundled/ai/standards/branch-completion.ai.yaml +82 -0
- package/bundled/ai/standards/change-batching-standards.ai.yaml +195 -0
- package/bundled/ai/standards/database-standards.ai.yaml +220 -0
- package/bundled/ai/standards/error-codes.ai.yaml +37 -2
- package/bundled/ai/standards/git-worktree.ai.yaml +77 -0
- package/bundled/ai/standards/logging.ai.yaml +21 -2
- package/bundled/ai/standards/model-selection.ai.yaml +81 -0
- package/bundled/ai/standards/pipeline-integration-standards.ai.yaml +184 -0
- package/bundled/ai/standards/systematic-debugging.ai.yaml +73 -0
- package/bundled/ai/standards/verification-evidence.ai.yaml +84 -0
- package/bundled/ai/standards/workflow-enforcement.ai.yaml +253 -0
- package/bundled/core/acceptance-criteria-traceability.md +293 -0
- package/bundled/core/agent-dispatch.md +149 -0
- package/bundled/core/ai-command-behavior.md +239 -0
- package/bundled/core/api-design-standards.md +938 -0
- package/bundled/core/branch-completion.md +158 -0
- package/bundled/core/change-batching-standards.md +257 -0
- package/bundled/core/checkin-standards.md +43 -6
- package/bundled/core/database-standards.md +828 -0
- package/bundled/core/error-code-standards.md +228 -2
- package/bundled/core/forward-derivation-standards.md +54 -2
- package/bundled/core/git-worktree.md +131 -0
- package/bundled/core/model-selection.md +153 -0
- package/bundled/core/pipeline-integration-standards.md +230 -0
- package/bundled/core/systematic-debugging.md +156 -0
- package/bundled/core/testing-standards.md +49 -39
- package/bundled/core/verification-evidence.md +172 -0
- package/bundled/core/workflow-enforcement.md +132 -0
- package/bundled/locales/zh-CN/CLAUDE.md +2 -2
- package/bundled/locales/zh-CN/README.md +4 -4
- package/bundled/locales/zh-CN/SECURITY.md +92 -0
- package/bundled/locales/zh-CN/core/acceptance-criteria-traceability.md +301 -0
- package/bundled/locales/zh-CN/core/acceptance-test-driven-development.md +47 -0
- package/bundled/locales/zh-CN/core/accessibility-standards.md +124 -0
- package/bundled/locales/zh-CN/core/agent-dispatch.md +113 -0
- package/bundled/locales/zh-CN/core/ai-agreement-standards.md +103 -0
- package/bundled/locales/zh-CN/core/ai-command-behavior.md +247 -0
- package/bundled/locales/zh-CN/core/api-design-standards.md +946 -0
- package/bundled/locales/zh-CN/core/behavior-driven-development.md +48 -0
- package/bundled/locales/zh-CN/core/branch-completion.md +112 -0
- package/bundled/locales/zh-CN/core/change-batching-standards.md +265 -0
- package/bundled/locales/zh-CN/core/checkin-standards.md +43 -5
- package/bundled/locales/zh-CN/core/context-aware-loading.md +106 -0
- package/bundled/locales/zh-CN/core/database-standards.md +836 -0
- package/bundled/locales/zh-CN/core/deployment-standards.md +80 -0
- package/bundled/locales/zh-CN/core/error-code-standards.md +2 -2
- package/bundled/locales/zh-CN/core/forward-derivation-standards.md +493 -0
- package/bundled/locales/zh-CN/core/git-worktree.md +101 -0
- package/bundled/locales/zh-CN/core/model-selection.md +112 -0
- package/bundled/locales/zh-CN/core/performance-standards.md +104 -0
- package/bundled/locales/zh-CN/core/pipeline-integration-standards.md +238 -0
- package/bundled/locales/zh-CN/core/project-context-memory.md +124 -0
- package/bundled/locales/zh-CN/core/requirement-engineering.md +49 -0
- package/bundled/locales/zh-CN/core/security-standards.md +100 -0
- package/bundled/locales/zh-CN/core/systematic-debugging.md +106 -0
- package/bundled/locales/zh-CN/core/test-governance.md +116 -0
- package/bundled/locales/zh-CN/core/testing-standards.md +392 -156
- package/bundled/locales/zh-CN/core/verification-evidence.md +118 -0
- package/bundled/locales/zh-CN/core/virtual-organization-standards.md +104 -0
- package/bundled/locales/zh-CN/core/workflow-enforcement.md +132 -0
- package/bundled/locales/zh-CN/docs/CHEATSHEET.md +27 -1
- package/bundled/locales/zh-CN/docs/CLI-INIT-OPTIONS.md +9 -1
- package/bundled/locales/zh-CN/docs/FEATURE-REFERENCE.md +37 -9
- package/bundled/locales/zh-CN/docs/USER-MANUAL.md +652 -0
- package/bundled/locales/zh-CN/integrations/github-copilot/copilot-instructions.md +1 -1
- package/bundled/locales/zh-CN/integrations/openspec/AGENTS.md +29 -4
- package/bundled/locales/zh-CN/integrations/spec-kit/AGENTS.md +143 -72
- package/bundled/locales/zh-CN/skills/ac-coverage-assistant/SKILL.md +126 -0
- package/bundled/locales/zh-CN/skills/api-design-assistant/SKILL.md +100 -0
- package/bundled/locales/zh-CN/skills/brainstorm-assistant/SKILL.md +119 -0
- package/bundled/locales/zh-CN/skills/ci-cd-assistant/SKILL.md +82 -0
- package/bundled/locales/zh-CN/skills/commands/ac-coverage.md +97 -0
- package/bundled/locales/zh-CN/skills/commands/api-design.md +94 -0
- package/bundled/locales/zh-CN/skills/commands/atdd.md +163 -0
- package/bundled/locales/zh-CN/skills/commands/audit.md +77 -0
- package/bundled/locales/zh-CN/skills/commands/bdd.md +1 -1
- package/bundled/locales/zh-CN/skills/commands/brainstorm.md +87 -0
- package/bundled/locales/zh-CN/skills/commands/changelog.md +63 -0
- package/bundled/locales/zh-CN/skills/commands/check.md +219 -0
- package/bundled/locales/zh-CN/skills/commands/checkin.md +61 -0
- package/bundled/locales/zh-CN/skills/commands/ci-cd.md +77 -0
- package/bundled/locales/zh-CN/skills/commands/commit.md +81 -0
- package/bundled/locales/zh-CN/skills/commands/config.md +207 -0
- package/bundled/locales/zh-CN/skills/commands/coverage.md +74 -0
- package/bundled/locales/zh-CN/skills/commands/database.md +78 -0
- package/bundled/locales/zh-CN/skills/commands/derive-all.md +58 -0
- package/bundled/locales/zh-CN/skills/commands/derive-atdd.md +54 -0
- package/bundled/locales/zh-CN/skills/commands/derive-bdd.md +53 -0
- package/bundled/locales/zh-CN/skills/commands/derive-tdd.md +54 -0
- package/bundled/locales/zh-CN/skills/commands/derive.md +78 -0
- package/bundled/locales/zh-CN/skills/commands/dev-workflow.md +119 -0
- package/bundled/locales/zh-CN/skills/commands/discover.md +86 -0
- package/bundled/locales/zh-CN/skills/commands/docgen.md +67 -0
- package/bundled/locales/zh-CN/skills/commands/docs.md +65 -0
- package/bundled/locales/zh-CN/skills/commands/durable.md +87 -0
- package/bundled/locales/zh-CN/skills/commands/guide.md +52 -0
- package/bundled/locales/zh-CN/skills/commands/incident.md +92 -0
- package/bundled/locales/zh-CN/skills/commands/init.md +272 -0
- package/bundled/locales/zh-CN/skills/commands/methodology.md +1 -1
- package/bundled/locales/zh-CN/skills/commands/metrics.md +73 -0
- package/bundled/locales/zh-CN/skills/commands/migrate.md +92 -0
- package/bundled/locales/zh-CN/skills/commands/pr.md +80 -0
- package/bundled/locales/zh-CN/skills/commands/refactor.md +1 -1
- package/bundled/locales/zh-CN/skills/commands/release.md +62 -0
- package/bundled/locales/zh-CN/skills/commands/requirement.md +54 -0
- package/bundled/locales/zh-CN/skills/commands/reverse-bdd.md +47 -0
- package/bundled/locales/zh-CN/skills/commands/reverse-sdd.md +51 -0
- package/bundled/locales/zh-CN/skills/commands/reverse-tdd.md +51 -0
- package/bundled/locales/zh-CN/skills/commands/reverse.md +63 -0
- package/bundled/locales/zh-CN/skills/commands/review.md +50 -0
- package/bundled/locales/zh-CN/skills/commands/scan.md +76 -0
- package/bundled/locales/zh-CN/skills/commands/sdd-retro.md +40 -0
- package/bundled/locales/zh-CN/skills/commands/sdd.md +379 -0
- package/bundled/locales/zh-CN/skills/commands/security.md +75 -0
- package/bundled/locales/zh-CN/skills/commands/tdd.md +111 -0
- package/bundled/locales/zh-CN/skills/commands/update.md +337 -0
- package/bundled/locales/zh-CN/skills/commit-standards/SKILL.md +1 -2
- package/bundled/locales/zh-CN/skills/database-assistant/SKILL.md +97 -0
- package/bundled/locales/zh-CN/skills/dev-workflow-guide/SKILL.md +145 -0
- package/bundled/locales/zh-CN/skills/durable-execution-assistant/SKILL.md +84 -0
- package/bundled/locales/zh-CN/skills/incident-response-assistant/SKILL.md +107 -0
- package/bundled/locales/zh-CN/skills/metrics-dashboard-assistant/SKILL.md +67 -0
- package/bundled/locales/zh-CN/skills/migration-assistant/SKILL.md +77 -0
- package/bundled/locales/zh-CN/skills/pr-automation-assistant/SKILL.md +90 -0
- package/bundled/locales/zh-CN/skills/security-assistant/SKILL.md +79 -0
- package/bundled/locales/zh-CN/skills/security-scan-assistant/SKILL.md +72 -0
- package/bundled/locales/zh-CN/skills/spec-driven-dev/SKILL.md +73 -10
- package/bundled/locales/zh-CN/skills/spec-driven-dev/guide.md +267 -122
- package/bundled/locales/zh-TW/CLAUDE.md +2 -2
- package/bundled/locales/zh-TW/README.md +4 -4
- package/bundled/locales/zh-TW/SECURITY.md +92 -0
- package/bundled/locales/zh-TW/core/acceptance-criteria-traceability.md +301 -0
- package/bundled/locales/zh-TW/core/acceptance-test-driven-development.md +47 -0
- package/bundled/locales/zh-TW/core/accessibility-standards.md +84 -0
- package/bundled/locales/zh-TW/core/agent-dispatch.md +89 -0
- package/bundled/locales/zh-TW/core/ai-agreement-standards.md +87 -0
- package/bundled/locales/zh-TW/core/ai-command-behavior.md +247 -0
- package/bundled/locales/zh-TW/core/api-design-standards.md +946 -0
- package/bundled/locales/zh-TW/core/behavior-driven-development.md +63 -0
- package/bundled/locales/zh-TW/core/branch-completion.md +76 -0
- package/bundled/locales/zh-TW/core/change-batching-standards.md +265 -0
- package/bundled/locales/zh-TW/core/checkin-standards.md +39 -5
- package/bundled/locales/zh-TW/core/context-aware-loading.md +86 -0
- package/bundled/locales/zh-TW/core/database-standards.md +836 -0
- package/bundled/locales/zh-TW/core/deployment-standards.md +66 -0
- package/bundled/locales/zh-TW/core/error-code-standards.md +230 -4
- package/bundled/locales/zh-TW/core/forward-derivation-standards.md +62 -2
- package/bundled/locales/zh-TW/core/git-worktree.md +104 -0
- package/bundled/locales/zh-TW/core/model-selection.md +83 -0
- package/bundled/locales/zh-TW/core/performance-standards.md +84 -0
- package/bundled/locales/zh-TW/core/pipeline-integration-standards.md +238 -0
- package/bundled/locales/zh-TW/core/project-context-memory.md +79 -0
- package/bundled/locales/zh-TW/core/requirement-engineering.md +79 -0
- package/bundled/locales/zh-TW/core/security-standards.md +74 -0
- package/bundled/locales/zh-TW/core/systematic-debugging.md +95 -0
- package/bundled/locales/zh-TW/core/test-governance.md +88 -0
- package/bundled/locales/zh-TW/core/testing-standards.md +309 -86
- package/bundled/locales/zh-TW/core/verification-evidence.md +94 -0
- package/bundled/locales/zh-TW/core/virtual-organization-standards.md +88 -0
- package/bundled/locales/zh-TW/core/workflow-enforcement.md +132 -0
- package/bundled/locales/zh-TW/docs/CHEATSHEET.md +27 -1
- package/bundled/locales/zh-TW/docs/CLI-INIT-OPTIONS.md +9 -1
- package/bundled/locales/zh-TW/docs/FEATURE-REFERENCE.md +37 -9
- package/bundled/locales/zh-TW/docs/USER-MANUAL.md +652 -0
- package/bundled/locales/zh-TW/integrations/github-copilot/copilot-instructions.md +1 -1
- package/bundled/locales/zh-TW/integrations/openspec/AGENTS.md +29 -4
- package/bundled/locales/zh-TW/integrations/spec-kit/AGENTS.md +142 -71
- package/bundled/locales/zh-TW/skills/ac-coverage-assistant/SKILL.md +127 -0
- package/bundled/locales/zh-TW/skills/ai-friendly-architecture/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/ai-instruction-standards/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/api-design-assistant/SKILL.md +97 -0
- package/bundled/locales/zh-TW/skills/atdd-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/audit-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/bdd-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/brainstorm-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/changelog-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/checkin-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/ci-cd-assistant/SKILL.md +79 -0
- package/bundled/locales/zh-TW/skills/code-review-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/commands/ac-coverage.md +95 -0
- package/bundled/locales/zh-TW/skills/commands/api-design.md +92 -0
- package/bundled/locales/zh-TW/skills/commands/atdd.md +167 -0
- package/bundled/locales/zh-TW/skills/commands/audit.md +75 -0
- package/bundled/locales/zh-TW/skills/commands/bdd.md +1 -1
- package/bundled/locales/zh-TW/skills/commands/brainstorm.md +91 -0
- package/bundled/locales/zh-TW/skills/commands/changelog.md +67 -0
- package/bundled/locales/zh-TW/skills/commands/check.md +223 -0
- package/bundled/locales/zh-TW/skills/commands/checkin.md +65 -0
- package/bundled/locales/zh-TW/skills/commands/ci-cd.md +75 -0
- package/bundled/locales/zh-TW/skills/commands/commit.md +85 -0
- package/bundled/locales/zh-TW/skills/commands/config.md +211 -0
- package/bundled/locales/zh-TW/skills/commands/coverage.md +78 -0
- package/bundled/locales/zh-TW/skills/commands/database.md +76 -0
- package/bundled/locales/zh-TW/skills/commands/derive-all.md +62 -0
- package/bundled/locales/zh-TW/skills/commands/derive-atdd.md +58 -0
- package/bundled/locales/zh-TW/skills/commands/derive-bdd.md +54 -0
- package/bundled/locales/zh-TW/skills/commands/derive-tdd.md +55 -0
- package/bundled/locales/zh-TW/skills/commands/derive.md +79 -0
- package/bundled/locales/zh-TW/skills/commands/dev-workflow.md +120 -0
- package/bundled/locales/zh-TW/skills/commands/discover.md +87 -0
- package/bundled/locales/zh-TW/skills/commands/docgen.md +68 -0
- package/bundled/locales/zh-TW/skills/commands/docs.md +66 -0
- package/bundled/locales/zh-TW/skills/commands/durable.md +85 -0
- package/bundled/locales/zh-TW/skills/commands/guide.md +51 -0
- package/bundled/locales/zh-TW/skills/commands/incident.md +90 -0
- package/bundled/locales/zh-TW/skills/commands/init.md +273 -0
- package/bundled/locales/zh-TW/skills/commands/methodology.md +1 -1
- package/bundled/locales/zh-TW/skills/commands/metrics.md +71 -0
- package/bundled/locales/zh-TW/skills/commands/migrate.md +90 -0
- package/bundled/locales/zh-TW/skills/commands/pr.md +78 -0
- package/bundled/locales/zh-TW/skills/commands/refactor.md +1 -1
- package/bundled/locales/zh-TW/skills/commands/release.md +63 -0
- package/bundled/locales/zh-TW/skills/commands/requirement.md +61 -0
- package/bundled/locales/zh-TW/skills/commands/reverse-bdd.md +54 -0
- package/bundled/locales/zh-TW/skills/commands/reverse-sdd.md +58 -0
- package/bundled/locales/zh-TW/skills/commands/reverse-tdd.md +58 -0
- package/bundled/locales/zh-TW/skills/commands/reverse.md +70 -0
- package/bundled/locales/zh-TW/skills/commands/review.md +57 -0
- package/bundled/locales/zh-TW/skills/commands/scan.md +74 -0
- package/bundled/locales/zh-TW/skills/commands/sdd-retro.md +47 -0
- package/bundled/locales/zh-TW/skills/commands/sdd.md +362 -0
- package/bundled/locales/zh-TW/skills/commands/security.md +73 -0
- package/bundled/locales/zh-TW/skills/commands/tdd.md +115 -0
- package/bundled/locales/zh-TW/skills/commands/update.md +326 -0
- package/bundled/locales/zh-TW/skills/commit-standards/SKILL.md +2 -6
- package/bundled/locales/zh-TW/skills/database-assistant/SKILL.md +94 -0
- package/bundled/locales/zh-TW/skills/dev-workflow-guide/SKILL.md +140 -0
- package/bundled/locales/zh-TW/skills/docs-generator/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/documentation-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/durable-execution-assistant/SKILL.md +81 -0
- package/bundled/locales/zh-TW/skills/error-code-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/forward-derivation/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/git-workflow-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/incident-response-assistant/SKILL.md +104 -0
- package/bundled/locales/zh-TW/skills/logging-guide/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/methodology-system/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/metrics-dashboard-assistant/SKILL.md +64 -0
- package/bundled/locales/zh-TW/skills/migration-assistant/SKILL.md +74 -0
- package/bundled/locales/zh-TW/skills/pr-automation-assistant/SKILL.md +87 -0
- package/bundled/locales/zh-TW/skills/project-discovery/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/refactoring-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/release-standards/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/requirement-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/reverse-engineer/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/security-assistant/SKILL.md +76 -0
- package/bundled/locales/zh-TW/skills/security-scan-assistant/SKILL.md +69 -0
- package/bundled/locales/zh-TW/skills/spec-driven-dev/SKILL.md +74 -14
- package/bundled/locales/zh-TW/skills/spec-driven-dev/guide.md +243 -98
- package/bundled/locales/zh-TW/skills/tdd-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/test-coverage-assistant/SKILL.md +1 -4
- package/bundled/locales/zh-TW/skills/testing-guide/SKILL.md +1 -5
- package/bundled/skills/README.md +11 -0
- package/bundled/skills/ac-coverage-assistant/SKILL.md +127 -0
- package/bundled/skills/ai-collaboration-standards/SKILL.md +10 -0
- package/bundled/skills/ai-friendly-architecture/SKILL.md +10 -0
- package/bundled/skills/ai-instruction-standards/SKILL.md +10 -0
- package/bundled/skills/api-design-assistant/SKILL.md +119 -0
- package/bundled/skills/atdd-assistant/SKILL.md +7 -0
- package/bundled/skills/bdd-assistant/SKILL.md +7 -0
- package/bundled/skills/brainstorm-assistant/SKILL.md +7 -0
- package/bundled/skills/checkin-assistant/SKILL.md +7 -0
- package/bundled/skills/ci-cd-assistant/SKILL.md +97 -0
- package/bundled/skills/code-review-assistant/SKILL.md +7 -0
- package/bundled/skills/commands/README.md +70 -0
- package/bundled/skills/commands/ac-coverage.md +135 -0
- package/bundled/skills/commands/api-design.md +86 -0
- package/bundled/skills/commands/atdd.md +69 -0
- package/bundled/skills/commands/audit.md +69 -0
- package/bundled/skills/commands/bdd.md +101 -0
- package/bundled/skills/commands/brainstorm.md +45 -0
- package/bundled/skills/commands/changelog.md +34 -0
- package/bundled/skills/commands/check.md +52 -1
- package/bundled/skills/commands/checkin.md +46 -0
- package/bundled/skills/commands/ci-cd.md +69 -0
- package/bundled/skills/commands/commit.md +79 -0
- package/bundled/skills/commands/config.md +48 -0
- package/bundled/skills/commands/coverage.md +53 -0
- package/bundled/skills/commands/database.md +70 -0
- package/bundled/skills/commands/derive-all.md +40 -0
- package/bundled/skills/commands/derive-atdd.md +33 -0
- package/bundled/skills/commands/derive-bdd.md +39 -0
- package/bundled/skills/commands/derive-tdd.md +40 -0
- package/bundled/skills/commands/derive.md +47 -0
- package/bundled/skills/commands/dev-workflow.md +104 -7
- package/bundled/skills/commands/discover.md +39 -0
- package/bundled/skills/commands/docgen.md +35 -0
- package/bundled/skills/commands/docs.md +40 -0
- package/bundled/skills/commands/durable.md +79 -0
- package/bundled/skills/commands/incident.md +84 -0
- package/bundled/skills/commands/init.md +55 -0
- package/bundled/skills/commands/methodology.md +72 -0
- package/bundled/skills/commands/metrics.md +65 -0
- package/bundled/skills/commands/migrate.md +84 -0
- package/bundled/skills/commands/pr.md +72 -0
- package/bundled/skills/commands/refactor.md +51 -0
- package/bundled/skills/commands/release.md +60 -0
- package/bundled/skills/commands/requirement.md +38 -0
- package/bundled/skills/commands/reverse-bdd.md +34 -0
- package/bundled/skills/commands/reverse-sdd.md +42 -0
- package/bundled/skills/commands/reverse-tdd.md +40 -0
- package/bundled/skills/commands/reverse.md +41 -0
- package/bundled/skills/commands/review.md +39 -0
- package/bundled/skills/commands/scan.md +68 -0
- package/bundled/skills/commands/sdd-retro.md +48 -0
- package/bundled/skills/commands/sdd.md +220 -0
- package/bundled/skills/commands/security.md +67 -0
- package/bundled/skills/commands/tdd.md +101 -0
- package/bundled/skills/commands/update.md +61 -0
- package/bundled/skills/commit-standards/SKILL.md +8 -2
- package/bundled/skills/database-assistant/SKILL.md +118 -0
- package/bundled/skills/dev-workflow-guide/SKILL.md +53 -7
- package/bundled/skills/dev-workflow-guide/workflow-phases.md +24 -0
- package/bundled/skills/durable-execution-assistant/SKILL.md +116 -0
- package/bundled/skills/forward-derivation/SKILL.md +7 -0
- package/bundled/skills/incident-response-assistant/SKILL.md +132 -0
- package/bundled/skills/methodology-system/SKILL.md +24 -2
- package/bundled/skills/metrics-dashboard-assistant/SKILL.md +109 -0
- package/bundled/skills/migration-assistant/SKILL.md +119 -0
- package/bundled/skills/pr-automation-assistant/SKILL.md +114 -0
- package/bundled/skills/project-discovery/SKILL.md +7 -0
- package/bundled/skills/refactoring-assistant/SKILL.md +7 -0
- package/bundled/skills/release-standards/SKILL.md +7 -0
- package/bundled/skills/requirement-assistant/SKILL.md +7 -0
- package/bundled/skills/reverse-engineer/SKILL.md +7 -0
- package/bundled/skills/security-assistant/SKILL.md +105 -0
- package/bundled/skills/security-scan-assistant/SKILL.md +96 -0
- package/bundled/skills/spec-driven-dev/SKILL.md +84 -4
- package/bundled/skills/spec-driven-dev/guide.md +156 -22
- package/bundled/skills/tdd-assistant/SKILL.md +7 -0
- package/bundled/skills/test-coverage-assistant/SKILL.md +11 -0
- package/bundled/skills/testing-guide/SKILL.md +23 -0
- package/package.json +1 -1
- package/src/commands/check.js +154 -8
- package/src/commands/config.js +231 -104
- package/src/commands/init.js +23 -3
- package/src/commands/update.js +250 -25
- package/src/config/ai-agent-paths.js +16 -0
- package/src/config/workflow-definitions.js +184 -0
- package/src/core/constants.js +6 -0
- package/src/flows/init-flow.js +23 -6
- package/src/i18n/messages.js +51 -3
- package/src/installers/integration-installer.js +67 -2
- package/src/installers/manifest-installer.js +1 -0
- package/src/installers/standards-installer.js +3 -2
- package/src/prompts/init.js +50 -3
- package/src/reconciler/manifest-migrator.js +2 -2
- package/src/reconciler/plan-executor.js +11 -0
- package/src/utils/integration-generator.js +327 -14
- package/src/utils/workflow-gate.js +292 -0
- package/standards-registry.json +160 -5
|
@@ -0,0 +1,828 @@
|
|
|
1
|
+
# Database Standards
|
|
2
|
+
|
|
3
|
+
> **Language**: English | [繁體中文](../locales/zh-TW/core/database-standards.md)
|
|
4
|
+
|
|
5
|
+
**Version**: 1.0.0
|
|
6
|
+
**Last Updated**: 2026-03-18
|
|
7
|
+
**Applicability**: All software projects
|
|
8
|
+
**Scope**: universal
|
|
9
|
+
**Industry Standards**: ISO/IEC 9075 (SQL), ACID Properties, BASE Theorem
|
|
10
|
+
**References**: [use-the-index-luke.com](https://use-the-index-luke.com/), [sqlstyle.guide](https://www.sqlstyle.guide/)
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Purpose
|
|
15
|
+
|
|
16
|
+
This standard defines guidelines for database design, querying, migration, and operations across relational and non-relational databases. It covers schema design principles, indexing strategies, migration workflows, query optimization, transaction management, and data integrity — ensuring databases are performant, maintainable, and secure.
|
|
17
|
+
|
|
18
|
+
**Reference Standards**:
|
|
19
|
+
- [ISO/IEC 9075 — SQL Standard](https://www.iso.org/standard/76583.html)
|
|
20
|
+
- [Use The Index, Luke — SQL Indexing and Tuning](https://use-the-index-luke.com/)
|
|
21
|
+
- [Martin Fowler — Evolutionary Database Design](https://martinfowler.com/articles/evodb.html)
|
|
22
|
+
- [Designing Data-Intensive Applications (Martin Kleppmann)](https://dataintensive.net/)
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Core Principles
|
|
27
|
+
|
|
28
|
+
| Principle | Description |
|
|
29
|
+
|-----------|-------------|
|
|
30
|
+
| **Data Integrity First** | Constraints, validations, and referential integrity are enforced at the database level, not only in application code |
|
|
31
|
+
| **Schema as Code** | Database schemas are version-controlled and reproducible through migration scripts |
|
|
32
|
+
| **Least Privilege** | Database accounts use the minimum permissions required for their function |
|
|
33
|
+
| **Measure Before Tuning** | Use EXPLAIN plans and metrics before optimizing; avoid premature optimization |
|
|
34
|
+
| **Defense in Depth** | Protect sensitive data with encryption, masking, and access controls at multiple layers |
|
|
35
|
+
| **Backward Compatibility** | Schema changes must maintain backward compatibility during deployment windows |
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Schema Design Principles
|
|
40
|
+
|
|
41
|
+
### Normalization
|
|
42
|
+
|
|
43
|
+
Apply normalization to eliminate redundancy and ensure data integrity. Target Third Normal Form (3NF) as the baseline for transactional systems.
|
|
44
|
+
|
|
45
|
+
| Normal Form | Rule | Example Violation |
|
|
46
|
+
|-------------|------|-------------------|
|
|
47
|
+
| **1NF** | Atomic values only; no repeating groups | `tags = "java,python,go"` in a single column |
|
|
48
|
+
| **2NF** | 1NF + no partial dependencies on composite keys | Non-key column depends on only part of a composite PK |
|
|
49
|
+
| **3NF** | 2NF + no transitive dependencies | `order.customer_name` derived from `customer.name` via `customer_id` |
|
|
50
|
+
|
|
51
|
+
### Denormalization Decision Matrix
|
|
52
|
+
|
|
53
|
+
Denormalization trades integrity for read performance. Use deliberately and document the tradeoff.
|
|
54
|
+
|
|
55
|
+
| Criteria | Normalize | Denormalize |
|
|
56
|
+
|----------|-----------|-------------|
|
|
57
|
+
| Read/Write ratio | Write-heavy or balanced | Read-heavy (>90% reads) |
|
|
58
|
+
| Data consistency | Critical (financial, medical) | Eventual consistency acceptable |
|
|
59
|
+
| Query complexity | Acceptable join performance | Joins cause unacceptable latency |
|
|
60
|
+
| Data volatility | Frequently updated | Rarely changes after creation |
|
|
61
|
+
| Storage cost | Minimize duplication | Storage is cheap; speed is priority |
|
|
62
|
+
|
|
63
|
+
**When denormalizing:**
|
|
64
|
+
- Document the source of truth and sync mechanism
|
|
65
|
+
- Add comments explaining why denormalization was chosen
|
|
66
|
+
- Implement consistency checks or reconciliation jobs
|
|
67
|
+
|
|
68
|
+
### Naming Conventions
|
|
69
|
+
|
|
70
|
+
| Element | Convention | Example |
|
|
71
|
+
|---------|-----------|---------|
|
|
72
|
+
| Tables | `snake_case`, singular | `user_account`, `order_item` |
|
|
73
|
+
| Columns | `snake_case` | `first_name`, `created_at` |
|
|
74
|
+
| Primary keys | `id` | `user_account.id` |
|
|
75
|
+
| Foreign keys | `<referenced_table>_id` | `order.user_account_id` |
|
|
76
|
+
| Boolean columns | `is_` or `has_` prefix | `is_active`, `has_verified_email` |
|
|
77
|
+
| Timestamps | `_at` suffix | `created_at`, `updated_at`, `deleted_at` |
|
|
78
|
+
| Indexes | `idx_<table>_<columns>` | `idx_user_account_email` |
|
|
79
|
+
| Unique constraints | `uq_<table>_<columns>` | `uq_user_account_email` |
|
|
80
|
+
| Check constraints | `ck_<table>_<description>` | `ck_order_positive_amount` |
|
|
81
|
+
|
|
82
|
+
### Reserved Words
|
|
83
|
+
|
|
84
|
+
- Avoid using SQL reserved words as identifiers (`order`, `user`, `group`, `select`)
|
|
85
|
+
- If unavoidable, suffix with the entity type: `user_account`, `order_record`, `user_group`
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## Data Types
|
|
90
|
+
|
|
91
|
+
### Choose Appropriate Types
|
|
92
|
+
|
|
93
|
+
| Scenario | Recommended | Avoid |
|
|
94
|
+
|----------|-------------|-------|
|
|
95
|
+
| Monetary values | `DECIMAL(19,4)` or `NUMERIC` | `FLOAT`, `DOUBLE` (precision loss) |
|
|
96
|
+
| Dates and times | `TIMESTAMPTZ` (with timezone) | `VARCHAR` for dates |
|
|
97
|
+
| Boolean flags | `BOOLEAN` | `INT` (0/1), `CHAR(1)` ('Y'/'N') |
|
|
98
|
+
| Short text (< 255) | `VARCHAR(n)` with appropriate length | `TEXT` for known-length fields |
|
|
99
|
+
| Long text | `TEXT` | `VARCHAR(MAX)` or oversized `VARCHAR` |
|
|
100
|
+
| IP addresses | Native IP type or `INET` | `VARCHAR(45)` |
|
|
101
|
+
| JSON data | `JSONB` (PostgreSQL) or native JSON | `TEXT` with JSON strings |
|
|
102
|
+
| Enumerations | `ENUM` type or lookup table | Stringly-typed values |
|
|
103
|
+
|
|
104
|
+
### UUID vs Auto-Increment
|
|
105
|
+
|
|
106
|
+
| Factor | Auto-Increment | UUID |
|
|
107
|
+
|--------|---------------|------|
|
|
108
|
+
| Storage size | 4-8 bytes | 16 bytes |
|
|
109
|
+
| Index performance | Better (sequential) | Worse (random inserts fragment B-tree) |
|
|
110
|
+
| Distributed generation | Requires coordination | No coordination needed |
|
|
111
|
+
| Security | Predictable (enumerable) | Non-guessable |
|
|
112
|
+
| URL exposure | Reveals record count | Safe for public URLs |
|
|
113
|
+
| Merge/replication | Conflict-prone | Conflict-free |
|
|
114
|
+
|
|
115
|
+
**Recommendation:**
|
|
116
|
+
- Use **auto-increment** for internal-only IDs in single-database systems
|
|
117
|
+
- Use **UUIDv7** (time-ordered) for distributed systems or publicly exposed IDs
|
|
118
|
+
- Consider **UUIDv7** as default for new projects — it combines sortability with uniqueness
|
|
119
|
+
|
|
120
|
+
```sql
|
|
121
|
+
-- PostgreSQL: UUIDv7 as primary key
|
|
122
|
+
CREATE TABLE user_account (
|
|
123
|
+
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
124
|
+
email VARCHAR(255) NOT NULL,
|
|
125
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
-- Auto-increment alternative
|
|
129
|
+
CREATE TABLE user_account (
|
|
130
|
+
id BIGSERIAL PRIMARY KEY,
|
|
131
|
+
public_id UUID NOT NULL DEFAULT gen_random_uuid(),
|
|
132
|
+
email VARCHAR(255) NOT NULL,
|
|
133
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
134
|
+
);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
## Indexing Strategy
|
|
140
|
+
|
|
141
|
+
### When to Create Indexes
|
|
142
|
+
|
|
143
|
+
| Create Index When | Avoid Index When |
|
|
144
|
+
|-------------------|------------------|
|
|
145
|
+
| Column appears in `WHERE` clauses frequently | Table has fewer than 1,000 rows |
|
|
146
|
+
| Column is used in `JOIN` conditions | Column has very low cardinality (e.g., boolean) |
|
|
147
|
+
| Column is used in `ORDER BY` | Table is write-heavy with rare reads |
|
|
148
|
+
| Column is used in `GROUP BY` | Column is frequently updated |
|
|
149
|
+
| Unique constraint is needed | You already have too many indexes (>8 per table) |
|
|
150
|
+
|
|
151
|
+
### Composite Index Column Order
|
|
152
|
+
|
|
153
|
+
The order of columns in a composite index matters significantly. Follow the **equality-first, range-last** rule:
|
|
154
|
+
|
|
155
|
+
```sql
|
|
156
|
+
-- Query pattern:
|
|
157
|
+
-- WHERE status = 'active' AND created_at > '2026-01-01' ORDER BY created_at
|
|
158
|
+
|
|
159
|
+
-- GOOD: equality columns first, range/sort columns last
|
|
160
|
+
CREATE INDEX idx_order_status_created ON order_record (status, created_at);
|
|
161
|
+
|
|
162
|
+
-- BAD: range column first eliminates effectiveness for equality filter
|
|
163
|
+
CREATE INDEX idx_order_created_status ON order_record (created_at, status);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**Column order rules:**
|
|
167
|
+
1. Equality conditions (`=`) first
|
|
168
|
+
2. `IN` conditions next
|
|
169
|
+
3. Range conditions (`>`, `<`, `BETWEEN`) last
|
|
170
|
+
4. `ORDER BY` columns at the end (if matching sort direction)
|
|
171
|
+
|
|
172
|
+
### Covering Indexes
|
|
173
|
+
|
|
174
|
+
A covering index includes all columns needed by a query, enabling index-only scans:
|
|
175
|
+
|
|
176
|
+
```sql
|
|
177
|
+
-- Query: SELECT email, first_name FROM user_account WHERE status = 'active';
|
|
178
|
+
|
|
179
|
+
-- Covering index — no table lookup needed
|
|
180
|
+
CREATE INDEX idx_user_account_status_covering
|
|
181
|
+
ON user_account (status) INCLUDE (email, first_name);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Partial Indexes
|
|
185
|
+
|
|
186
|
+
Create indexes on a subset of rows to reduce index size and improve write performance:
|
|
187
|
+
|
|
188
|
+
```sql
|
|
189
|
+
-- Only index active records (90% of queries filter on active)
|
|
190
|
+
CREATE INDEX idx_order_active
|
|
191
|
+
ON order_record (created_at)
|
|
192
|
+
WHERE status = 'active';
|
|
193
|
+
|
|
194
|
+
-- Only index non-null values
|
|
195
|
+
CREATE INDEX idx_user_account_phone
|
|
196
|
+
ON user_account (phone_number)
|
|
197
|
+
WHERE phone_number IS NOT NULL;
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Indexing Anti-Patterns
|
|
201
|
+
|
|
202
|
+
| Anti-Pattern | Problem | Solution |
|
|
203
|
+
|-------------|---------|----------|
|
|
204
|
+
| **Over-indexing** | Slows writes, wastes storage | Audit indexes quarterly; drop unused ones |
|
|
205
|
+
| **Unused indexes** | Maintenance cost with no read benefit | Monitor `pg_stat_user_indexes` or equivalent |
|
|
206
|
+
| **Duplicate indexes** | Redundant overhead | Index `(a, b)` already covers queries on `(a)` |
|
|
207
|
+
| **Indexing low-cardinality columns** | Full table scan is often faster | Use partial indexes or bitmap indexes instead |
|
|
208
|
+
| **Missing index on FK** | Slow cascading deletes and joins | Always index foreign key columns |
|
|
209
|
+
| **Function on indexed column** | Index bypassed | Create functional/expression index |
|
|
210
|
+
|
|
211
|
+
```sql
|
|
212
|
+
-- Anti-pattern: function prevents index usage
|
|
213
|
+
SELECT * FROM user_account WHERE LOWER(email) = 'test@example.com';
|
|
214
|
+
|
|
215
|
+
-- Solution: expression index
|
|
216
|
+
CREATE INDEX idx_user_account_email_lower ON user_account (LOWER(email));
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## Migration Strategy
|
|
222
|
+
|
|
223
|
+
### Principles
|
|
224
|
+
|
|
225
|
+
| Principle | Description |
|
|
226
|
+
|-----------|-------------|
|
|
227
|
+
| **Version-Controlled** | All migrations stored in source control alongside application code |
|
|
228
|
+
| **Sequential** | Migrations execute in deterministic order |
|
|
229
|
+
| **Idempotent** | Running a migration twice produces the same result |
|
|
230
|
+
| **Tested** | Migrations are tested against production-like data volumes before deployment |
|
|
231
|
+
| **Documented** | Each migration includes a description of what and why |
|
|
232
|
+
|
|
233
|
+
### Naming Convention
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
YYYYMMDDHHMMSS_description.sql
|
|
237
|
+
|
|
238
|
+
Examples:
|
|
239
|
+
20260318120000_create_user_account_table.sql
|
|
240
|
+
20260318120100_add_email_index_to_user_account.sql
|
|
241
|
+
20260318120200_add_phone_column_to_user_account.sql
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Forward-Only vs Reversible
|
|
245
|
+
|
|
246
|
+
| Approach | Pros | Cons | Use When |
|
|
247
|
+
|----------|------|------|----------|
|
|
248
|
+
| **Reversible** (up/down) | Easy rollback, safer | More code to maintain, some changes not reversible | Development, non-destructive changes |
|
|
249
|
+
| **Forward-only** | Simpler, matches reality | Requires separate rollback migration | Production, destructive changes |
|
|
250
|
+
|
|
251
|
+
**Recommendation:** Use reversible migrations by default. For destructive operations (drop column, drop table), use forward-only with a separate rollback migration file.
|
|
252
|
+
|
|
253
|
+
### Zero-Downtime Migration Patterns (Expand-Contract)
|
|
254
|
+
|
|
255
|
+
For schema changes in systems that cannot tolerate downtime:
|
|
256
|
+
|
|
257
|
+
**Phase 1 — Expand (backward-compatible)**
|
|
258
|
+
```sql
|
|
259
|
+
-- Add new column (nullable, no constraint yet)
|
|
260
|
+
ALTER TABLE user_account ADD COLUMN phone VARCHAR(20);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
**Phase 2 — Migrate (dual-write)**
|
|
264
|
+
```sql
|
|
265
|
+
-- Backfill existing data
|
|
266
|
+
UPDATE user_account SET phone = legacy_phone WHERE phone IS NULL;
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**Phase 3 — Contract (after all consumers updated)**
|
|
270
|
+
```sql
|
|
271
|
+
-- Add constraint now that all data is populated
|
|
272
|
+
ALTER TABLE user_account ALTER COLUMN phone SET NOT NULL;
|
|
273
|
+
-- Drop old column (only after verifying no consumers use it)
|
|
274
|
+
ALTER TABLE user_account DROP COLUMN legacy_phone;
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### Rollback Strategy
|
|
278
|
+
|
|
279
|
+
| Scenario | Rollback Approach |
|
|
280
|
+
|----------|-------------------|
|
|
281
|
+
| Added column | Drop the column |
|
|
282
|
+
| Added index | Drop the index |
|
|
283
|
+
| Added table | Drop the table |
|
|
284
|
+
| Dropped column | Cannot undo — must restore from backup or re-add |
|
|
285
|
+
| Data transformation | Run reverse transformation (if designed) |
|
|
286
|
+
| Renamed column | Rename back |
|
|
287
|
+
|
|
288
|
+
**Critical rule:** Never drop columns or tables in the same deployment that stops writing to them. Use the expand-contract pattern.
|
|
289
|
+
|
|
290
|
+
---
|
|
291
|
+
|
|
292
|
+
## Query Best Practices
|
|
293
|
+
|
|
294
|
+
### N+1 Query Prevention
|
|
295
|
+
|
|
296
|
+
The N+1 problem occurs when code executes one query to fetch a list, then N additional queries for each item.
|
|
297
|
+
|
|
298
|
+
```sql
|
|
299
|
+
-- N+1 PROBLEM (application issues N queries)
|
|
300
|
+
-- Query 1: SELECT * FROM order_record WHERE user_id = 42;
|
|
301
|
+
-- Query 2..N: SELECT * FROM order_item WHERE order_id = ?; (for each order)
|
|
302
|
+
|
|
303
|
+
-- SOLUTION: JOIN or subquery
|
|
304
|
+
SELECT o.*, oi.*
|
|
305
|
+
FROM order_record o
|
|
306
|
+
JOIN order_item oi ON oi.order_id = o.id
|
|
307
|
+
WHERE o.user_id = 42;
|
|
308
|
+
|
|
309
|
+
-- Or batch loading
|
|
310
|
+
SELECT * FROM order_item
|
|
311
|
+
WHERE order_id IN (SELECT id FROM order_record WHERE user_id = 42);
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### EXPLAIN Plan Usage
|
|
315
|
+
|
|
316
|
+
Always analyze query plans for:
|
|
317
|
+
- Queries executing more than 100 times per minute
|
|
318
|
+
- Queries with execution time > 100ms
|
|
319
|
+
- Queries touching more than 10,000 rows
|
|
320
|
+
- Any new query before deployment
|
|
321
|
+
|
|
322
|
+
```sql
|
|
323
|
+
-- PostgreSQL
|
|
324
|
+
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
|
|
325
|
+
SELECT * FROM order_record WHERE status = 'pending' AND created_at > '2026-01-01';
|
|
326
|
+
|
|
327
|
+
-- Key indicators to watch:
|
|
328
|
+
-- Seq Scan on large tables → missing index
|
|
329
|
+
-- Nested Loop with high rows → consider Hash Join
|
|
330
|
+
-- Sort with high cost → add index matching ORDER BY
|
|
331
|
+
-- Rows (estimated vs actual) → stale statistics, run ANALYZE
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### Pagination
|
|
335
|
+
|
|
336
|
+
| Method | Pros | Cons | Use When |
|
|
337
|
+
|--------|------|------|----------|
|
|
338
|
+
| **Offset-based** | Simple, supports random page access | Slow on large offsets, inconsistent with concurrent writes | Small datasets, admin panels |
|
|
339
|
+
| **Keyset (cursor)** | Consistent performance, stable results | No random page jumps, complex with multi-column sort | APIs, infinite scroll, large datasets |
|
|
340
|
+
|
|
341
|
+
```sql
|
|
342
|
+
-- Offset-based (avoid for large tables)
|
|
343
|
+
SELECT * FROM order_record ORDER BY id LIMIT 20 OFFSET 10000;
|
|
344
|
+
-- Performance degrades: DB must scan and discard 10,000 rows
|
|
345
|
+
|
|
346
|
+
-- Keyset-based (preferred)
|
|
347
|
+
SELECT * FROM order_record
|
|
348
|
+
WHERE id > :last_seen_id
|
|
349
|
+
ORDER BY id
|
|
350
|
+
LIMIT 20;
|
|
351
|
+
-- Consistent performance regardless of page depth
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### Query Anti-Patterns
|
|
355
|
+
|
|
356
|
+
| Anti-Pattern | Problem | Solution |
|
|
357
|
+
|-------------|---------|----------|
|
|
358
|
+
| `SELECT *` | Fetches unnecessary data, breaks on schema changes | Explicitly list needed columns |
|
|
359
|
+
| String concatenation in queries | SQL injection vulnerability | Use parameterized queries / prepared statements |
|
|
360
|
+
| `OR` on different columns | Prevents index usage | Use `UNION ALL` or restructure |
|
|
361
|
+
| `NOT IN` with NULLs | Unexpected empty results | Use `NOT EXISTS` instead |
|
|
362
|
+
| Implicit type casting | Index bypass, wrong results | Cast explicitly |
|
|
363
|
+
| `LIKE '%prefix'` | Leading wildcard prevents index usage | Use full-text search or reverse index |
|
|
364
|
+
|
|
365
|
+
```sql
|
|
366
|
+
-- ANTI-PATTERN: SQL injection risk
|
|
367
|
+
query = "SELECT * FROM user_account WHERE email = '" + email + "'";
|
|
368
|
+
|
|
369
|
+
-- CORRECT: Parameterized query
|
|
370
|
+
query = "SELECT * FROM user_account WHERE email = $1";
|
|
371
|
+
params = [email];
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
## Transaction Management
|
|
377
|
+
|
|
378
|
+
### ACID Properties
|
|
379
|
+
|
|
380
|
+
| Property | Description | Enforcement |
|
|
381
|
+
|----------|-------------|-------------|
|
|
382
|
+
| **Atomicity** | All operations succeed or all fail | Use transactions; avoid partial commits |
|
|
383
|
+
| **Consistency** | Database moves from one valid state to another | Enforce constraints at DB level |
|
|
384
|
+
| **Isolation** | Concurrent transactions don't interfere | Choose appropriate isolation level |
|
|
385
|
+
| **Durability** | Committed data survives system failure | Use WAL (Write-Ahead Logging); confirm sync writes |
|
|
386
|
+
|
|
387
|
+
### Isolation Levels
|
|
388
|
+
|
|
389
|
+
| Level | Dirty Read | Non-Repeatable Read | Phantom Read | Performance | Use Case |
|
|
390
|
+
|-------|-----------|---------------------|-------------|-------------|----------|
|
|
391
|
+
| **Read Uncommitted** | Possible | Possible | Possible | Fastest | Never recommended |
|
|
392
|
+
| **Read Committed** | Prevented | Possible | Possible | Fast | Default for most RDBMS; general-purpose queries |
|
|
393
|
+
| **Repeatable Read** | Prevented | Prevented | Possible | Medium | Financial reports, inventory checks |
|
|
394
|
+
| **Serializable** | Prevented | Prevented | Prevented | Slowest | Money transfers, booking systems |
|
|
395
|
+
|
|
396
|
+
**Recommendation:** Use **Read Committed** as the default. Escalate to **Repeatable Read** or **Serializable** only for operations requiring strict consistency (e.g., account balance updates, seat reservations).
|
|
397
|
+
|
|
398
|
+
### Deadlock Prevention
|
|
399
|
+
|
|
400
|
+
| Strategy | Implementation |
|
|
401
|
+
|----------|---------------|
|
|
402
|
+
| **Consistent lock ordering** | Always acquire locks on tables/rows in the same order across all transactions |
|
|
403
|
+
| **Short transactions** | Keep transactions as brief as possible; move non-DB work outside |
|
|
404
|
+
| **Lock timeouts** | Set `lock_timeout` to fail fast rather than wait indefinitely |
|
|
405
|
+
| **Retry logic** | Implement exponential backoff retry for deadlock errors (SQLSTATE 40P01) |
|
|
406
|
+
| **Avoid user interaction** | Never hold a transaction open waiting for user input |
|
|
407
|
+
|
|
408
|
+
```sql
|
|
409
|
+
-- Set lock timeout to prevent indefinite waiting
|
|
410
|
+
SET lock_timeout = '5s';
|
|
411
|
+
|
|
412
|
+
-- Keep transactions short
|
|
413
|
+
BEGIN;
|
|
414
|
+
UPDATE account SET balance = balance - 100 WHERE id = 1;
|
|
415
|
+
UPDATE account SET balance = balance + 100 WHERE id = 2;
|
|
416
|
+
COMMIT;
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### Optimistic vs Pessimistic Locking
|
|
420
|
+
|
|
421
|
+
| Factor | Optimistic | Pessimistic |
|
|
422
|
+
|--------|-----------|-------------|
|
|
423
|
+
| Mechanism | Version column / timestamp check at write time | `SELECT ... FOR UPDATE` acquires row lock |
|
|
424
|
+
| Conflict rate | Low conflict environments | High conflict environments |
|
|
425
|
+
| Performance | Better when conflicts are rare | Better when conflicts are frequent |
|
|
426
|
+
| UX impact | User may see "someone else modified" error | User may wait for lock release |
|
|
427
|
+
| Deadlock risk | None | Possible |
|
|
428
|
+
|
|
429
|
+
```sql
|
|
430
|
+
-- Optimistic locking
|
|
431
|
+
UPDATE order_record
|
|
432
|
+
SET status = 'shipped', version = version + 1
|
|
433
|
+
WHERE id = 42 AND version = 3;
|
|
434
|
+
-- If affected rows = 0, another transaction modified it → retry or error
|
|
435
|
+
|
|
436
|
+
-- Pessimistic locking
|
|
437
|
+
BEGIN;
|
|
438
|
+
SELECT * FROM order_record WHERE id = 42 FOR UPDATE;
|
|
439
|
+
-- Row is locked; other transactions wait
|
|
440
|
+
UPDATE order_record SET status = 'shipped' WHERE id = 42;
|
|
441
|
+
COMMIT;
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
---
|
|
445
|
+
|
|
446
|
+
## SQL vs NoSQL Decision Matrix
|
|
447
|
+
|
|
448
|
+
| Criteria | Relational (SQL) | Document (NoSQL) | Key-Value | Graph |
|
|
449
|
+
|----------|------------------|-------------------|-----------|-------|
|
|
450
|
+
| **Data structure** | Structured, well-defined schema | Semi-structured, flexible schema | Simple key→value pairs | Highly connected entities |
|
|
451
|
+
| **Consistency** | Strong (ACID) | Eventual (BASE), some offer ACID | Eventual | Varies |
|
|
452
|
+
| **Query complexity** | Complex joins, aggregations | Simple lookups, nested documents | Single-key lookups | Relationship traversals |
|
|
453
|
+
| **Scale pattern** | Vertical (scale-up) | Horizontal (scale-out) | Horizontal | Varies |
|
|
454
|
+
| **Schema changes** | Migration required | Schema-less / flexible | Schema-less | Schema-optional |
|
|
455
|
+
| **Example use cases** | Financial systems, ERP, CRM | Content management, user profiles, catalogs | Caching, sessions, rate limiting | Social networks, recommendations, fraud detection |
|
|
456
|
+
| **Example databases** | PostgreSQL, MySQL, SQL Server | MongoDB, CouchDB, DynamoDB | Redis, Memcached, DynamoDB | Neo4j, Amazon Neptune |
|
|
457
|
+
|
|
458
|
+
### Decision Guide
|
|
459
|
+
|
|
460
|
+
```
|
|
461
|
+
Is your data highly relational with complex queries?
|
|
462
|
+
├── Yes → Relational (SQL)
|
|
463
|
+
└── No → Is your data simple key-value pairs?
|
|
464
|
+
├── Yes → Key-Value store
|
|
465
|
+
└── No → Are relationships the primary query pattern?
|
|
466
|
+
├── Yes → Graph database
|
|
467
|
+
└── No → Document database
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
**Polyglot Persistence:** Many systems benefit from using multiple database types. Example:
|
|
471
|
+
- **PostgreSQL** for transactional data (orders, accounts)
|
|
472
|
+
- **Redis** for caching and sessions
|
|
473
|
+
- **Elasticsearch** for full-text search
|
|
474
|
+
- **Neo4j** for recommendation engine
|
|
475
|
+
|
|
476
|
+
---
|
|
477
|
+
|
|
478
|
+
## Connection Management
|
|
479
|
+
|
|
480
|
+
### Connection Pooling
|
|
481
|
+
|
|
482
|
+
Every application MUST use connection pooling. Creating a new database connection per request is prohibitively expensive (TCP handshake, authentication, SSL negotiation).
|
|
483
|
+
|
|
484
|
+
| Parameter | Recommended Default | Description |
|
|
485
|
+
|-----------|--------------------|-------------|
|
|
486
|
+
| **Min pool size** | 2-5 | Minimum idle connections maintained |
|
|
487
|
+
| **Max pool size** | 10-20 | Maximum concurrent connections |
|
|
488
|
+
| **Connection timeout** | 5 seconds | Time to wait for a connection from pool |
|
|
489
|
+
| **Idle timeout** | 10 minutes | Close idle connections after this period |
|
|
490
|
+
| **Max lifetime** | 30 minutes | Recycle connections to prevent stale state |
|
|
491
|
+
| **Validation query** | `SELECT 1` | Health check before returning connection |
|
|
492
|
+
|
|
493
|
+
### Pool Sizing Formula
|
|
494
|
+
|
|
495
|
+
A common formula for maximum pool size:
|
|
496
|
+
|
|
497
|
+
```
|
|
498
|
+
pool_size = (core_count * 2) + effective_spindle_count
|
|
499
|
+
|
|
500
|
+
Examples:
|
|
501
|
+
- 4-core server, SSD: (4 * 2) + 1 = 9-10 connections
|
|
502
|
+
- 8-core server, SSD: (8 * 2) + 1 = 17 connections
|
|
503
|
+
- 4-core server, 4 HDDs: (4 * 2) + 4 = 12 connections
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
**Important:** More connections is NOT better. Excessive connections cause:
|
|
507
|
+
- Thread contention in the database
|
|
508
|
+
- Memory pressure (each connection uses ~5-10 MB)
|
|
509
|
+
- Increased context switching
|
|
510
|
+
|
|
511
|
+
### Health Checks
|
|
512
|
+
|
|
513
|
+
```sql
|
|
514
|
+
-- Basic health check
|
|
515
|
+
SELECT 1;
|
|
516
|
+
|
|
517
|
+
-- Advanced health check (verify read/write capability)
|
|
518
|
+
SELECT NOW();
|
|
519
|
+
|
|
520
|
+
-- Connection validation before use
|
|
521
|
+
SET statement_timeout = '2s';
|
|
522
|
+
SELECT 1;
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
---
|
|
526
|
+
|
|
527
|
+
## Data Integrity
|
|
528
|
+
|
|
529
|
+
### Constraints
|
|
530
|
+
|
|
531
|
+
Always enforce data integrity at the database level. Application-level validation is a complement, not a replacement.
|
|
532
|
+
|
|
533
|
+
| Constraint | Purpose | Example |
|
|
534
|
+
|-----------|---------|---------|
|
|
535
|
+
| `NOT NULL` | Prevent missing required data | `email VARCHAR(255) NOT NULL` |
|
|
536
|
+
| `UNIQUE` | Prevent duplicate values | `UNIQUE (email)` |
|
|
537
|
+
| `CHECK` | Validate value ranges/formats | `CHECK (amount > 0)` |
|
|
538
|
+
| `FOREIGN KEY` | Enforce referential integrity | `REFERENCES user_account(id)` |
|
|
539
|
+
| `DEFAULT` | Provide sensible default values | `DEFAULT NOW()` |
|
|
540
|
+
| `EXCLUSION` | Prevent overlapping ranges | `EXCLUDE USING gist (room WITH =, period WITH &&)` |
|
|
541
|
+
|
|
542
|
+
```sql
|
|
543
|
+
CREATE TABLE order_record (
|
|
544
|
+
id BIGSERIAL PRIMARY KEY,
|
|
545
|
+
user_account_id BIGINT NOT NULL REFERENCES user_account(id),
|
|
546
|
+
amount DECIMAL(19,4) NOT NULL CHECK (amount > 0),
|
|
547
|
+
status VARCHAR(20) NOT NULL DEFAULT 'pending'
|
|
548
|
+
CHECK (status IN ('pending', 'confirmed', 'shipped', 'delivered', 'cancelled')),
|
|
549
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
550
|
+
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
551
|
+
);
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
### Cascading Rules
|
|
555
|
+
|
|
556
|
+
| Rule | Behavior | Use When |
|
|
557
|
+
|------|----------|----------|
|
|
558
|
+
| `CASCADE` | Delete/update child rows automatically | Strong ownership (order → order_items) |
|
|
559
|
+
| `SET NULL` | Set FK to NULL on parent delete | Optional relationship (post → author on author deletion) |
|
|
560
|
+
| `SET DEFAULT` | Set FK to default on parent delete | Reassign to default value |
|
|
561
|
+
| `RESTRICT` | Prevent parent deletion if children exist | Protect critical references (user → audit_log) |
|
|
562
|
+
| `NO ACTION` | Same as RESTRICT (deferred check possible) | Default behavior |
|
|
563
|
+
|
|
564
|
+
**Recommendation:** Use `RESTRICT` by default. Only use `CASCADE` when parent-child lifecycle is tightly coupled.
|
|
565
|
+
|
|
566
|
+
### Soft Delete vs Hard Delete
|
|
567
|
+
|
|
568
|
+
| Factor | Soft Delete | Hard Delete |
|
|
569
|
+
|--------|------------|-------------|
|
|
570
|
+
| Implementation | `deleted_at TIMESTAMPTZ` column | `DELETE FROM table` |
|
|
571
|
+
| Data recovery | Easy — set `deleted_at = NULL` | Requires backup restoration |
|
|
572
|
+
| Query complexity | Must add `WHERE deleted_at IS NULL` everywhere | Simpler queries |
|
|
573
|
+
| Storage | Grows over time | Reclaims space |
|
|
574
|
+
| Compliance | Audit trail preserved | May violate retention requirements |
|
|
575
|
+
| Performance | Large tables with many soft-deleted rows | Cleaner table statistics |
|
|
576
|
+
|
|
577
|
+
```sql
|
|
578
|
+
-- Soft delete implementation
|
|
579
|
+
ALTER TABLE user_account ADD COLUMN deleted_at TIMESTAMPTZ;
|
|
580
|
+
|
|
581
|
+
-- Create partial index for active records
|
|
582
|
+
CREATE INDEX idx_user_account_active ON user_account (email) WHERE deleted_at IS NULL;
|
|
583
|
+
|
|
584
|
+
-- Application query pattern
|
|
585
|
+
SELECT * FROM user_account WHERE deleted_at IS NULL AND email = $1;
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
**Recommendation:** Use soft delete for user-facing data and anything requiring audit trails. Use hard delete for ephemeral data (sessions, temporary tokens, logs beyond retention).
|
|
589
|
+
|
|
590
|
+
---
|
|
591
|
+
|
|
592
|
+
## Backup and Recovery
|
|
593
|
+
|
|
594
|
+
### Backup Strategy Types
|
|
595
|
+
|
|
596
|
+
| Strategy | Description | Speed (Backup) | Speed (Restore) | Storage Cost |
|
|
597
|
+
|----------|-------------|-----------------|------------------|-------------|
|
|
598
|
+
| **Full** | Complete copy of entire database | Slowest | Fastest | Highest |
|
|
599
|
+
| **Incremental** | Only changes since last backup (any type) | Fastest | Slowest (requires chain) | Lowest |
|
|
600
|
+
| **Differential** | Changes since last full backup | Medium | Medium (full + diff) | Medium |
|
|
601
|
+
|
|
602
|
+
### RPO and RTO
|
|
603
|
+
|
|
604
|
+
| Metric | Definition | Example Targets |
|
|
605
|
+
|--------|-----------|-----------------|
|
|
606
|
+
| **RPO** (Recovery Point Objective) | Maximum acceptable data loss (time) | 1 hour: hourly backups; 0: continuous replication |
|
|
607
|
+
| **RTO** (Recovery Time Objective) | Maximum acceptable downtime | 15 min: automated failover; 4 hours: manual restore |
|
|
608
|
+
|
|
609
|
+
### Backup Schedule Recommendation
|
|
610
|
+
|
|
611
|
+
| Tier | RPO | RTO | Strategy |
|
|
612
|
+
|------|-----|-----|----------|
|
|
613
|
+
| **Critical** (financial, healthcare) | < 1 min | < 15 min | Synchronous replication + continuous WAL archiving |
|
|
614
|
+
| **Important** (e-commerce, SaaS) | < 1 hour | < 1 hour | Streaming replication + hourly WAL archiving |
|
|
615
|
+
| **Standard** (internal tools) | < 24 hours | < 4 hours | Daily full + hourly incremental |
|
|
616
|
+
| **Low** (development, staging) | < 1 week | < 1 day | Weekly full backup |
|
|
617
|
+
|
|
618
|
+
### Backup Testing
|
|
619
|
+
|
|
620
|
+
| Requirement | Frequency |
|
|
621
|
+
|-------------|-----------|
|
|
622
|
+
| Restore test to separate environment | Monthly |
|
|
623
|
+
| Verify data integrity after restore | Every restore test |
|
|
624
|
+
| Measure actual RTO vs target | Quarterly |
|
|
625
|
+
| Test point-in-time recovery | Semi-annually |
|
|
626
|
+
| Document and update runbook | After each test |
|
|
627
|
+
|
|
628
|
+
---
|
|
629
|
+
|
|
630
|
+
## Sensitive Data Handling
|
|
631
|
+
|
|
632
|
+
### Column-Level Encryption
|
|
633
|
+
|
|
634
|
+
```sql
|
|
635
|
+
-- Encrypt at write time
|
|
636
|
+
INSERT INTO user_account (email, ssn_encrypted)
|
|
637
|
+
VALUES ($1, pgp_sym_encrypt($2, $encryption_key));
|
|
638
|
+
|
|
639
|
+
-- Decrypt at read time (only by authorized roles)
|
|
640
|
+
SELECT email, pgp_sym_decrypt(ssn_encrypted, $encryption_key) AS ssn
|
|
641
|
+
FROM user_account WHERE id = $1;
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
### Data Classification
|
|
645
|
+
|
|
646
|
+
| Level | Description | Examples | Handling |
|
|
647
|
+
|-------|-------------|----------|----------|
|
|
648
|
+
| **Public** | No sensitivity | Marketing content, public APIs | No special handling |
|
|
649
|
+
| **Internal** | Business-sensitive | Revenue data, roadmaps | Access control, no public exposure |
|
|
650
|
+
| **Confidential** | Personally identifiable | Email, phone, address | Encryption at rest, access logging |
|
|
651
|
+
| **Restricted** | Highly sensitive | SSN, credit cards, passwords | Column encryption, data masking, strict audit |
|
|
652
|
+
|
|
653
|
+
### Data Masking
|
|
654
|
+
|
|
655
|
+
```sql
|
|
656
|
+
-- View-based masking for support staff
|
|
657
|
+
CREATE VIEW user_account_masked AS
|
|
658
|
+
SELECT
|
|
659
|
+
id,
|
|
660
|
+
LEFT(email, 2) || '***@' || SPLIT_PART(email, '@', 2) AS email,
|
|
661
|
+
'***-**-' || RIGHT(ssn, 4) AS ssn_masked,
|
|
662
|
+
first_name,
|
|
663
|
+
created_at
|
|
664
|
+
FROM user_account;
|
|
665
|
+
|
|
666
|
+
-- Grant support team access to masked view only
|
|
667
|
+
GRANT SELECT ON user_account_masked TO support_role;
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
### PII Management Checklist
|
|
671
|
+
|
|
672
|
+
- [ ] Identify and catalog all PII columns across all tables
|
|
673
|
+
- [ ] Encrypt PII at rest (column-level or tablespace encryption)
|
|
674
|
+
- [ ] Encrypt PII in transit (TLS for all connections)
|
|
675
|
+
- [ ] Implement data masking for non-production environments
|
|
676
|
+
- [ ] Define and enforce retention policies
|
|
677
|
+
- [ ] Support data subject requests (GDPR right to erasure, access)
|
|
678
|
+
- [ ] Log all access to PII columns
|
|
679
|
+
- [ ] Anonymize data in development and staging environments
|
|
680
|
+
|
|
681
|
+
### Audit Logging
|
|
682
|
+
|
|
683
|
+
```sql
|
|
684
|
+
-- Audit log table
|
|
685
|
+
CREATE TABLE audit_log (
|
|
686
|
+
id BIGSERIAL PRIMARY KEY,
|
|
687
|
+
table_name VARCHAR(100) NOT NULL,
|
|
688
|
+
record_id BIGINT NOT NULL,
|
|
689
|
+
action VARCHAR(10) NOT NULL CHECK (action IN ('INSERT', 'UPDATE', 'DELETE')),
|
|
690
|
+
old_values JSONB,
|
|
691
|
+
new_values JSONB,
|
|
692
|
+
changed_by VARCHAR(100) NOT NULL,
|
|
693
|
+
changed_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
694
|
+
);
|
|
695
|
+
|
|
696
|
+
-- Trigger-based audit (PostgreSQL example)
|
|
697
|
+
CREATE OR REPLACE FUNCTION audit_trigger_fn()
|
|
698
|
+
RETURNS TRIGGER AS $$
|
|
699
|
+
BEGIN
|
|
700
|
+
INSERT INTO audit_log (table_name, record_id, action, old_values, new_values, changed_by)
|
|
701
|
+
VALUES (
|
|
702
|
+
TG_TABLE_NAME,
|
|
703
|
+
COALESCE(NEW.id, OLD.id),
|
|
704
|
+
TG_OP,
|
|
705
|
+
CASE WHEN TG_OP != 'INSERT' THEN to_jsonb(OLD) END,
|
|
706
|
+
CASE WHEN TG_OP != 'DELETE' THEN to_jsonb(NEW) END,
|
|
707
|
+
current_user
|
|
708
|
+
);
|
|
709
|
+
RETURN COALESCE(NEW, OLD);
|
|
710
|
+
END;
|
|
711
|
+
$$ LANGUAGE plpgsql;
|
|
712
|
+
```
|
|
713
|
+
|
|
714
|
+
---
|
|
715
|
+
|
|
716
|
+
## Performance Monitoring
|
|
717
|
+
|
|
718
|
+
### Slow Query Logging
|
|
719
|
+
|
|
720
|
+
| Database | Configuration | Recommended Threshold |
|
|
721
|
+
|----------|--------------|----------------------|
|
|
722
|
+
| PostgreSQL | `log_min_duration_statement` | 200ms (development), 1000ms (production) |
|
|
723
|
+
| MySQL | `slow_query_log`, `long_query_time` | 1 second |
|
|
724
|
+
| SQL Server | Extended Events or Query Store | 1 second |
|
|
725
|
+
|
|
726
|
+
### Key Metrics to Monitor
|
|
727
|
+
|
|
728
|
+
| Metric | Warning Threshold | Critical Threshold | Tool |
|
|
729
|
+
|--------|-------------------|-------------------|------|
|
|
730
|
+
| **Active connections** | > 70% of max | > 90% of max | Database dashboard |
|
|
731
|
+
| **Cache/buffer hit ratio** | < 95% | < 90% | `pg_stat_bgwriter`, InnoDB buffer pool |
|
|
732
|
+
| **Replication lag** | > 1 second | > 10 seconds | Replication monitoring |
|
|
733
|
+
| **Transaction rate** | Deviation > 20% from baseline | > 50% deviation | Application metrics |
|
|
734
|
+
| **Lock wait time** | > 1 second average | > 5 seconds | Lock monitoring queries |
|
|
735
|
+
| **Deadlocks per minute** | > 1 | > 5 | Database logs |
|
|
736
|
+
| **Table bloat** | > 20% dead tuples | > 40% dead tuples | `pg_stat_user_tables` |
|
|
737
|
+
| **Query execution time (p95)** | > 500ms | > 2 seconds | APM tools |
|
|
738
|
+
|
|
739
|
+
### Query Plan Analysis Workflow
|
|
740
|
+
|
|
741
|
+
```
|
|
742
|
+
1. Identify slow query (slow query log or APM)
|
|
743
|
+
↓
|
|
744
|
+
2. Run EXPLAIN ANALYZE on staging with production-like data
|
|
745
|
+
↓
|
|
746
|
+
3. Look for:
|
|
747
|
+
- Sequential scans on large tables → add index
|
|
748
|
+
- High actual vs estimated rows → run ANALYZE (update statistics)
|
|
749
|
+
- Nested loops with many iterations → restructure query or add index
|
|
750
|
+
- Sort operations with high cost → add index matching sort order
|
|
751
|
+
↓
|
|
752
|
+
4. Apply fix (add index, rewrite query, update statistics)
|
|
753
|
+
↓
|
|
754
|
+
5. Re-run EXPLAIN ANALYZE to verify improvement
|
|
755
|
+
↓
|
|
756
|
+
6. Deploy and monitor execution time in production
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
---
|
|
760
|
+
|
|
761
|
+
## Quick Reference Card
|
|
762
|
+
|
|
763
|
+
### Schema Design
|
|
764
|
+
|
|
765
|
+
```
|
|
766
|
+
✅ Use snake_case for all identifiers
|
|
767
|
+
✅ Singular table names (user_account, not user_accounts)
|
|
768
|
+
✅ Use _id suffix for foreign keys
|
|
769
|
+
✅ Always have id, created_at, updated_at columns
|
|
770
|
+
✅ Enforce constraints at database level
|
|
771
|
+
✅ Normalize to 3NF, denormalize with documented reasoning
|
|
772
|
+
```
|
|
773
|
+
|
|
774
|
+
### Queries
|
|
775
|
+
|
|
776
|
+
```
|
|
777
|
+
✅ Always use parameterized queries
|
|
778
|
+
✅ List specific columns instead of SELECT *
|
|
779
|
+
✅ Use EXPLAIN ANALYZE for query optimization
|
|
780
|
+
✅ Prefer keyset pagination over offset for large datasets
|
|
781
|
+
✅ Batch operations to prevent N+1 queries
|
|
782
|
+
✅ Set statement_timeout to prevent runaway queries
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
### Operations
|
|
786
|
+
|
|
787
|
+
```
|
|
788
|
+
✅ Use connection pooling (never open per-request connections)
|
|
789
|
+
✅ Version-control all migrations
|
|
790
|
+
✅ Test migrations with production-like data
|
|
791
|
+
✅ Use expand-contract for zero-downtime schema changes
|
|
792
|
+
✅ Monitor slow queries, connections, cache hit ratio
|
|
793
|
+
✅ Test backup restoration regularly
|
|
794
|
+
```
|
|
795
|
+
|
|
796
|
+
---
|
|
797
|
+
|
|
798
|
+
## Related Standards
|
|
799
|
+
|
|
800
|
+
- [Security Standards](security-standards.md) — Data encryption, access control, PII handling
|
|
801
|
+
- [Performance Standards](performance-standards.md) — Application-level performance optimization
|
|
802
|
+
- [Logging Standards](logging-standards.md) — Structured logging for database operations
|
|
803
|
+
- [Deployment Standards](deployment-standards.md) — Database migration as part of deployment pipeline
|
|
804
|
+
|
|
805
|
+
---
|
|
806
|
+
|
|
807
|
+
## Version History
|
|
808
|
+
|
|
809
|
+
| Version | Date | Changes |
|
|
810
|
+
|---------|------|---------|
|
|
811
|
+
| 1.0.0 | 2026-03-18 | Initial release |
|
|
812
|
+
|
|
813
|
+
---
|
|
814
|
+
|
|
815
|
+
## References
|
|
816
|
+
|
|
817
|
+
- [ISO/IEC 9075 — SQL Standard](https://www.iso.org/standard/76583.html)
|
|
818
|
+
- [Use The Index, Luke](https://use-the-index-luke.com/) — SQL Indexing and Tuning
|
|
819
|
+
- [SQL Style Guide](https://www.sqlstyle.guide/) — Consistent SQL formatting
|
|
820
|
+
- [Martin Fowler — Evolutionary Database Design](https://martinfowler.com/articles/evodb.html)
|
|
821
|
+
- [Designing Data-Intensive Applications](https://dataintensive.net/) — Martin Kleppmann
|
|
822
|
+
- [PostgreSQL Documentation](https://www.postgresql.org/docs/)
|
|
823
|
+
|
|
824
|
+
---
|
|
825
|
+
|
|
826
|
+
## License
|
|
827
|
+
|
|
828
|
+
This standard is released under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/).
|