#35021: Debug query capturing on psycopg3 disregards execute wrappers
-------------------------------------+-------------------------------------
               Reporter:  Ran        |          Owner:  nobody
  Benita                             |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  5.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 == Problem

 I use [https://docs.djangoproject.com/en/4.2/topics/db/instrumentation
 /#connection-execute-wrapper connection.execute_wrapper] as a way to add
 comments to certain queries (see code at the end for reference). And I
 test the wrapper using `assertNumQueries` (see code at end for reference).
 Once I switched from psycopg2 to psycopg3, this test started failing: the
 unmodified query is captured, instead of the query modified by the execute
 wrapper. Note however that the modified query is what is actually sent to
 the DB, so this is only a debug issue.

 I expect it's the same for `django.db.backends` debug logs but I haven't
 verified this.

 == Analysis

 I've traced the issue to the following code in
 `postgresql.DatabaseOperations.last_executed_query`
 
https://github.com/django/django/blob/66d58e77de3196404e0820a6fef0a6144186015a/django/db/backends/postgresql/operations.py#L296-L311:

 {{{
 #!python

     if is_psycopg3:

         def last_executed_query(self, cursor, sql, params):
             try:
                 return self.compose_sql(sql, params)
             except errors.DataError:
                 return None

     else:

         def last_executed_query(self, cursor, sql, params):
             # https://www.psycopg.org/docs/cursor.html#cursor.query
             # The query attribute is a Psycopg extension to the DB API
 2.0.
             if cursor.query is not None:
                 return cursor.query.decode()
             return None
 }}}

 psycopg2 uses `cursor.query` which ends up being the modified query.
 psycopg3 uses whatever's passed in which ends up being the unmodified
 query.

 It seems like psycopg3 has an equivalent in
 [https://www.psycopg.org/psycopg3/docs/api/cursors.html#psycopg.Cursor._query
 cursor._query]. It is documented in the API reference but with a warning
 "You shouldn’t consider it part of the public interface of the object: it
 might change without warnings. [...] If you would like to build reliable
 features using this object, please get in touch so we can try and design
 an useful interface for it.". So if this is the desired solution, will
 need to work with psycopg to expose a stable interface.

 == Reproduction code

 Example execute wrapper:

 {{{
 #!python
 def db_comment_wrapper(comment: str) -> AbstractContextManager[None]:
     def handler(execute, sql, params, many, context):
         clean_comment = escape(comment)
         return execute(f'/* {clean_comment} */ {sql}', params, many,
 context)
     return db_connection.execute_wrapper(handler)
 }}}

 Test:

 {{{
 #!python
 with self.assertNumQueries(1) as captured:
     with db_comment_wrapper('This is a comment'):
         list(ContentType.objects.all())
 sql = captured[0]['sql']
 assert sql.startswith('/* This is a comment */')
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/35021>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018c3f1fbd3d-93443bed-909f-4211-9e23-7e2fe44cfc35-000000%40eu-central-1.amazonses.com.

Reply via email to