Code

Opened 2 years ago

Closed 14 months ago

#18130 closed New feature (fixed)

postgres_psycopg2 adapter should not use hard coded isolation levels

Reported by: limscoder Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords: postgres
Cc: niwi@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Django's postgres_psycopg2 adapter sets the connection's isolation level to psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED when autocommit == False.

My application requires a more strict isolation level, so I am forced to create a new backend and override DatabaseWrapper._set_isolation_level to make sure the connection has the correct isolation level set.

There should be a configuration option within the database config in settings.py that allows users to set the appropriate isolation level if the default level is not correct for their application. It would also be nice if there was an option to not set the isolation level at all, and instead rely on the default isolation level provided by the Postgres server.

Attachments (0)

Change History (5)

comment:1 Changed 2 years ago by limscoder

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from postgres_psycopg2 adapter uses hard coded isolation levels to postgres_psycopg2 adapter should not use hard coded isolation levels

comment:2 Changed 22 months ago by niwi

  • Cc niwi@… added
  • Has patch set

Here the patch to allow to define the default isolation_level parameter on database configuracion dict: https://github.com/niwibe/django/compare/issue_18130

I run the tests (not all tests) and I have not seen any fail that might be related.

Last edited 22 months ago by niwi (previous) (diff)

comment:3 Changed 22 months ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

Yes, we could provide a way to switch to "serializable" for people with high concurrency and integrity requirements.

IIRC, under the "serializable" isolation level, the application should be prepared to handle transaction failures (and possibly retry) on every database query. I'm not sure we can handle this automatically in Django without introducing magic. It deserves a fat warning in the documentation. This isolation level is for people who know what they're doing :)

NB: there is some material on this topic in #3460.

comment:4 Changed 22 months ago by niwi

I think, does not need any kind of magic by django, as you say: "This isolation level is for people who know what they're doing."

I now update the patch with some checks for isolation_level value and documentation notes with warning.

comment:5 Changed 14 months ago by Aymeric Augustin <aymeric.augustin@…>

  • Resolution set to fixed
  • Status changed from new to closed

In e0449316ebacaa550e9c529f8c9cb9a9b44e3765:

Fixed #18130 -- Made the isolation level configurable on PostgreSQL.

Thanks limscoder for the report and niwi for the draft patch.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.