Skip to content

Conversation

@dspatoulas
Copy link

@dspatoulas dspatoulas commented Nov 18, 2025

Description

Update PsycopgInstrumentor.instrument_connection to support both sync and async psycopg connections by configuring the appropriate cursor_factory.

Previously, instrument_connection always used the sync cursor factory and did not properly handle AsyncConnection instances. As a result, calls made through an async connection would fail because of the async context.

With this change:

  • If connection is a psycopg.AsyncConnection, we set connection.cursor_factory to _new_cursor_async_factory(...).
  • Otherwise, we set connection.cursor_factory to _new_cursor_factory(...).

This aligns instrument_connection with the behavior of the global instrument() helper and ensures both sync and async connections are correctly traced.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

The tests in the psycopg instrumentation test suite to already cover both sync and async connections.

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Nov 18, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@dspatoulas dspatoulas force-pushed the fix-psycopg-async-cursor branch from 93f0638 to b3a3ee1 Compare November 18, 2025 16:28
@herin049
Copy link
Contributor

herin049 commented Nov 19, 2025

Can we add some unit tests to cover this scenario? Also make sure to add a changelog entry.

@dspatoulas
Copy link
Author

Can we add some unit tests to cover this scenario? Also make sure to add a changelog entry.

Yup, updated the existing tests and added a new test for this change.

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.

2 participants