agentleFS
PostHogKnown publisherPostHog/posthog-foss/.agents/skills/profiling-slow-api-endpoints/SKILL.md

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

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
  1. Very specific:

    Use this skill when a PostHog API request spends most of its time in Postgres or Python.

    line 15
  2. Very specific:

    Read slow APM traces with posthog:query-apm-spans and the exploring-apm-traces skill.

    line 25
  3. Distinctive:

    Follow the request to the function that creates the work.

    line 33
  4. Distinctive:

    Do not optimize a view wrapper when another function owns the delay.

    line 34
  5. Distinctive:

    Keep its filters, ordering, and page size.

    line 39
  6. Distinctive:

    Check for repeated queries, count queries, and work that does not block the response.

    line 40
  7. Very specific:

    Use EXPLAIN (ANALYZE, BUFFERS) only when the exact SELECT is safe to run.

    line 48
  8. Distinctive:

    Remove work that the response does not need before you change a query or add an index.

    line 53
  9. Distinctive:

    Run each more than once, record the cache state, and verify equal results.

    line 55
  10. Distinctive:

    Test both sides of a plan crossover before you select a threshold.

    line 58
  11. Distinctive:

    Prefer one plan when it performs well across the measured range.

    line 61
  12. Distinctive:

    If a size check or cache selects the plan:

    line 62
  13. Distinctive:

    keep the check cheaper than the work it avoids

    line 64
  14. Distinctive:

    keep the request working when the cache fails

    line 66
  15. Distinctive:

    Use a feature flag when behavior is uncertain or the change needs a staged rollout.

    line 69
  16. Distinctive:

    Do not use a flag as a substitute for measurements.

    line 70
  17. Distinctive:

    Test public behavior at the lowest useful level.

    line 74
  18. Distinctive:

    Add plan or SQL-shape tests only when the improvement depends on that shape.

    line 75
  19. Distinctive:

    Test each plan and the failure path when the code selects between plans.

    line 76
  20. Distinctive:

    Use a representative database for plan and timing comparisons because unit tests cannot prove latency.

    line 77
  21. Distinctive:

    After deployment, check the same latency measure, request volume, error rate, and database load.

    line 79
  22. Distinctive:

    Remove the change if it does not improve the user action without a regression.

    line 81

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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.execute spans 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.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.