Document transaction/connection management outside the web server context
|Reported by:||Patryk Zawadzki||Owned by:||Aymeric Augustin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
First some background info:
- PostgreSQL provides several transaction isolation levels.
- Django's psycopg backend defaults to ghost-read-preventing isolation.
- psycopg isolation modes automatically create the isolating transaction whenever you execute your first SQL statement.
- psycopg expects you to tell it when you're done with your work by either running
- When you terminate the transaction, psycopg will wait for the next SQL statement and start a new isolating transaction.
The problem is: Django breaks assumption 4 and never commits or rollbacks the isolating transaction. Note I'm not talking about the transactions explicitly started by django, I'm talking about the transaction implicitly started by requesting a non-zero isolation level. This has the nasty side-effect of leaving the connection permanently in-transaction.
Try it yourselves: start a django server, make it use the DB and then try to - say -
VACUUM FULL the table it selected from. Or try to
ALTER the table (run a south migration). You will end up with a hanging connection, waiting for Django to leave the transaction block.
The attached patch makes it work for psycopg2 by introducing a new method in the backend class. It needs a call to
leave_transaction_management() so it works best with the
Now for a real solution:
I think it would be better to introduce some sort of
leave_isolation_block() API. "Enter" would call
set_isolation_level(1) (currently done unconditionally), "leave" would terminate the isolation transaction. Then introduce a default middleware that wraps all the views in these calls. It just doesn't seem intuitive to mix the transactions implicitly created by isolation API with the explicit transaction management API.
Change History (14)
comment:5 Changed 6 years ago by
|Triage Stage:||Unreviewed → Design decision needed|
comment:8 Changed 5 years ago by
|Component:||Database layer (models, ORM) → Documentation|
|Patch needs improvement:||set|
|Triage Stage:||Design decision needed → Accepted|
comment:9 Changed 5 years ago by
|Patch needs improvement:||unset|
|Summary:||psycopg2 backend never terminates isolating transactions → Document transaction/connection management outside the web server context|
|Type:||Bug → New feature|
|Version:||1.2 → master|
comment:11 Changed 4 years ago by
|Owner:||changed from nobody to Aymeric Augustin|
|Status:||new → assigned|