Skip to content

psycopg2: cursor.execute doesn't allow vars argument #2279

Open
@MarcinWieczorek

Description

@MarcinWieczorek

Describe the bug: ...
psycopg2's Cursor can be invoked like this: cursor.execute(sql, vars=...) [0]. This works without instrumentation, but fails when wrapped.

To Reproduce

  1. Spin up apm and postgres
  2. execute any cursor with vars argument with instrumentation enabled (example provided)

Environment (please complete the following information)

  • OS: GNU
  • Python version: 3.13.2
  • APM Server version: 8.13.0
  • Agent version: 6.23.0

Additional context
Resources:
[0] https://www.psycopg.org/docs/cursor.html#cursor.execute
Code example:

import psycopg2
from elasticapm.traces import capture_span
from elasticapm import instrument


def query_db():
    conn = psycopg2.connect("host=localhost dbname=test user=postgres password=postgres port=15432")
    with conn.cursor() as cur:
        cur = conn.cursor()
        cur.execute("SELECT now()", vars=[1])
        print(cur.fetchone())
    conn.close()

if __name__ == "__main__":
    query_db()
    instrument()
    query_db()

Output

(datetime.datetime(2025, 4, 23, 13, 0, 56, 460818, tzinfo=datetime.timezone.utc),)
Traceback (most recent call last):
  File "/home/marcin/git/elasticapm-psycopg2-bug/hello.py", line 17, in <module>
    query_db()
    ~~~~~~~~^^
  File "/home/marcin/git/elasticapm-psycopg2-bug/hello.py", line 10, in query_db
    cur.execute("SELECT now()", vars=[1])
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: CursorProxy.execute() got an unexpected keyword argument 'vars'

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions