Skip to content

fix(node): Prevent PostgresJs integration from emitting duplicate spans per query#21364

Merged
andreiborza merged 2 commits into
developfrom
ab/fix-postgresjs-duplicate-spans
Jun 8, 2026
Merged

fix(node): Prevent PostgresJs integration from emitting duplicate spans per query#21364
andreiborza merged 2 commits into
developfrom
ab/fix-postgresjs-duplicate-spans

Conversation

@andreiborza
Copy link
Copy Markdown
Member

postgres.js calls handle() from then/catch/finally, but only the first invocation executes SQL (guarded by this.executed). The patched handle was creating a new span on every call, inflating span count 2-3x.

Fixes #21355

@andreiborza andreiborza requested a review from a team as a code owner June 8, 2026 12:23
@andreiborza andreiborza requested review from JPeer264, Lms24, chargome, mydea and s1gr1d and removed request for a team, JPeer264 and mydea June 8, 2026 12:23
// Skip if this query came from an instrumented sql instance (already handled by wrapper),
// or if handle() was already called (postgres.js calls handle() from then/catch/finally —
// only the first call executes SQL, subsequent calls are no-ops).
if ((this as Record<symbol, unknown>)[QUERY_FROM_INSTRUMENTED_SQL] || this.executed) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: put this.executed as a first condition. Makes it a bit easier to read.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3e94d7e

andreiborza and others added 2 commits June 8, 2026 14:37
…ns per query

postgres.js calls handle() from then/catch/finally, but only the first
invocation executes SQL (guarded by this.executed). The patched handle
was creating a new span on every call, inflating span count 2-3x.

Fixes #21355

Co-Authored-By: Claude claude-opus-4-6 <noreply@anthropic.com>
Co-Authored-By: Claude claude-opus-4-6 <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@chargome chargome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one q, otherwise looks good!

const wrappedHandle = async function (this: { executed?: boolean }, ...args: unknown[]): Promise<unknown> {
// postgres.js calls handle() from then/catch/finally — only the first call executes SQL,
// subsequent calls are no-ops (guarded by this.executed). Skip span creation for no-ops.
if (this.executed || !_shouldCreateSpans(options)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Is this available on all versions that we support?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems to have been available since v3 which we target with >= 3.0.0 < 4.

@andreiborza andreiborza force-pushed the ab/fix-postgresjs-duplicate-spans branch from 10ecfaa to 3e94d7e Compare June 8, 2026 12:40
@andreiborza andreiborza merged commit c6f790b into develop Jun 8, 2026
271 of 272 checks passed
@andreiborza andreiborza deleted the ab/fix-postgresjs-duplicate-spans branch June 8, 2026 12:59
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.

PostgresJs integration emits 2-3 spans per SQL statement (Query lifecycle calls handle() multiple times)

3 participants