@kolbo/kolbo-code-linux-arm64-musl 1.1.74 → 2.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/kolbo +0 -0
- package/package.json +1 -1
- package/skills/brainstorming/SKILL.md +164 -0
- package/skills/brainstorming/scripts/frame-template.html +214 -0
- package/skills/brainstorming/scripts/helper.js +88 -0
- package/skills/brainstorming/scripts/server.cjs +354 -0
- package/skills/brainstorming/scripts/start-server.sh +148 -0
- package/skills/brainstorming/scripts/stop-server.sh +56 -0
- package/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
- package/skills/brainstorming/visual-companion.md +287 -0
- package/skills/dispatching-parallel-agents/SKILL.md +182 -0
- package/skills/docx/.skillfish.json +10 -0
- package/skills/docx/SKILL.md +196 -0
- package/skills/docx/docx-js.md +350 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/skills/docx/ooxml/schemas/mce/mc.xsd +75 -0
- package/skills/docx/ooxml/schemas/microsoft/wml-2010.xsd +560 -0
- package/skills/docx/ooxml/schemas/microsoft/wml-2012.xsd +67 -0
- package/skills/docx/ooxml/schemas/microsoft/wml-2018.xsd +14 -0
- package/skills/docx/ooxml/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/skills/docx/ooxml/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/skills/docx/ooxml/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/skills/docx/ooxml/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/skills/docx/ooxml/scripts/pack.py +159 -0
- package/skills/docx/ooxml/scripts/unpack.py +29 -0
- package/skills/docx/ooxml/scripts/validate.py +69 -0
- package/skills/docx/ooxml/scripts/validation/__init__.py +15 -0
- package/skills/docx/ooxml/scripts/validation/base.py +951 -0
- package/skills/docx/ooxml/scripts/validation/docx.py +274 -0
- package/skills/docx/ooxml/scripts/validation/pptx.py +315 -0
- package/skills/docx/ooxml/scripts/validation/redlining.py +279 -0
- package/skills/docx/ooxml.md +599 -0
- package/skills/docx/scripts/__init__.py +1 -0
- package/skills/docx/scripts/document.py +1272 -0
- package/skills/docx/scripts/templates/comments.xml +3 -0
- package/skills/docx/scripts/templates/commentsExtended.xml +3 -0
- package/skills/docx/scripts/templates/commentsExtensible.xml +3 -0
- package/skills/docx/scripts/templates/commentsIds.xml +3 -0
- package/skills/docx/scripts/templates/people.xml +3 -0
- package/skills/docx/scripts/utilities.py +374 -0
- package/skills/executing-plans/SKILL.md +70 -0
- package/skills/finishing-a-development-branch/SKILL.md +200 -0
- package/skills/fullstack-app/SKILL.md +621 -0
- package/skills/kolbo/SKILL.md +19 -263
- package/skills/ollama-vision/SKILL.md +105 -0
- package/skills/pdf/.skillfish.json +10 -0
- package/skills/pdf/FORMS.md +205 -0
- package/skills/pdf/REFERENCE.md +612 -0
- package/skills/pdf/SKILL.md +293 -0
- package/skills/pdf/scripts/check_bounding_boxes.py +70 -0
- package/skills/pdf/scripts/check_bounding_boxes_test.py +226 -0
- package/skills/pdf/scripts/check_fillable_fields.py +12 -0
- package/skills/pdf/scripts/convert_pdf_to_images.py +35 -0
- package/skills/pdf/scripts/create_validation_image.py +41 -0
- package/skills/pdf/scripts/extract_form_field_info.py +152 -0
- package/skills/pdf/scripts/fill_fillable_fields.py +114 -0
- package/skills/pdf/scripts/fill_pdf_form_with_annotations.py +108 -0
- package/skills/photo-studio/SKILL.md +122 -0
- package/skills/pptx/.skillfish.json +10 -0
- package/skills/pptx/SKILL.md +483 -0
- package/skills/pptx/html2pptx.md +626 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +1499 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +146 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +1085 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +11 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-main.xsd +3081 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +23 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +185 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +287 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd +1676 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +28 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +144 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +174 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +25 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +18 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +59 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +56 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +195 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-math.xsd +582 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +25 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/sml.xsd +4439 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-main.xsd +570 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +509 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +12 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +108 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +96 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/wml.xsd +3646 -0
- package/skills/pptx/ooxml/schemas/ISO-IEC29500-4_2016/xml.xsd +116 -0
- package/skills/pptx/ooxml/schemas/ecma/fouth-edition/opc-contentTypes.xsd +42 -0
- package/skills/pptx/ooxml/schemas/ecma/fouth-edition/opc-coreProperties.xsd +50 -0
- package/skills/pptx/ooxml/schemas/ecma/fouth-edition/opc-digSig.xsd +49 -0
- package/skills/pptx/ooxml/schemas/ecma/fouth-edition/opc-relationships.xsd +33 -0
- package/skills/pptx/ooxml/schemas/mce/mc.xsd +75 -0
- package/skills/pptx/ooxml/schemas/microsoft/wml-2010.xsd +560 -0
- package/skills/pptx/ooxml/schemas/microsoft/wml-2012.xsd +67 -0
- package/skills/pptx/ooxml/schemas/microsoft/wml-2018.xsd +14 -0
- package/skills/pptx/ooxml/schemas/microsoft/wml-cex-2018.xsd +20 -0
- package/skills/pptx/ooxml/schemas/microsoft/wml-cid-2016.xsd +13 -0
- package/skills/pptx/ooxml/schemas/microsoft/wml-sdtdatahash-2020.xsd +4 -0
- package/skills/pptx/ooxml/schemas/microsoft/wml-symex-2015.xsd +8 -0
- package/skills/pptx/ooxml/scripts/pack.py +159 -0
- package/skills/pptx/ooxml/scripts/unpack.py +29 -0
- package/skills/pptx/ooxml/scripts/validate.py +69 -0
- package/skills/pptx/ooxml/scripts/validation/__init__.py +15 -0
- package/skills/pptx/ooxml/scripts/validation/base.py +951 -0
- package/skills/pptx/ooxml/scripts/validation/docx.py +274 -0
- package/skills/pptx/ooxml/scripts/validation/pptx.py +315 -0
- package/skills/pptx/ooxml/scripts/validation/redlining.py +279 -0
- package/skills/pptx/ooxml.md +427 -0
- package/skills/pptx/scripts/html2pptx.js +995 -0
- package/skills/pptx/scripts/inventory.py +1020 -0
- package/skills/pptx/scripts/rearrange.py +231 -0
- package/skills/pptx/scripts/replace.py +385 -0
- package/skills/pptx/scripts/thumbnail.py +450 -0
- package/skills/receiving-code-review/SKILL.md +213 -0
- package/skills/requesting-code-review/SKILL.md +105 -0
- package/skills/requesting-code-review/code-reviewer.md +146 -0
- package/skills/subagent-driven-development/SKILL.md +277 -0
- package/skills/subagent-driven-development/code-quality-reviewer-prompt.md +26 -0
- package/skills/subagent-driven-development/implementer-prompt.md +113 -0
- package/skills/subagent-driven-development/spec-reviewer-prompt.md +61 -0
- package/skills/supabase/.skillfish.json +10 -0
- package/skills/supabase/SKILL.md +106 -0
- package/skills/supabase/assets/feedback-issue-template.md +17 -0
- package/skills/supabase/references/skill-feedback.md +17 -0
- package/skills/supabase-postgres-best-practices/.skillfish.json +10 -0
- package/skills/supabase-postgres-best-practices/SKILL.md +64 -0
- package/skills/supabase-postgres-best-practices/references/_contributing.md +170 -0
- package/skills/supabase-postgres-best-practices/references/_sections.md +39 -0
- package/skills/supabase-postgres-best-practices/references/_template.md +34 -0
- package/skills/supabase-postgres-best-practices/references/advanced-full-text-search.md +55 -0
- package/skills/supabase-postgres-best-practices/references/advanced-jsonb-indexing.md +49 -0
- package/skills/supabase-postgres-best-practices/references/conn-idle-timeout.md +46 -0
- package/skills/supabase-postgres-best-practices/references/conn-limits.md +44 -0
- package/skills/supabase-postgres-best-practices/references/conn-pooling.md +41 -0
- package/skills/supabase-postgres-best-practices/references/conn-prepared-statements.md +46 -0
- package/skills/supabase-postgres-best-practices/references/data-batch-inserts.md +54 -0
- package/skills/supabase-postgres-best-practices/references/data-n-plus-one.md +53 -0
- package/skills/supabase-postgres-best-practices/references/data-pagination.md +50 -0
- package/skills/supabase-postgres-best-practices/references/data-upsert.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-advisory.md +56 -0
- package/skills/supabase-postgres-best-practices/references/lock-deadlock-prevention.md +68 -0
- package/skills/supabase-postgres-best-practices/references/lock-short-transactions.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-skip-locked.md +54 -0
- package/skills/supabase-postgres-best-practices/references/monitor-explain-analyze.md +45 -0
- package/skills/supabase-postgres-best-practices/references/monitor-pg-stat-statements.md +55 -0
- package/skills/supabase-postgres-best-practices/references/monitor-vacuum-analyze.md +55 -0
- package/skills/supabase-postgres-best-practices/references/query-composite-indexes.md +44 -0
- package/skills/supabase-postgres-best-practices/references/query-covering-indexes.md +40 -0
- package/skills/supabase-postgres-best-practices/references/query-index-types.md +48 -0
- package/skills/supabase-postgres-best-practices/references/query-missing-indexes.md +43 -0
- package/skills/supabase-postgres-best-practices/references/query-partial-indexes.md +45 -0
- package/skills/supabase-postgres-best-practices/references/schema-constraints.md +80 -0
- package/skills/supabase-postgres-best-practices/references/schema-data-types.md +46 -0
- package/skills/supabase-postgres-best-practices/references/schema-foreign-key-indexes.md +59 -0
- package/skills/supabase-postgres-best-practices/references/schema-lowercase-identifiers.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-partitioning.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-primary-keys.md +61 -0
- package/skills/supabase-postgres-best-practices/references/security-privileges.md +54 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-basics.md +50 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-performance.md +57 -0
- package/skills/supabase-quickstart/SKILL.md +400 -0
- package/skills/systematic-debugging/CREATION-LOG.md +119 -0
- package/skills/systematic-debugging/SKILL.md +296 -0
- package/skills/systematic-debugging/condition-based-waiting-example.ts +158 -0
- package/skills/systematic-debugging/condition-based-waiting.md +115 -0
- package/skills/systematic-debugging/defense-in-depth.md +122 -0
- package/skills/systematic-debugging/find-polluter.sh +63 -0
- package/skills/systematic-debugging/root-cause-tracing.md +169 -0
- package/skills/systematic-debugging/test-academic.md +14 -0
- package/skills/systematic-debugging/test-pressure-1.md +58 -0
- package/skills/systematic-debugging/test-pressure-2.md +68 -0
- package/skills/systematic-debugging/test-pressure-3.md +69 -0
- package/skills/test-driven-development/SKILL.md +371 -0
- package/skills/test-driven-development/testing-anti-patterns.md +299 -0
- package/skills/using-git-worktrees/SKILL.md +218 -0
- package/skills/using-superpowers/SKILL.md +115 -0
- package/skills/using-superpowers/references/codex-tools.md +100 -0
- package/skills/using-superpowers/references/gemini-tools.md +33 -0
- package/skills/verification-before-completion/SKILL.md +139 -0
- package/skills/video-production/SKILL.md +8 -7
- package/skills/writing-plans/SKILL.md +152 -0
- package/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
- package/skills/writing-skills/SKILL.md +655 -0
- package/skills/writing-skills/anthropic-best-practices.md +1150 -0
- package/skills/writing-skills/examples/CLAUDE_MD_TESTING.md +189 -0
- package/skills/writing-skills/graphviz-conventions.dot +172 -0
- package/skills/writing-skills/persuasion-principles.md +187 -0
- package/skills/writing-skills/render-graphs.js +168 -0
- package/skills/writing-skills/testing-skills-with-subagents.md +384 -0
- package/skills/xlsx/.skillfish.json +10 -0
- package/skills/xlsx/SKILL.md +288 -0
- package/skills/xlsx/recalc.py +178 -0
- package/skills/color-grading/SKILL.md +0 -152
- package/skills/ffmpeg-patterns/SKILL.md +0 -240
- package/skills/image-prompting-guide/SKILL.md +0 -143
- package/skills/music-prompting/SKILL.md +0 -146
- package/skills/production-review/SKILL.md +0 -152
- package/skills/short-form-video/SKILL.md +0 -168
- package/skills/sound-design/SKILL.md +0 -154
- package/skills/storytelling/SKILL.md +0 -139
- package/skills/subtitle-production/SKILL.md +0 -244
- package/skills/subtitle-production/reference/burn_to_video.py +0 -222
- package/skills/subtitle-production/reference/export_srts.py +0 -127
- package/skills/subtitle-production/reference/gen_srt.py +0 -42
- package/skills/typography-video/SKILL.md +0 -182
- package/skills/typography-video/reference/KineticTitleScene.tsx +0 -345
- package/skills/video-editing/SKILL.md +0 -128
- package/skills/video-prompting-guide/SKILL.md +0 -268
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Enable pg_stat_statements for Query Analysis
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: Identify top resource-consuming queries
|
|
5
|
+
tags: pg-stat-statements, monitoring, statistics, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Enable pg_stat_statements for Query Analysis
|
|
9
|
+
|
|
10
|
+
pg_stat_statements tracks execution statistics for all queries, helping identify slow and frequent queries.
|
|
11
|
+
|
|
12
|
+
**Incorrect (no visibility into query patterns):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Database is slow, but which queries are the problem?
|
|
16
|
+
-- No way to know without pg_stat_statements
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Correct (enable and query pg_stat_statements):**
|
|
20
|
+
|
|
21
|
+
```sql
|
|
22
|
+
-- Enable the extension
|
|
23
|
+
create extension if not exists pg_stat_statements;
|
|
24
|
+
|
|
25
|
+
-- Find slowest queries by total time
|
|
26
|
+
select
|
|
27
|
+
calls,
|
|
28
|
+
round(total_exec_time::numeric, 2) as total_time_ms,
|
|
29
|
+
round(mean_exec_time::numeric, 2) as mean_time_ms,
|
|
30
|
+
query
|
|
31
|
+
from pg_stat_statements
|
|
32
|
+
order by total_exec_time desc
|
|
33
|
+
limit 10;
|
|
34
|
+
|
|
35
|
+
-- Find most frequent queries
|
|
36
|
+
select calls, query
|
|
37
|
+
from pg_stat_statements
|
|
38
|
+
order by calls desc
|
|
39
|
+
limit 10;
|
|
40
|
+
|
|
41
|
+
-- Reset statistics after optimization
|
|
42
|
+
select pg_stat_statements_reset();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Key metrics to monitor:
|
|
46
|
+
|
|
47
|
+
```sql
|
|
48
|
+
-- Queries with high mean time (candidates for optimization)
|
|
49
|
+
select query, mean_exec_time, calls
|
|
50
|
+
from pg_stat_statements
|
|
51
|
+
where mean_exec_time > 100 -- > 100ms average
|
|
52
|
+
order by mean_exec_time desc;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Reference: [pg_stat_statements](https://supabase.com/docs/guides/database/extensions/pg_stat_statements)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Maintain Table Statistics with VACUUM and ANALYZE
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: 2-10x better query plans with accurate statistics
|
|
5
|
+
tags: vacuum, analyze, statistics, maintenance, autovacuum
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Maintain Table Statistics with VACUUM and ANALYZE
|
|
9
|
+
|
|
10
|
+
Outdated statistics cause the query planner to make poor decisions. VACUUM reclaims space, ANALYZE updates statistics.
|
|
11
|
+
|
|
12
|
+
**Incorrect (stale statistics):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Table has 1M rows but stats say 1000
|
|
16
|
+
-- Query planner chooses wrong strategy
|
|
17
|
+
explain select * from orders where status = 'pending';
|
|
18
|
+
-- Shows: Seq Scan (because stats show small table)
|
|
19
|
+
-- Actually: Index Scan would be much faster
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Correct (maintain fresh statistics):**
|
|
23
|
+
|
|
24
|
+
```sql
|
|
25
|
+
-- Manually analyze after large data changes
|
|
26
|
+
analyze orders;
|
|
27
|
+
|
|
28
|
+
-- Analyze specific columns used in WHERE clauses
|
|
29
|
+
analyze orders (status, created_at);
|
|
30
|
+
|
|
31
|
+
-- Check when tables were last analyzed
|
|
32
|
+
select
|
|
33
|
+
relname,
|
|
34
|
+
last_vacuum,
|
|
35
|
+
last_autovacuum,
|
|
36
|
+
last_analyze,
|
|
37
|
+
last_autoanalyze
|
|
38
|
+
from pg_stat_user_tables
|
|
39
|
+
order by last_analyze nulls first;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Autovacuum tuning for busy tables:
|
|
43
|
+
|
|
44
|
+
```sql
|
|
45
|
+
-- Increase frequency for high-churn tables
|
|
46
|
+
alter table orders set (
|
|
47
|
+
autovacuum_vacuum_scale_factor = 0.05, -- Vacuum at 5% dead tuples (default 20%)
|
|
48
|
+
autovacuum_analyze_scale_factor = 0.02 -- Analyze at 2% changes (default 10%)
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
-- Check autovacuum status
|
|
52
|
+
select * from pg_stat_progress_vacuum;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Reference: [VACUUM](https://supabase.com/docs/guides/database/database-size#vacuum-operations)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Create Composite Indexes for Multi-Column Queries
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 5-10x faster multi-column queries
|
|
5
|
+
tags: indexes, composite-index, multi-column, query-optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Create Composite Indexes for Multi-Column Queries
|
|
9
|
+
|
|
10
|
+
When queries filter on multiple columns, a composite index is more efficient than separate single-column indexes.
|
|
11
|
+
|
|
12
|
+
**Incorrect (separate indexes require bitmap scan):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Two separate indexes
|
|
16
|
+
create index orders_status_idx on orders (status);
|
|
17
|
+
create index orders_created_idx on orders (created_at);
|
|
18
|
+
|
|
19
|
+
-- Query must combine both indexes (slower)
|
|
20
|
+
select * from orders where status = 'pending' and created_at > '2024-01-01';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (composite index):**
|
|
24
|
+
|
|
25
|
+
```sql
|
|
26
|
+
-- Single composite index (leftmost column first for equality checks)
|
|
27
|
+
create index orders_status_created_idx on orders (status, created_at);
|
|
28
|
+
|
|
29
|
+
-- Query uses one efficient index scan
|
|
30
|
+
select * from orders where status = 'pending' and created_at > '2024-01-01';
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Column order matters** - place equality columns first, range columns last:
|
|
34
|
+
|
|
35
|
+
```sql
|
|
36
|
+
-- Good: status (=) before created_at (>)
|
|
37
|
+
create index idx on orders (status, created_at);
|
|
38
|
+
|
|
39
|
+
-- Works for: WHERE status = 'pending'
|
|
40
|
+
-- Works for: WHERE status = 'pending' AND created_at > '2024-01-01'
|
|
41
|
+
-- Does NOT work for: WHERE created_at > '2024-01-01' (leftmost prefix rule)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Reference: [Multicolumn Indexes](https://www.postgresql.org/docs/current/indexes-multicolumn.html)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Covering Indexes to Avoid Table Lookups
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: 2-5x faster queries by eliminating heap fetches
|
|
5
|
+
tags: indexes, covering-index, include, index-only-scan
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Covering Indexes to Avoid Table Lookups
|
|
9
|
+
|
|
10
|
+
Covering indexes include all columns needed by a query, enabling index-only scans that skip the table entirely.
|
|
11
|
+
|
|
12
|
+
**Incorrect (index scan + heap fetch):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create index users_email_idx on users (email);
|
|
16
|
+
|
|
17
|
+
-- Must fetch name and created_at from table heap
|
|
18
|
+
select email, name, created_at from users where email = 'user@example.com';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (index-only scan with INCLUDE):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
-- Include non-searchable columns in the index
|
|
25
|
+
create index users_email_idx on users (email) include (name, created_at);
|
|
26
|
+
|
|
27
|
+
-- All columns served from index, no table access needed
|
|
28
|
+
select email, name, created_at from users where email = 'user@example.com';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Use INCLUDE for columns you SELECT but don't filter on:
|
|
32
|
+
|
|
33
|
+
```sql
|
|
34
|
+
-- Searching by status, but also need customer_id and total
|
|
35
|
+
create index orders_status_idx on orders (status) include (customer_id, total);
|
|
36
|
+
|
|
37
|
+
select status, customer_id, total from orders where status = 'shipped';
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Reference: [Index-Only Scans](https://www.postgresql.org/docs/current/indexes-index-only-scans.html)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Choose the Right Index Type for Your Data
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 10-100x improvement with correct index type
|
|
5
|
+
tags: indexes, btree, gin, gist, brin, hash, index-types
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Choose the Right Index Type for Your Data
|
|
9
|
+
|
|
10
|
+
Different index types excel at different query patterns. The default B-tree isn't always optimal.
|
|
11
|
+
|
|
12
|
+
**Incorrect (B-tree for JSONB containment):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- B-tree cannot optimize containment operators
|
|
16
|
+
create index products_attrs_idx on products (attributes);
|
|
17
|
+
select * from products where attributes @> '{"color": "red"}';
|
|
18
|
+
-- Full table scan - B-tree doesn't support @> operator
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (GIN for JSONB):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
-- GIN supports @>, ?, ?&, ?| operators
|
|
25
|
+
create index products_attrs_idx on products using gin (attributes);
|
|
26
|
+
select * from products where attributes @> '{"color": "red"}';
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Index type guide:
|
|
30
|
+
|
|
31
|
+
```sql
|
|
32
|
+
-- B-tree (default): =, <, >, BETWEEN, IN, IS NULL
|
|
33
|
+
create index users_created_idx on users (created_at);
|
|
34
|
+
|
|
35
|
+
-- GIN: arrays, JSONB, full-text search
|
|
36
|
+
create index posts_tags_idx on posts using gin (tags);
|
|
37
|
+
|
|
38
|
+
-- GiST: geometric data, range types, nearest-neighbor (KNN) queries
|
|
39
|
+
create index locations_idx on places using gist (location);
|
|
40
|
+
|
|
41
|
+
-- BRIN: large time-series tables (10-100x smaller)
|
|
42
|
+
create index events_time_idx on events using brin (created_at);
|
|
43
|
+
|
|
44
|
+
-- Hash: equality-only (slightly faster than B-tree for =)
|
|
45
|
+
create index sessions_token_idx on sessions using hash (token);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Reference: [Index Types](https://www.postgresql.org/docs/current/indexes-types.html)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Add Indexes on WHERE and JOIN Columns
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: 100-1000x faster queries on large tables
|
|
5
|
+
tags: indexes, performance, sequential-scan, query-optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Add Indexes on WHERE and JOIN Columns
|
|
9
|
+
|
|
10
|
+
Queries filtering or joining on unindexed columns cause full table scans, which become exponentially slower as tables grow.
|
|
11
|
+
|
|
12
|
+
**Incorrect (sequential scan on large table):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- No index on customer_id causes full table scan
|
|
16
|
+
select * from orders where customer_id = 123;
|
|
17
|
+
|
|
18
|
+
-- EXPLAIN shows: Seq Scan on orders (cost=0.00..25000.00 rows=100 width=85)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (index scan):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
-- Create index on frequently filtered column
|
|
25
|
+
create index orders_customer_id_idx on orders (customer_id);
|
|
26
|
+
|
|
27
|
+
select * from orders where customer_id = 123;
|
|
28
|
+
|
|
29
|
+
-- EXPLAIN shows: Index Scan using orders_customer_id_idx (cost=0.42..8.44 rows=100 width=85)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
For JOIN columns, always index the foreign key side:
|
|
33
|
+
|
|
34
|
+
```sql
|
|
35
|
+
-- Index the referencing column
|
|
36
|
+
create index orders_customer_id_idx on orders (customer_id);
|
|
37
|
+
|
|
38
|
+
select c.name, o.total
|
|
39
|
+
from customers c
|
|
40
|
+
join orders o on o.customer_id = c.id;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Reference: [Query Optimization](https://supabase.com/docs/guides/database/query-optimization)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Partial Indexes for Filtered Queries
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 5-20x smaller indexes, faster writes and queries
|
|
5
|
+
tags: indexes, partial-index, query-optimization, storage
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Partial Indexes for Filtered Queries
|
|
9
|
+
|
|
10
|
+
Partial indexes only include rows matching a WHERE condition, making them smaller and faster when queries consistently filter on the same condition.
|
|
11
|
+
|
|
12
|
+
**Incorrect (full index includes irrelevant rows):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Index includes all rows, even soft-deleted ones
|
|
16
|
+
create index users_email_idx on users (email);
|
|
17
|
+
|
|
18
|
+
-- Query always filters active users
|
|
19
|
+
select * from users where email = 'user@example.com' and deleted_at is null;
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Correct (partial index matches query filter):**
|
|
23
|
+
|
|
24
|
+
```sql
|
|
25
|
+
-- Index only includes active users
|
|
26
|
+
create index users_active_email_idx on users (email)
|
|
27
|
+
where deleted_at is null;
|
|
28
|
+
|
|
29
|
+
-- Query uses the smaller, faster index
|
|
30
|
+
select * from users where email = 'user@example.com' and deleted_at is null;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Common use cases for partial indexes:
|
|
34
|
+
|
|
35
|
+
```sql
|
|
36
|
+
-- Only pending orders (status rarely changes once completed)
|
|
37
|
+
create index orders_pending_idx on orders (created_at)
|
|
38
|
+
where status = 'pending';
|
|
39
|
+
|
|
40
|
+
-- Only non-null values
|
|
41
|
+
create index products_sku_idx on products (sku)
|
|
42
|
+
where sku is not null;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Reference: [Partial Indexes](https://www.postgresql.org/docs/current/indexes-partial.html)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Add Constraints Safely in Migrations
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Prevents migration failures and enables idempotent schema changes
|
|
5
|
+
tags: constraints, migrations, schema, alter-table
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Add Constraints Safely in Migrations
|
|
9
|
+
|
|
10
|
+
PostgreSQL does not support `ADD CONSTRAINT IF NOT EXISTS`. Migrations using this syntax will fail.
|
|
11
|
+
|
|
12
|
+
**Incorrect (causes syntax error):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- ERROR: syntax error at or near "not" (SQLSTATE 42601)
|
|
16
|
+
alter table public.profiles
|
|
17
|
+
add constraint if not exists profiles_birthchart_id_unique unique (birthchart_id);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Correct (idempotent constraint creation):**
|
|
21
|
+
|
|
22
|
+
```sql
|
|
23
|
+
-- Use DO block to check before adding
|
|
24
|
+
do $$
|
|
25
|
+
begin
|
|
26
|
+
if not exists (
|
|
27
|
+
select 1 from pg_constraint
|
|
28
|
+
where conname = 'profiles_birthchart_id_unique'
|
|
29
|
+
and conrelid = 'public.profiles'::regclass
|
|
30
|
+
) then
|
|
31
|
+
alter table public.profiles
|
|
32
|
+
add constraint profiles_birthchart_id_unique unique (birthchart_id);
|
|
33
|
+
end if;
|
|
34
|
+
end $$;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For all constraint types:
|
|
38
|
+
|
|
39
|
+
```sql
|
|
40
|
+
-- Check constraints
|
|
41
|
+
do $$
|
|
42
|
+
begin
|
|
43
|
+
if not exists (
|
|
44
|
+
select 1 from pg_constraint
|
|
45
|
+
where conname = 'check_age_positive'
|
|
46
|
+
) then
|
|
47
|
+
alter table users add constraint check_age_positive check (age > 0);
|
|
48
|
+
end if;
|
|
49
|
+
end $$;
|
|
50
|
+
|
|
51
|
+
-- Foreign keys
|
|
52
|
+
do $$
|
|
53
|
+
begin
|
|
54
|
+
if not exists (
|
|
55
|
+
select 1 from pg_constraint
|
|
56
|
+
where conname = 'profiles_birthchart_id_fkey'
|
|
57
|
+
) then
|
|
58
|
+
alter table profiles
|
|
59
|
+
add constraint profiles_birthchart_id_fkey
|
|
60
|
+
foreign key (birthchart_id) references birthcharts(id);
|
|
61
|
+
end if;
|
|
62
|
+
end $$;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Check if constraint exists:
|
|
66
|
+
|
|
67
|
+
```sql
|
|
68
|
+
-- Query to check constraint existence
|
|
69
|
+
select conname, contype, pg_get_constraintdef(oid)
|
|
70
|
+
from pg_constraint
|
|
71
|
+
where conrelid = 'public.profiles'::regclass;
|
|
72
|
+
|
|
73
|
+
-- contype values:
|
|
74
|
+
-- 'p' = PRIMARY KEY
|
|
75
|
+
-- 'f' = FOREIGN KEY
|
|
76
|
+
-- 'u' = UNIQUE
|
|
77
|
+
-- 'c' = CHECK
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Reference: [Constraints](https://www.postgresql.org/docs/current/ddl-constraints.html)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Choose Appropriate Data Types
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 50% storage reduction, faster comparisons
|
|
5
|
+
tags: data-types, schema, storage, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Choose Appropriate Data Types
|
|
9
|
+
|
|
10
|
+
Using the right data types reduces storage, improves query performance, and prevents bugs.
|
|
11
|
+
|
|
12
|
+
**Incorrect (wrong data types):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create table users (
|
|
16
|
+
id int, -- Will overflow at 2.1 billion
|
|
17
|
+
email varchar(255), -- Unnecessary length limit
|
|
18
|
+
created_at timestamp, -- Missing timezone info
|
|
19
|
+
is_active varchar(5), -- String for boolean
|
|
20
|
+
price varchar(20) -- String for numeric
|
|
21
|
+
);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Correct (appropriate data types):**
|
|
25
|
+
|
|
26
|
+
```sql
|
|
27
|
+
create table users (
|
|
28
|
+
id bigint generated always as identity primary key, -- 9 quintillion max
|
|
29
|
+
email text, -- No artificial limit, same performance as varchar
|
|
30
|
+
created_at timestamptz, -- Always store timezone-aware timestamps
|
|
31
|
+
is_active boolean default true, -- 1 byte vs variable string length
|
|
32
|
+
price numeric(10,2) -- Exact decimal arithmetic
|
|
33
|
+
);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Key guidelines:
|
|
37
|
+
|
|
38
|
+
```sql
|
|
39
|
+
-- IDs: use bigint, not int (future-proofing)
|
|
40
|
+
-- Strings: use text, not varchar(n) unless constraint needed
|
|
41
|
+
-- Time: use timestamptz, not timestamp
|
|
42
|
+
-- Money: use numeric, not float (precision matters)
|
|
43
|
+
-- Enums: use text with check constraint or create enum type
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Reference: [Data Types](https://www.postgresql.org/docs/current/datatype.html)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Index Foreign Key Columns
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 10-100x faster JOINs and CASCADE operations
|
|
5
|
+
tags: foreign-key, indexes, joins, schema
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Index Foreign Key Columns
|
|
9
|
+
|
|
10
|
+
Postgres does not automatically index foreign key columns. Missing indexes cause slow JOINs and CASCADE operations.
|
|
11
|
+
|
|
12
|
+
**Incorrect (unindexed foreign key):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create table orders (
|
|
16
|
+
id bigint generated always as identity primary key,
|
|
17
|
+
customer_id bigint references customers(id) on delete cascade,
|
|
18
|
+
total numeric(10,2)
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
-- No index on customer_id!
|
|
22
|
+
-- JOINs and ON DELETE CASCADE both require full table scan
|
|
23
|
+
select * from orders where customer_id = 123; -- Seq Scan
|
|
24
|
+
delete from customers where id = 123; -- Locks table, scans all orders
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (indexed foreign key):**
|
|
28
|
+
|
|
29
|
+
```sql
|
|
30
|
+
create table orders (
|
|
31
|
+
id bigint generated always as identity primary key,
|
|
32
|
+
customer_id bigint references customers(id) on delete cascade,
|
|
33
|
+
total numeric(10,2)
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
-- Always index the FK column
|
|
37
|
+
create index orders_customer_id_idx on orders (customer_id);
|
|
38
|
+
|
|
39
|
+
-- Now JOINs and cascades are fast
|
|
40
|
+
select * from orders where customer_id = 123; -- Index Scan
|
|
41
|
+
delete from customers where id = 123; -- Uses index, fast cascade
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Find missing FK indexes:
|
|
45
|
+
|
|
46
|
+
```sql
|
|
47
|
+
select
|
|
48
|
+
conrelid::regclass as table_name,
|
|
49
|
+
a.attname as fk_column
|
|
50
|
+
from pg_constraint c
|
|
51
|
+
join pg_attribute a on a.attrelid = c.conrelid and a.attnum = any(c.conkey)
|
|
52
|
+
where c.contype = 'f'
|
|
53
|
+
and not exists (
|
|
54
|
+
select 1 from pg_index i
|
|
55
|
+
where i.indrelid = c.conrelid and a.attnum = any(i.indkey)
|
|
56
|
+
);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Reference: [Foreign Keys](https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-FK)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Lowercase Identifiers for Compatibility
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Avoid case-sensitivity bugs with tools, ORMs, and AI assistants
|
|
5
|
+
tags: naming, identifiers, case-sensitivity, schema, conventions
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Lowercase Identifiers for Compatibility
|
|
9
|
+
|
|
10
|
+
PostgreSQL folds unquoted identifiers to lowercase. Quoted mixed-case identifiers require quotes forever and cause issues with tools, ORMs, and AI assistants that may not recognize them.
|
|
11
|
+
|
|
12
|
+
**Incorrect (mixed-case identifiers):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Quoted identifiers preserve case but require quotes everywhere
|
|
16
|
+
CREATE TABLE "Users" (
|
|
17
|
+
"userId" bigint PRIMARY KEY,
|
|
18
|
+
"firstName" text,
|
|
19
|
+
"lastName" text
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
-- Must always quote or queries fail
|
|
23
|
+
SELECT "firstName" FROM "Users" WHERE "userId" = 1;
|
|
24
|
+
|
|
25
|
+
-- This fails - Users becomes users without quotes
|
|
26
|
+
SELECT firstName FROM Users;
|
|
27
|
+
-- ERROR: relation "users" does not exist
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**Correct (lowercase snake_case):**
|
|
31
|
+
|
|
32
|
+
```sql
|
|
33
|
+
-- Unquoted lowercase identifiers are portable and tool-friendly
|
|
34
|
+
CREATE TABLE users (
|
|
35
|
+
user_id bigint PRIMARY KEY,
|
|
36
|
+
first_name text,
|
|
37
|
+
last_name text
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
-- Works without quotes, recognized by all tools
|
|
41
|
+
SELECT first_name FROM users WHERE user_id = 1;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Common sources of mixed-case identifiers:
|
|
45
|
+
|
|
46
|
+
```sql
|
|
47
|
+
-- ORMs often generate quoted camelCase - configure them to use snake_case
|
|
48
|
+
-- Migrations from other databases may preserve original casing
|
|
49
|
+
-- Some GUI tools quote identifiers by default - disable this
|
|
50
|
+
|
|
51
|
+
-- If stuck with mixed-case, create views as a compatibility layer
|
|
52
|
+
CREATE VIEW users AS SELECT "userId" AS user_id, "firstName" AS first_name FROM "Users";
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Reference: [Identifiers and Key Words](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Partition Large Tables for Better Performance
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: 5-20x faster queries and maintenance on large tables
|
|
5
|
+
tags: partitioning, large-tables, time-series, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Partition Large Tables for Better Performance
|
|
9
|
+
|
|
10
|
+
Partitioning splits a large table into smaller pieces, improving query performance and maintenance operations.
|
|
11
|
+
|
|
12
|
+
**Incorrect (single large table):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create table events (
|
|
16
|
+
id bigint generated always as identity,
|
|
17
|
+
created_at timestamptz,
|
|
18
|
+
data jsonb
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
-- 500M rows, queries scan everything
|
|
22
|
+
select * from events where created_at > '2024-01-01'; -- Slow
|
|
23
|
+
vacuum events; -- Takes hours, locks table
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Correct (partitioned by time range):**
|
|
27
|
+
|
|
28
|
+
```sql
|
|
29
|
+
create table events (
|
|
30
|
+
id bigint generated always as identity,
|
|
31
|
+
created_at timestamptz not null,
|
|
32
|
+
data jsonb
|
|
33
|
+
) partition by range (created_at);
|
|
34
|
+
|
|
35
|
+
-- Create partitions for each month
|
|
36
|
+
create table events_2024_01 partition of events
|
|
37
|
+
for values from ('2024-01-01') to ('2024-02-01');
|
|
38
|
+
|
|
39
|
+
create table events_2024_02 partition of events
|
|
40
|
+
for values from ('2024-02-01') to ('2024-03-01');
|
|
41
|
+
|
|
42
|
+
-- Queries only scan relevant partitions
|
|
43
|
+
select * from events where created_at > '2024-01-15'; -- Only scans events_2024_01+
|
|
44
|
+
|
|
45
|
+
-- Drop old data instantly
|
|
46
|
+
drop table events_2023_01; -- Instant vs DELETE taking hours
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
When to partition:
|
|
50
|
+
|
|
51
|
+
- Tables > 100M rows
|
|
52
|
+
- Time-series data with date-based queries
|
|
53
|
+
- Need to efficiently drop old data
|
|
54
|
+
|
|
55
|
+
Reference: [Table Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Select Optimal Primary Key Strategy
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Better index locality, reduced fragmentation
|
|
5
|
+
tags: primary-key, identity, uuid, serial, schema
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Select Optimal Primary Key Strategy
|
|
9
|
+
|
|
10
|
+
Primary key choice affects insert performance, index size, and replication
|
|
11
|
+
efficiency.
|
|
12
|
+
|
|
13
|
+
**Incorrect (problematic PK choices):**
|
|
14
|
+
|
|
15
|
+
```sql
|
|
16
|
+
-- identity is the SQL-standard approach
|
|
17
|
+
create table users (
|
|
18
|
+
id serial primary key -- Works, but IDENTITY is recommended
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
-- Random UUIDs (v4) cause index fragmentation
|
|
22
|
+
create table orders (
|
|
23
|
+
id uuid default gen_random_uuid() primary key -- UUIDv4 = random = scattered inserts
|
|
24
|
+
);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (optimal PK strategies):**
|
|
28
|
+
|
|
29
|
+
```sql
|
|
30
|
+
-- Use IDENTITY for sequential IDs (SQL-standard, best for most cases)
|
|
31
|
+
create table users (
|
|
32
|
+
id bigint generated always as identity primary key
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
-- For distributed systems needing UUIDs, use UUIDv7 (time-ordered)
|
|
36
|
+
-- Requires pg_uuidv7 extension: create extension pg_uuidv7;
|
|
37
|
+
create table orders (
|
|
38
|
+
id uuid default uuid_generate_v7() primary key -- Time-ordered, no fragmentation
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
-- Alternative: time-prefixed IDs for sortable, distributed IDs (no extension needed)
|
|
42
|
+
create table events (
|
|
43
|
+
id text default concat(
|
|
44
|
+
to_char(now() at time zone 'utc', 'YYYYMMDDHH24MISSMS'),
|
|
45
|
+
gen_random_uuid()::text
|
|
46
|
+
) primary key
|
|
47
|
+
);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Guidelines:
|
|
51
|
+
|
|
52
|
+
- Single database: `bigint identity` (sequential, 8 bytes, SQL-standard)
|
|
53
|
+
- Distributed/exposed IDs: UUIDv7 (requires pg_uuidv7) or ULID (time-ordered, no
|
|
54
|
+
fragmentation)
|
|
55
|
+
- `serial` works but `identity` is SQL-standard and preferred for new
|
|
56
|
+
applications
|
|
57
|
+
- Avoid random UUIDs (v4) as primary keys on large tables (causes index
|
|
58
|
+
fragmentation)
|
|
59
|
+
|
|
60
|
+
Reference:
|
|
61
|
+
[Identity Columns](https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-GENERATED-IDENTITY)
|