profiling-slow-api-endpoints
Skill721 starsChanged 58 days agoMITView on GitHub
Profiles slow PostHog API endpoints when the main cost is in Postgres or Python. Use when a screen, picker, or list is slow; a Django endpoint has high tail latency; a query plan changes with tenant size; or a proposed database fix needs production evidence. Covers APM traces, safe production EXPLAIN, representative measurements, implementation choices, tests, rollout, and post-deploy verification. For ClickHouse or HogQL latency, use `optimizing-clickhouse-and-hogql-queries` instead.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- PostHog/posthogword for word, first seen Oct 4, 2026
Other versions1 project uses a version of this.
Word for word · 1
- PostHog/posthog★ 40k100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- AGENTS.mdAGENTS.md
- common/AGENTS.mdAGENTS.md
- proto/AGENTS.mdAGENTS.md
- rust/AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- common/CLAUDE.mdCLAUDE.md
- proto/CLAUDE.mdCLAUDE.md
- rust/CLAUDE.mdCLAUDE.md
- .cursor/rules/django-python.mdcCursor rule
- .cursor/rules/react-typescript.mdcCursor rule
- .cursor/rules/rust.mdcCursor rule
- adding-activity-loggingSkill
- adding-api-scopesSkill
- adding-inbound-webhooksSkill
- adding-inbox-sourcesSkill
- adding-ingestion-warningsSkill
- adding-mcp-store-serversSkill
- adding-personhog-rpcSkill
- adding-product-alertingSkill
- adding-project-secret-api-key-authSkill
- adopting-generated-api-typesSkill
- analyzing-experiment-precompute-canarySkill
- analyzing-experiment-query-performanceSkill
- analyzing-insights-across-teamsSkill
- announcing-behavior-changesSkill
- auditing-llm-gateway-paritySkill
- auditing-warehouse-source-coverageSkill
- authenticating-to-clickhouseSkill
- authoring-ci-workflowsSkill
- autoresolving-pr-conflictsSkill
- building-product-empty-statesSkill
- clickhouse-migrationsSkill
- debugging-ci-failuresSkill
- debugging-local-replaySkill
- debugging-local-task-agent-runsSkill
- debugging-mcp-analyticsSkill
- debugging-signals-pipelineSkill
- debugging-table-access-deniedSkill
- depot-ciSkill
- depot-container-buildsSkill
- depot-github-runnersSkill
- developing-subscriptionsSkill
- django-migrationsSkill
- django-startup-timeSkill
- documenting-warehouse-sourcesSkill
- editing-agents-mdSkill
- establishing-code-ownershipSkill
- extending-hobby-smoke-testsSkill
- extending-personhog-test-harnessSkill
- finding-llm-gateway-migration-candidatesSkill
- fixing-flaky-testsSkill
- gating-production-deploysSkill
- gating-sensitive-actionsSkill
- generating-clickhouse-query-performance-reportsSkill
- hogliSkill
- implementing-mcp-toolsSkill
- implementing-mcp-ui-appsSkill
- implementing-warehouse-sourcesSkill
- improving-drf-endpointsSkill
- improving-mcp-toolsSkill
- The first 60 shown.
The file
Show its rules and text
We split this skill into the rules it gives an agent
22 rules. About 0 are common, 19 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈19
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 15
Use this skill when a PostHog API request spends most of its time in Postgres or Python.
- Very specific: line 25
Read slow APM traces with posthog:query-apm-spans and the exploring-apm-traces skill.
- Distinctive: line 33
Follow the request to the function that creates the work.
- Distinctive: line 34
Do not optimize a view wrapper when another function owns the delay.
- Distinctive: line 39
Keep its filters, ordering, and page size.
- Distinctive: line 40
Check for repeated queries, count queries, and work that does not block the response.
- Very specific: line 48
Use EXPLAIN (ANALYZE, BUFFERS) only when the exact SELECT is safe to run.
- Distinctive: line 53
Remove work that the response does not need before you change a query or add an index.
- Distinctive: line 55
Run each more than once, record the cache state, and verify equal results.
- Distinctive: line 58
Test both sides of a plan crossover before you select a threshold.
- Distinctive: line 61
Prefer one plan when it performs well across the measured range.
- Distinctive: line 62
If a size check or cache selects the plan:
- Distinctive: line 64
keep the check cheaper than the work it avoids
- Distinctive: line 66
keep the request working when the cache fails
- Distinctive: line 69
Use a feature flag when behavior is uncertain or the change needs a staged rollout.
- Distinctive: line 70
Do not use a flag as a substitute for measurements.
- Distinctive: line 74
Test public behavior at the lowest useful level.
- Distinctive: line 75
Add plan or SQL-shape tests only when the improvement depends on that shape.
- Distinctive: line 76
Test each plan and the failure path when the code selects between plans.
- Distinctive: line 77
Use a representative database for plan and timing comparisons because unit tests cannot prove latency.
- Distinctive: line 79
After deployment, check the same latency measure, request volume, error rate, and database load.
- Distinctive: line 81
Remove the change if it does not improve the user action without a regression.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Profiling slow API endpoints
Use this skill when a PostHog API request spends most of its time in Postgres or Python.
For ClickHouse and HogQL, use
optimizing-clickhouse-and-hogql-queries.
Measure the same user action before and after the change. A faster query does not help if the action stays slow.
Find the source of the delay
Record the page action, endpoint, request shape, affected users, request volume, and a latency percentile such as p95.
Read slow APM traces with posthog:query-apm-spans and the exploring-apm-traces skill.
The distribution shows how often requests are slow.
The traces show where they spend time.
- Django ORM and
cursor.executespans point to Postgres. - ClickHouse work belongs in the ClickHouse skill.
- Time outside database spans can indicate repeated calls, serialization, or excess data loading.
Follow the request to the function that creates the work. Do not optimize a view wrapper when another function owns the delay.
Capture the exact work
Get the SQL and parameters from a slow request. Keep its filters, ordering, and page size. Check for repeated queries, count queries, and work that does not block the response. A reduced query can use a different plan. Local data and statistics can also produce a different plan.
Use
querying-production-databases-via-metabase
to inspect the production read replica.
Start with EXPLAIN.
Use EXPLAIN (ANALYZE, BUFFERS) only when the exact SELECT is safe to run.
Inspect row estimates, indexes, join types, loops, filters, sorts, and buffer use.
Compare the smallest useful change
Remove work that the response does not need before you change a query or add an index. Compare the original and candidate with the same parameters. Run each more than once, record the cache state, and verify equal results.
Measure the tenant-size distribution when plans can change with tenant size. Test both sides of a plan crossover before you select a threshold. One tenant is not enough evidence for a conditional plan.
Prefer one plan when it performs well across the measured range. If a size check or cache selects the plan:
- keep the check cheaper than the work it avoids
- let stale data affect latency, not results
- keep the request working when the cache fails
- record the selected plan on the request span
Use a feature flag when behavior is uncertain or the change needs a staged rollout. Do not use a flag as a substitute for measurements.
Verify the change
Test public behavior at the lowest useful level. Add plan or SQL-shape tests only when the improvement depends on that shape. Test each plan and the failure path when the code selects between plans. Use a representative database for plan and timing comparisons because unit tests cannot prove latency.
After deployment, check the same latency measure, request volume, error rate, and database load. Compare recorded plan attributes when more than one plan exists. Remove the change if it does not improve the user action without a regression.
Common mistakes
- Starting with a code theory instead of a slow trace.
- Measuring only the mean or one warm query.
- Testing SQL that differs from the endpoint SQL.
- Selecting a threshold from one tenant.
- Moving the first response behind optional work.
- Adding an index before checking the current plan.
- Declaring success from tests instead of the production measure.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

