Skip to content

fix(asyncpg): Use distinct span ops for cursor iteration and fetch to prevent N+1 false positives#6609

Open
ericapisani wants to merge 3 commits into
masterfrom
py-2534-identify-db-ops-related-to-cursor-iteration
Open

fix(asyncpg): Use distinct span ops for cursor iteration and fetch to prevent N+1 false positives#6609
ericapisani wants to merge 3 commits into
masterfrom
py-2534-identify-db-ops-related-to-cursor-iteration

Conversation

@ericapisani

Copy link
Copy Markdown
Member

When asyncpg iterates a server-side cursor (via CursorIterator.anext) or fetches rows in batches (via Cursor.fetch), each batch triggers BaseCursor._bind_exec or BaseCursor._exec, producing a burst of identical spans. Sentry's N+1 detector incorrectly flags these as N+1 queries.

Set sentry.op to db.cursor.iter for CursorIterator-driven spans and db.cursor.fetch for Cursor.fetch-driven spans so the backend can exclude them from N+1 detection.

Adds span_op_override_value parameter to record_sql_queries to propagate the override without changing existing callers.

This will require changes on the backend in order to not trigger n+1 alerts on these ops.

Fixes #6576
Fixes PY-2534

… prevent N+1 false positives

When asyncpg iterates a server-side cursor (via CursorIterator.__anext__) or
fetches rows in batches (via Cursor.fetch), each batch triggers BaseCursor._bind_exec
or BaseCursor._exec, producing a burst of identical spans. Sentry's N+1 detector
incorrectly flags these as N+1 queries.

Set `sentry.op` to `db.cursor.iter` for CursorIterator-driven spans and `db.cursor.fetch`
for Cursor.fetch-driven spans so the backend can exclude them from N+1 detection.

Adds span_op_override_value parameter to record_sql_queries to propagate the
override without changing existing callers.

This will require changes on the backend in order to not trigger n+1 alerts on these
ops.

Fixes #6576
Fixes PY-2534
@linear-code

linear-code Bot commented Jun 19, 2026

Copy link
Copy Markdown

PY-2534

@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

92048 passed | ⏭️ 6140 skipped | Total: 98188 | Pass Rate: 93.75% | Execution Time: 322m 24s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +52
Passed Tests 📈 +52
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 93.33%. Project has 2414 uncovered lines.
✅ Project coverage is 89.85%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
sentry_sdk/integrations/asyncpg.py 92.86% ⚠️ 2 Missing and 2 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.85%    89.85%        —%
==========================================
  Files          192       192         —
  Lines        23745     23774       +29
  Branches      8198      8206        +8
==========================================
+ Hits         21334     21360       +26
- Misses        2411      2414        +3
- Partials      1344      1348        +4

Generated by Codecov Action

ericapisani added a commit to getsentry/sentry that referenced this pull request Jun 19, 2026
@ericapisani ericapisani marked this pull request as ready for review June 19, 2026 17:45
@ericapisani ericapisani requested a review from a team as a code owner June 19, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sentry detects N+1 queries in server side cursor

1 participant