@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,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Configure Idle Connection Timeouts
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Reclaim 30-50% of connection slots from idle clients
|
|
5
|
+
tags: connections, timeout, idle, resource-management
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Configure Idle Connection Timeouts
|
|
9
|
+
|
|
10
|
+
Idle connections waste resources. Configure timeouts to automatically reclaim them.
|
|
11
|
+
|
|
12
|
+
**Incorrect (connections held indefinitely):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- No timeout configured
|
|
16
|
+
show idle_in_transaction_session_timeout; -- 0 (disabled)
|
|
17
|
+
|
|
18
|
+
-- Connections stay open forever, even when idle
|
|
19
|
+
select pid, state, state_change, query
|
|
20
|
+
from pg_stat_activity
|
|
21
|
+
where state = 'idle in transaction';
|
|
22
|
+
-- Shows transactions idle for hours, holding locks
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (automatic cleanup of idle connections):**
|
|
26
|
+
|
|
27
|
+
```sql
|
|
28
|
+
-- Terminate connections idle in transaction after 30 seconds
|
|
29
|
+
alter system set idle_in_transaction_session_timeout = '30s';
|
|
30
|
+
|
|
31
|
+
-- Terminate completely idle connections after 10 minutes
|
|
32
|
+
alter system set idle_session_timeout = '10min';
|
|
33
|
+
|
|
34
|
+
-- Reload configuration
|
|
35
|
+
select pg_reload_conf();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
For pooled connections, configure at the pooler level:
|
|
39
|
+
|
|
40
|
+
```ini
|
|
41
|
+
# pgbouncer.ini
|
|
42
|
+
server_idle_timeout = 60
|
|
43
|
+
client_idle_timeout = 300
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Reference: [Connection Timeouts](https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-IDLE-IN-TRANSACTION-SESSION-TIMEOUT)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Set Appropriate Connection Limits
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: Prevent database crashes and memory exhaustion
|
|
5
|
+
tags: connections, max-connections, limits, stability
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Set Appropriate Connection Limits
|
|
9
|
+
|
|
10
|
+
Too many connections exhaust memory and degrade performance. Set limits based on available resources.
|
|
11
|
+
|
|
12
|
+
**Incorrect (unlimited or excessive connections):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Default max_connections = 100, but often increased blindly
|
|
16
|
+
show max_connections; -- 500 (way too high for 4GB RAM)
|
|
17
|
+
|
|
18
|
+
-- Each connection uses 1-3MB RAM
|
|
19
|
+
-- 500 connections * 2MB = 1GB just for connections!
|
|
20
|
+
-- Out of memory errors under load
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (calculate based on resources):**
|
|
24
|
+
|
|
25
|
+
```sql
|
|
26
|
+
-- Formula: max_connections = (RAM in MB / 5MB per connection) - reserved
|
|
27
|
+
-- For 4GB RAM: (4096 / 5) - 10 = ~800 theoretical max
|
|
28
|
+
-- But practically, 100-200 is better for query performance
|
|
29
|
+
|
|
30
|
+
-- Recommended settings for 4GB RAM
|
|
31
|
+
alter system set max_connections = 100;
|
|
32
|
+
|
|
33
|
+
-- Also set work_mem appropriately
|
|
34
|
+
-- work_mem * max_connections should not exceed 25% of RAM
|
|
35
|
+
alter system set work_mem = '8MB'; -- 8MB * 100 = 800MB max
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Monitor connection usage:
|
|
39
|
+
|
|
40
|
+
```sql
|
|
41
|
+
select count(*), state from pg_stat_activity group by state;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Reference: [Database Connections](https://supabase.com/docs/guides/platform/performance#connection-management)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Connection Pooling for All Applications
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: Handle 10-100x more concurrent users
|
|
5
|
+
tags: connection-pooling, pgbouncer, performance, scalability
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Connection Pooling for All Applications
|
|
9
|
+
|
|
10
|
+
Postgres connections are expensive (1-3MB RAM each). Without pooling, applications exhaust connections under load.
|
|
11
|
+
|
|
12
|
+
**Incorrect (new connection per request):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Each request creates a new connection
|
|
16
|
+
-- Application code: db.connect() per request
|
|
17
|
+
-- Result: 500 concurrent users = 500 connections = crashed database
|
|
18
|
+
|
|
19
|
+
-- Check current connections
|
|
20
|
+
select count(*) from pg_stat_activity; -- 487 connections!
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (connection pooling):**
|
|
24
|
+
|
|
25
|
+
```sql
|
|
26
|
+
-- Use a pooler like PgBouncer between app and database
|
|
27
|
+
-- Application connects to pooler, pooler reuses a small pool to Postgres
|
|
28
|
+
|
|
29
|
+
-- Configure pool_size based on: (CPU cores * 2) + spindle_count
|
|
30
|
+
-- Example for 4 cores: pool_size = 10
|
|
31
|
+
|
|
32
|
+
-- Result: 500 concurrent users share 10 actual connections
|
|
33
|
+
select count(*) from pg_stat_activity; -- 10 connections
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Pool modes:
|
|
37
|
+
|
|
38
|
+
- **Transaction mode**: connection returned after each transaction (best for most apps)
|
|
39
|
+
- **Session mode**: connection held for entire session (needed for prepared statements, temp tables)
|
|
40
|
+
|
|
41
|
+
Reference: [Connection Pooling](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pooler)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Prepared Statements Correctly with Pooling
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Avoid prepared statement conflicts in pooled environments
|
|
5
|
+
tags: prepared-statements, connection-pooling, transaction-mode
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Prepared Statements Correctly with Pooling
|
|
9
|
+
|
|
10
|
+
Prepared statements are tied to individual database connections. In transaction-mode pooling, connections are shared, causing conflicts.
|
|
11
|
+
|
|
12
|
+
**Incorrect (named prepared statements with transaction pooling):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Named prepared statement
|
|
16
|
+
prepare get_user as select * from users where id = $1;
|
|
17
|
+
|
|
18
|
+
-- In transaction mode pooling, next request may get different connection
|
|
19
|
+
execute get_user(123);
|
|
20
|
+
-- ERROR: prepared statement "get_user" does not exist
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (use unnamed statements or session mode):**
|
|
24
|
+
|
|
25
|
+
```sql
|
|
26
|
+
-- Option 1: Use unnamed prepared statements (most ORMs do this automatically)
|
|
27
|
+
-- The query is prepared and executed in a single protocol message
|
|
28
|
+
|
|
29
|
+
-- Option 2: Deallocate after use in transaction mode
|
|
30
|
+
prepare get_user as select * from users where id = $1;
|
|
31
|
+
execute get_user(123);
|
|
32
|
+
deallocate get_user;
|
|
33
|
+
|
|
34
|
+
-- Option 3: Use session mode pooling (port 5432 vs 6543)
|
|
35
|
+
-- Connection is held for entire session, prepared statements persist
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Check your driver settings:
|
|
39
|
+
|
|
40
|
+
```sql
|
|
41
|
+
-- Many drivers use prepared statements by default
|
|
42
|
+
-- Node.js pg: { prepare: false } to disable
|
|
43
|
+
-- JDBC: prepareThreshold=0 to disable
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Reference: [Prepared Statements with Pooling](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pool-modes)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Batch INSERT Statements for Bulk Data
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: 10-50x faster bulk inserts
|
|
5
|
+
tags: batch, insert, bulk, performance, copy
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Batch INSERT Statements for Bulk Data
|
|
9
|
+
|
|
10
|
+
Individual INSERT statements have high overhead. Batch multiple rows in single statements or use COPY.
|
|
11
|
+
|
|
12
|
+
**Incorrect (individual inserts):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Each insert is a separate transaction and round trip
|
|
16
|
+
insert into events (user_id, action) values (1, 'click');
|
|
17
|
+
insert into events (user_id, action) values (1, 'view');
|
|
18
|
+
insert into events (user_id, action) values (2, 'click');
|
|
19
|
+
-- ... 1000 more individual inserts
|
|
20
|
+
|
|
21
|
+
-- 1000 inserts = 1000 round trips = slow
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Correct (batch insert):**
|
|
25
|
+
|
|
26
|
+
```sql
|
|
27
|
+
-- Multiple rows in single statement
|
|
28
|
+
insert into events (user_id, action) values
|
|
29
|
+
(1, 'click'),
|
|
30
|
+
(1, 'view'),
|
|
31
|
+
(2, 'click'),
|
|
32
|
+
-- ... up to ~1000 rows per batch
|
|
33
|
+
(999, 'view');
|
|
34
|
+
|
|
35
|
+
-- One round trip for 1000 rows
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
For large imports, use COPY:
|
|
39
|
+
|
|
40
|
+
```sql
|
|
41
|
+
-- COPY is fastest for bulk loading
|
|
42
|
+
copy events (user_id, action, created_at)
|
|
43
|
+
from '/path/to/data.csv'
|
|
44
|
+
with (format csv, header true);
|
|
45
|
+
|
|
46
|
+
-- Or from stdin in application
|
|
47
|
+
copy events (user_id, action) from stdin with (format csv);
|
|
48
|
+
1,click
|
|
49
|
+
1,view
|
|
50
|
+
2,click
|
|
51
|
+
\.
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Reference: [COPY](https://www.postgresql.org/docs/current/sql-copy.html)
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Eliminate N+1 Queries with Batch Loading
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: 10-100x fewer database round trips
|
|
5
|
+
tags: n-plus-one, batch, performance, queries
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Eliminate N+1 Queries with Batch Loading
|
|
9
|
+
|
|
10
|
+
N+1 queries execute one query per item in a loop. Batch them into a single query using arrays or JOINs.
|
|
11
|
+
|
|
12
|
+
**Incorrect (N+1 queries):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- First query: get all users
|
|
16
|
+
select id from users where active = true; -- Returns 100 IDs
|
|
17
|
+
|
|
18
|
+
-- Then N queries, one per user
|
|
19
|
+
select * from orders where user_id = 1;
|
|
20
|
+
select * from orders where user_id = 2;
|
|
21
|
+
select * from orders where user_id = 3;
|
|
22
|
+
-- ... 97 more queries!
|
|
23
|
+
|
|
24
|
+
-- Total: 101 round trips to database
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (single batch query):**
|
|
28
|
+
|
|
29
|
+
```sql
|
|
30
|
+
-- Collect IDs and query once with ANY
|
|
31
|
+
select * from orders where user_id = any(array[1, 2, 3, ...]);
|
|
32
|
+
|
|
33
|
+
-- Or use JOIN instead of loop
|
|
34
|
+
select u.id, u.name, o.*
|
|
35
|
+
from users u
|
|
36
|
+
left join orders o on o.user_id = u.id
|
|
37
|
+
where u.active = true;
|
|
38
|
+
|
|
39
|
+
-- Total: 1 round trip
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Application pattern:
|
|
43
|
+
|
|
44
|
+
```sql
|
|
45
|
+
-- Instead of looping in application code:
|
|
46
|
+
-- for user in users: db.query("SELECT * FROM orders WHERE user_id = $1", user.id)
|
|
47
|
+
|
|
48
|
+
-- Pass array parameter:
|
|
49
|
+
select * from orders where user_id = any($1::bigint[]);
|
|
50
|
+
-- Application passes: [1, 2, 3, 4, 5, ...]
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Reference: [N+1 Query Problem](https://supabase.com/docs/guides/database/query-optimization)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Cursor-Based Pagination Instead of OFFSET
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: Consistent O(1) performance regardless of page depth
|
|
5
|
+
tags: pagination, cursor, keyset, offset, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Cursor-Based Pagination Instead of OFFSET
|
|
9
|
+
|
|
10
|
+
OFFSET-based pagination scans all skipped rows, getting slower on deeper pages. Cursor pagination is O(1).
|
|
11
|
+
|
|
12
|
+
**Incorrect (OFFSET pagination):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Page 1: scans 20 rows
|
|
16
|
+
select * from products order by id limit 20 offset 0;
|
|
17
|
+
|
|
18
|
+
-- Page 100: scans 2000 rows to skip 1980
|
|
19
|
+
select * from products order by id limit 20 offset 1980;
|
|
20
|
+
|
|
21
|
+
-- Page 10000: scans 200,000 rows!
|
|
22
|
+
select * from products order by id limit 20 offset 199980;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (cursor/keyset pagination):**
|
|
26
|
+
|
|
27
|
+
```sql
|
|
28
|
+
-- Page 1: get first 20
|
|
29
|
+
select * from products order by id limit 20;
|
|
30
|
+
-- Application stores last_id = 20
|
|
31
|
+
|
|
32
|
+
-- Page 2: start after last ID
|
|
33
|
+
select * from products where id > 20 order by id limit 20;
|
|
34
|
+
-- Uses index, always fast regardless of page depth
|
|
35
|
+
|
|
36
|
+
-- Page 10000: same speed as page 1
|
|
37
|
+
select * from products where id > 199980 order by id limit 20;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
For multi-column sorting:
|
|
41
|
+
|
|
42
|
+
```sql
|
|
43
|
+
-- Cursor must include all sort columns
|
|
44
|
+
select * from products
|
|
45
|
+
where (created_at, id) > ('2024-01-15 10:00:00', 12345)
|
|
46
|
+
order by created_at, id
|
|
47
|
+
limit 20;
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reference: [Pagination](https://supabase.com/docs/guides/database/pagination)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use UPSERT for Insert-or-Update Operations
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Atomic operation, eliminates race conditions
|
|
5
|
+
tags: upsert, on-conflict, insert, update
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use UPSERT for Insert-or-Update Operations
|
|
9
|
+
|
|
10
|
+
Using separate SELECT-then-INSERT/UPDATE creates race conditions. Use INSERT ... ON CONFLICT for atomic upserts.
|
|
11
|
+
|
|
12
|
+
**Incorrect (check-then-insert race condition):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Race condition: two requests check simultaneously
|
|
16
|
+
select * from settings where user_id = 123 and key = 'theme';
|
|
17
|
+
-- Both find nothing
|
|
18
|
+
|
|
19
|
+
-- Both try to insert
|
|
20
|
+
insert into settings (user_id, key, value) values (123, 'theme', 'dark');
|
|
21
|
+
-- One succeeds, one fails with duplicate key error!
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Correct (atomic UPSERT):**
|
|
25
|
+
|
|
26
|
+
```sql
|
|
27
|
+
-- Single atomic operation
|
|
28
|
+
insert into settings (user_id, key, value)
|
|
29
|
+
values (123, 'theme', 'dark')
|
|
30
|
+
on conflict (user_id, key)
|
|
31
|
+
do update set value = excluded.value, updated_at = now();
|
|
32
|
+
|
|
33
|
+
-- Returns the inserted/updated row
|
|
34
|
+
insert into settings (user_id, key, value)
|
|
35
|
+
values (123, 'theme', 'dark')
|
|
36
|
+
on conflict (user_id, key)
|
|
37
|
+
do update set value = excluded.value
|
|
38
|
+
returning *;
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Insert-or-ignore pattern:
|
|
42
|
+
|
|
43
|
+
```sql
|
|
44
|
+
-- Insert only if not exists (no update)
|
|
45
|
+
insert into page_views (page_id, user_id)
|
|
46
|
+
values (1, 123)
|
|
47
|
+
on conflict (page_id, user_id) do nothing;
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reference: [INSERT ON CONFLICT](https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT)
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Advisory Locks for Application-Level Locking
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Efficient coordination without row-level lock overhead
|
|
5
|
+
tags: advisory-locks, coordination, application-locks
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Advisory Locks for Application-Level Locking
|
|
9
|
+
|
|
10
|
+
Advisory locks provide application-level coordination without requiring database rows to lock.
|
|
11
|
+
|
|
12
|
+
**Incorrect (creating rows just for locking):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Creating dummy rows to lock on
|
|
16
|
+
create table resource_locks (
|
|
17
|
+
resource_name text primary key
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
insert into resource_locks values ('report_generator');
|
|
21
|
+
|
|
22
|
+
-- Lock by selecting the row
|
|
23
|
+
select * from resource_locks where resource_name = 'report_generator' for update;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Correct (advisory locks):**
|
|
27
|
+
|
|
28
|
+
```sql
|
|
29
|
+
-- Session-level advisory lock (released on disconnect or unlock)
|
|
30
|
+
select pg_advisory_lock(hashtext('report_generator'));
|
|
31
|
+
-- ... do exclusive work ...
|
|
32
|
+
select pg_advisory_unlock(hashtext('report_generator'));
|
|
33
|
+
|
|
34
|
+
-- Transaction-level lock (released on commit/rollback)
|
|
35
|
+
begin;
|
|
36
|
+
select pg_advisory_xact_lock(hashtext('daily_report'));
|
|
37
|
+
-- ... do work ...
|
|
38
|
+
commit; -- Lock automatically released
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Try-lock for non-blocking operations:
|
|
42
|
+
|
|
43
|
+
```sql
|
|
44
|
+
-- Returns immediately with true/false instead of waiting
|
|
45
|
+
select pg_try_advisory_lock(hashtext('resource_name'));
|
|
46
|
+
|
|
47
|
+
-- Use in application
|
|
48
|
+
if (acquired) {
|
|
49
|
+
-- Do work
|
|
50
|
+
select pg_advisory_unlock(hashtext('resource_name'));
|
|
51
|
+
} else {
|
|
52
|
+
-- Skip or retry later
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Reference: [Advisory Locks](https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Prevent Deadlocks with Consistent Lock Ordering
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: Eliminate deadlock errors, improve reliability
|
|
5
|
+
tags: deadlocks, locking, transactions, ordering
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Prevent Deadlocks with Consistent Lock Ordering
|
|
9
|
+
|
|
10
|
+
Deadlocks occur when transactions lock resources in different orders. Always
|
|
11
|
+
acquire locks in a consistent order.
|
|
12
|
+
|
|
13
|
+
**Incorrect (inconsistent lock ordering):**
|
|
14
|
+
|
|
15
|
+
```sql
|
|
16
|
+
-- Transaction A -- Transaction B
|
|
17
|
+
begin; begin;
|
|
18
|
+
update accounts update accounts
|
|
19
|
+
set balance = balance - 100 set balance = balance - 50
|
|
20
|
+
where id = 1; where id = 2; -- B locks row 2
|
|
21
|
+
|
|
22
|
+
update accounts update accounts
|
|
23
|
+
set balance = balance + 100 set balance = balance + 50
|
|
24
|
+
where id = 2; -- A waits for B where id = 1; -- B waits for A
|
|
25
|
+
|
|
26
|
+
-- DEADLOCK! Both waiting for each other
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Correct (lock rows in consistent order first):**
|
|
30
|
+
|
|
31
|
+
```sql
|
|
32
|
+
-- Explicitly acquire locks in ID order before updating
|
|
33
|
+
begin;
|
|
34
|
+
select * from accounts where id in (1, 2) order by id for update;
|
|
35
|
+
|
|
36
|
+
-- Now perform updates in any order - locks already held
|
|
37
|
+
update accounts set balance = balance - 100 where id = 1;
|
|
38
|
+
update accounts set balance = balance + 100 where id = 2;
|
|
39
|
+
commit;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Alternative: use a single statement to update atomically:
|
|
43
|
+
|
|
44
|
+
```sql
|
|
45
|
+
-- Single statement acquires all locks atomically
|
|
46
|
+
begin;
|
|
47
|
+
update accounts
|
|
48
|
+
set balance = balance + case id
|
|
49
|
+
when 1 then -100
|
|
50
|
+
when 2 then 100
|
|
51
|
+
end
|
|
52
|
+
where id in (1, 2);
|
|
53
|
+
commit;
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Detect deadlocks in logs:
|
|
57
|
+
|
|
58
|
+
```sql
|
|
59
|
+
-- Check for recent deadlocks
|
|
60
|
+
select * from pg_stat_database where deadlocks > 0;
|
|
61
|
+
|
|
62
|
+
-- Enable deadlock logging
|
|
63
|
+
set log_lock_waits = on;
|
|
64
|
+
set deadlock_timeout = '1s';
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Reference:
|
|
68
|
+
[Deadlocks](https://www.postgresql.org/docs/current/explicit-locking.html#LOCKING-DEADLOCKS)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Keep Transactions Short to Reduce Lock Contention
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: 3-5x throughput improvement, fewer deadlocks
|
|
5
|
+
tags: transactions, locking, contention, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Keep Transactions Short to Reduce Lock Contention
|
|
9
|
+
|
|
10
|
+
Long-running transactions hold locks that block other queries. Keep transactions as short as possible.
|
|
11
|
+
|
|
12
|
+
**Incorrect (long transaction with external calls):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
begin;
|
|
16
|
+
select * from orders where id = 1 for update; -- Lock acquired
|
|
17
|
+
|
|
18
|
+
-- Application makes HTTP call to payment API (2-5 seconds)
|
|
19
|
+
-- Other queries on this row are blocked!
|
|
20
|
+
|
|
21
|
+
update orders set status = 'paid' where id = 1;
|
|
22
|
+
commit; -- Lock held for entire duration
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Correct (minimal transaction scope):**
|
|
26
|
+
|
|
27
|
+
```sql
|
|
28
|
+
-- Validate data and call APIs outside transaction
|
|
29
|
+
-- Application: response = await paymentAPI.charge(...)
|
|
30
|
+
|
|
31
|
+
-- Only hold lock for the actual update
|
|
32
|
+
begin;
|
|
33
|
+
update orders
|
|
34
|
+
set status = 'paid', payment_id = $1
|
|
35
|
+
where id = $2 and status = 'pending'
|
|
36
|
+
returning *;
|
|
37
|
+
commit; -- Lock held for milliseconds
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Use `statement_timeout` to prevent runaway transactions:
|
|
41
|
+
|
|
42
|
+
```sql
|
|
43
|
+
-- Abort queries running longer than 30 seconds
|
|
44
|
+
set statement_timeout = '30s';
|
|
45
|
+
|
|
46
|
+
-- Or per-session
|
|
47
|
+
set local statement_timeout = '5s';
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reference: [Transaction Management](https://www.postgresql.org/docs/current/tutorial-transactions.html)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use SKIP LOCKED for Non-Blocking Queue Processing
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: 10x throughput for worker queues
|
|
5
|
+
tags: skip-locked, queue, workers, concurrency
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use SKIP LOCKED for Non-Blocking Queue Processing
|
|
9
|
+
|
|
10
|
+
When multiple workers process a queue, SKIP LOCKED allows workers to process different rows without waiting.
|
|
11
|
+
|
|
12
|
+
**Incorrect (workers block each other):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Worker 1 and Worker 2 both try to get next job
|
|
16
|
+
begin;
|
|
17
|
+
select * from jobs where status = 'pending' order by created_at limit 1 for update;
|
|
18
|
+
-- Worker 2 waits for Worker 1's lock to release!
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (SKIP LOCKED for parallel processing):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
-- Each worker skips locked rows and gets the next available
|
|
25
|
+
begin;
|
|
26
|
+
select * from jobs
|
|
27
|
+
where status = 'pending'
|
|
28
|
+
order by created_at
|
|
29
|
+
limit 1
|
|
30
|
+
for update skip locked;
|
|
31
|
+
|
|
32
|
+
-- Worker 1 gets job 1, Worker 2 gets job 2 (no waiting)
|
|
33
|
+
|
|
34
|
+
update jobs set status = 'processing' where id = $1;
|
|
35
|
+
commit;
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Complete queue pattern:
|
|
39
|
+
|
|
40
|
+
```sql
|
|
41
|
+
-- Atomic claim-and-update in one statement
|
|
42
|
+
update jobs
|
|
43
|
+
set status = 'processing', worker_id = $1, started_at = now()
|
|
44
|
+
where id = (
|
|
45
|
+
select id from jobs
|
|
46
|
+
where status = 'pending'
|
|
47
|
+
order by created_at
|
|
48
|
+
limit 1
|
|
49
|
+
for update skip locked
|
|
50
|
+
)
|
|
51
|
+
returning *;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Reference: [SELECT FOR UPDATE SKIP LOCKED](https://www.postgresql.org/docs/current/sql-select.html#SQL-FOR-UPDATE-SHARE)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use EXPLAIN ANALYZE to Diagnose Slow Queries
|
|
3
|
+
impact: LOW-MEDIUM
|
|
4
|
+
impactDescription: Identify exact bottlenecks in query execution
|
|
5
|
+
tags: explain, analyze, diagnostics, query-plan
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use EXPLAIN ANALYZE to Diagnose Slow Queries
|
|
9
|
+
|
|
10
|
+
EXPLAIN ANALYZE executes the query and shows actual timings, revealing the true performance bottlenecks.
|
|
11
|
+
|
|
12
|
+
**Incorrect (guessing at performance issues):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Query is slow, but why?
|
|
16
|
+
select * from orders where customer_id = 123 and status = 'pending';
|
|
17
|
+
-- "It must be missing an index" - but which one?
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Correct (use EXPLAIN ANALYZE):**
|
|
21
|
+
|
|
22
|
+
```sql
|
|
23
|
+
explain (analyze, buffers, format text)
|
|
24
|
+
select * from orders where customer_id = 123 and status = 'pending';
|
|
25
|
+
|
|
26
|
+
-- Output reveals the issue:
|
|
27
|
+
-- Seq Scan on orders (cost=0.00..25000.00 rows=50 width=100) (actual time=0.015..450.123 rows=50 loops=1)
|
|
28
|
+
-- Filter: ((customer_id = 123) AND (status = 'pending'::text))
|
|
29
|
+
-- Rows Removed by Filter: 999950
|
|
30
|
+
-- Buffers: shared hit=5000 read=15000
|
|
31
|
+
-- Planning Time: 0.150 ms
|
|
32
|
+
-- Execution Time: 450.500 ms
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Key things to look for:
|
|
36
|
+
|
|
37
|
+
```sql
|
|
38
|
+
-- Seq Scan on large tables = missing index
|
|
39
|
+
-- Rows Removed by Filter = poor selectivity or missing index
|
|
40
|
+
-- Buffers: read >> hit = data not cached, needs more memory
|
|
41
|
+
-- Nested Loop with high loops = consider different join strategy
|
|
42
|
+
-- Sort Method: external merge = work_mem too low
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Reference: [EXPLAIN](https://supabase.com/docs/guides/database/inspect)
|