Greetings Django developers,
I'd like to explicitly declare some of my database transactions as read only to enable certain optimizations, but after inspecting the documentation, forums and source code, it seems this feature is missing.
Use atomic(using=None, savepoint=True) with the appropriate parameters, either as a decorator or a context manager.
It would be nice to have an additional parameter:
atomic(using=None, savepoint=True, readonly=False)
I understand that Django defaults to the READ COMMITTED isolation level - just like most databases it supports - and at that level, there isn't much to gain with read only transactions. But if one chooses REPEATABLE READ or SERIALIZABLE, certain transaction aborts could be avoided and transactional throughput could be increased by declaring transactions not modifying shared data explicitly as read only.
This is also a very common feature, so most users could make use of it regardless of the database they use:
Note: Using PostgreSQL, one can add an additional keyword to read only: deferrable.
It makes sure that the read only transaction will never be aborted, even under serializable isolation lever, in exchange of some possible delay of the start of its execution.
This could also be added to atomic with a default value of False, and be ignored on other databases which don't support it.
Please consider implementing this feature, giving us a standard way to declare our transactions as read only when needed!
Best regards,
Andor