Skip to content

psycopg: TypeError keyword not supported: prepare #2488

@piotrpawlaczek

Description

@piotrpawlaczek

Description
When using Django with the recommended psycopg[c] v3 adapter and Elastic APM enabled, executing a QuerySet.iterator() fails with: TypeError: keyword not supported: prepare.

This occurs because psycopg v3’s server-side cursor implementation rejects the prepare=keyword argument, which is being passed through Elastic APM's database instrumentation.

To Reproduce

  1. Install Django with the recommended PostgreSQL adapter (psycopg[c] v3.2.12).
  2. Enable Elastic APM in the Django project.
  3. Execute a Django query using QuerySet.iterator(), for example:
for obj in MyModel.objects.iterator():
    ...

Environment

  • OS: Linux, Debian
  • Python version: 3.12.12
  • Framework and version Django 4.2
  • APM Server version: 9
  • Agent version: 6.24.0
  • Server side cursors: enabled

Expected behavior:
The queryset should be processed normally using a server-side cursor without errors.

Actual behavior:
TypeError is raised: keyword not supported: prepare

psycopg/_server_cursor.py in execute at line 88

   ) -> Self:
        """
        Open a cursor to execute a query to the database.
        """
        if kwargs: # <---- here {"prepare": None}
            raise TypeError(f"keyword not supported: {list(kwargs)[0]}")

        if self._pgconn.pipeline_status:
            raise e.NotSupportedError(
                "server-side cursors not supported in pipeline mode"
            )

Additional context

  • The error does not occur if Elastic APM is disabled.
  • The error does not occur when using the not-recommended psycopg v2
  • The issue appears to be caused by Elastic APM adding a prepare=None parameter during its instrumentation of database queries.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions