Troubleshooting¶
Symptom, cause, fix. Each entry is deliberately short and ends with a link to the page
that owns the full explanation. If your symptom is not here, run python manage.py check
and python manage.py check_rls --verbose first: between them they catch most
misconfigurations and name the fix in the message.
Nothing comes back¶
Every query returns zero rows¶
Symptom: The data is in the table (you can see it in psql as a superuser), but the
ORM, raw SQL and even Model.objects.count() return nothing. No error is raised.
Cause: No RLS context is active on that connection, so the policy's ELSE branch
compares tenant_id with NULL. This is the fail-closed design, not a bug. Typical
places: a management command, a shell session, a Celery task, a test, or a request with
an unauthenticated user.
Fix: Establish a context around the code: tenant_context(tenant_id) or
admin_context() (for an existing management command, run it through
run_as_tenant or run_for_all_tenants), .for_user(user) on the queryset, or RLSTenantMiddleware for
requests. While developing, set STRICT_MODE=True so this surfaces as a
NoTenantContextError instead of an empty result. Before 1.4.0, count(), exists(),
iterator() and the writes of a .for_user() queryset also returned nothing outside a
context (#131); upgrade, or wrap them in a context manager. With the
audit log at INFO (1.6.0+), the code path shows no
context.entered record before the query, or a context.denied one says why the context
was refused.
See: Context Managers,
Middleware,
STRICT_MODE,
Fail-Closed Design,
Audit Logging.
Queries made while streaming a response see no rows¶
Symptom: A StreamingHttpResponse whose generator queries RLS-protected models
yields nothing, although the view itself saw data.
Cause: The middleware's context ends when the view returns the response object, before the body is iterated.
Fix: Open tenant_context() / admin_context() inside the synchronous generator
and evaluate the queries there. The middleware's request transaction has already ended,
so with USE_LOCAL_SET=True you can enter transaction.atomic(using=alias) before the
context, using the same database alias for both. Since 1.4.0 that is optional on an alias
in RLS_TENANTS["DATABASES"]: without it, each statement runs in a transaction of its own
with the context's GUCs. In an async generator (ASGI), use
async with async_tenant_context() / async_admin_context() instead.
See: Transaction Per Request.
Work handed to another thread sees no rows¶
Symptom: Under RLSTenantMiddleware or a context manager, code run on another thread
returns zero rows, while the rest of the request sees its data.
Cause: That code runs on another thread with its own database connection, and the
context's GUCs are set on the connection of the thread that entered it. Changed in 1.4.0:
when the other thread got a copy of the context (sync_to_async(..., thread_sensitive=False),
asyncio.to_thread(), the error-page hop Django makes under ASGI), the
GUC affinity guard applies the context to that
thread's connection before each statement (with USE_LOCAL_SET=True, transaction-locally),
so this no longer happens there. It still happens when:
- the thread never got the context:
ThreadPoolExecutor.submit()andloop.run_in_executor()do not copy it; - the work runs after the context's block has ended (a task started and not awaited inside it): the guard applies a context only while it is live;
- the context was built by hand with
set_current_tenant_id()/set_rls_context_active()instead of a context manager or the middleware, or the GUCs were set by hand withset_guc()/rls_context(): the guard only applies the admin and tenant GUCs of contexts the library set up.
Before 1.4.0 such a connection got the request's GUCs only if it opened during that request, and kept them for later requests on its thread (#130).
Fix: Pass the context along (contextvars.copy_context().run, asyncio.to_thread()),
open tenant_context() / admin_context() inside the code that runs on the other thread, or
keep it on the request's thread with thread_sensitive=True (the default for
sync_to_async).
See: Running under ASGI, Context propagation.
Errors¶
NoTenantContextError with one of three hints¶
Symptom: The traceback ends in NoTenantContextError followed by a Hint: line.
Cause and fix depend on the hint:
| Hint starts with | Cause | Fix |
|---|---|---|
Establish an RLS context before the query |
STRICT_MODE=True and a query ran with no context |
Wrap the code in tenant_context() / admin_context(), use .for_user(), or run it under the middleware. If the model is meant to be read without a context (an auth backend under bypass_flag()), list it in STRICT_MODE_EXCLUDE, or skip the guard for one query with rls_skip_strict(), as the hint says |
Assign the user to a tenant (set rls_tenant_id) |
A non-admin TenantUser has rls_tenant_id=None: a createsuperuser account, a user not yet attached to a tenant |
Give the user a tenant, or set is_tenant_admin=True. In a request, a user who belongs to several tenants (MultiTenantUser) may instead select one: see Users in Several Tenants |
Pass a concrete tenant primary key |
tenant_context(None) was called |
Pass a real tenant PK, or use admin_context() for cross-tenant access |
For the first hint, the full text continues: If Order is meant to be read without a
context, list 'app.Order' in RLS_TENANTS['STRICT_MODE_EXCLUDE'], or call
rls_skip_strict() on a single query that is meant to run without one (RLS still filters
it at the database); to disable this guard for every model, set STRICT_MODE=False in
RLS_TENANTS. Both STRICT_MODE_EXCLUDE (a whole model) and rls_skip_strict() (one query) remove only this
check; the query still returns just the rows the database policy allows without a context.
With STRICT_MODE=False, the same mistake gives an empty result instead of an error. Set
AUDIT_NO_CONTEXT_QUERIES = True to log a context.denied record (source queryset,
reason no_context, rls_model) for each such query; see
Audit Logging.
The first hint also appears when a Celery task with rls_require_context=True
arrives without propagated context. Enqueue it inside a tenant or admin context;
opening a context in the task body is too late because the guard runs before the body.
See Requiring a context.
See: Strict Mode,
User Integration,
tenant_context.
RuntimeError: Cannot use SET LOCAL for '...' outside a transaction¶
Symptom: The error above, raised from set_guc() when entering tenant_context(),
admin_context() or a testing helper.
Cause: USE_LOCAL_SET=True makes a GUC write a SET LOCAL, which PostgreSQL only
accepts inside a transaction. Since 1.4.0 a context manager entered outside
transaction.atomic() on an alias in RLS_TENANTS["DATABASES"] writes nothing (since 1.5.0
inside it too), and the GUC affinity guard applies the context to each statement, so the
error is left for:
- a context entered on an alias that is not in
RLS_TENANTS["DATABASES"](the guard leaves such an alias alone); - a direct
set_guc(),rls_context()orbypass_flag()call withis_local=True.
Before 1.4.0 it also happened on every alias for scripts, tasks and tests that opened the context by hand (the middleware has opened its own transaction since 1.3.2).
Fix: Add the alias to RLS_TENANTS["DATABASES"], or wrap the block in
transaction.atomic(using=alias):
Or keep the default USE_LOCAL_SET=False unless you run behind an external pooler in
transaction mode (PgBouncer). On 1.3.x and earlier, wrap every such block in
transaction.atomic().
See: Connection Pooling, Testing.
RLSConfigurationError: USE_LOCAL_SET=True requires exception-handling middleware ... before RLSTenantMiddleware¶
Symptom: Django fails at startup with this RLSConfigurationError (or the sibling
message about a middleware factory).
Cause: With USE_LOCAL_SET=True under WSGI the middleware owns the request transaction
and rolls it back from a process_exception hook. Django runs those hooks in reverse order,
so an exception handler listed after RLSTenantMiddleware could return a response, skip
the rollback and commit partial writes. The check refuses the unsafe order. It is skipped
for an instance on the middleware's async path (since 1.4.0), which has no request
transaction; under ASGI with a synchronous-only middleware listed after
RLSTenantMiddleware, Django runs it synchronously and the check applies.
Fix: Move every middleware that defines process_exception, and every function-based
middleware factory, above RLSTenantMiddleware in MIDDLEWARE.
See: Transaction Per Request.
RLSConfigurationError: RLSTenantMiddleware cannot use RLS_TENANTS['USE_LOCAL_SET'] = True under ASGI¶
Symptom: On 1.3.4 and earlier, every authenticated request failed with this error on an ASGI server (Daphne, Uvicorn, Hypercorn). The error no longer exists.
Cause: The async middleware path cannot hold a synchronous transaction.atomic() open
across the awaited downstream chain, so there was no transaction for SET LOCAL to live
in.
Fix: Upgrade to 1.4.0 or later. On the middleware's async path it now opens no
transaction and writes no GUC; the GUC affinity guard applies the request's context to each
statement, transaction-locally. Note that a WSGI request is atomic in this mode and a
request on the async path is not. On 1.3.4 and earlier, serve the application through WSGI,
or set USE_LOCAL_SET=False.
See: USE_LOCAL_SET and ASGI.
RLSConfigurationError: async_tenant_context() and async_admin_context() cannot use RLS_TENANTS['USE_LOCAL_SET'] = True on alias '...' here¶
Symptom: async with async_tenant_context(..., using="analytics") or
async_admin_context(using=...) fails on entry with this error, naming the alias. It only
happens for an alias that is not in RLS_TENANTS["DATABASES"].
Cause: SET LOCAL needs transaction.atomic() open on the thread that runs the
database calls, and a coroutine cannot open one. For an alias in
RLS_TENANTS["DATABASES"] the GUC affinity guard applies the context per statement
instead, but it leaves other aliases alone, so the block would otherwise fail inside
SET LOCAL or set the GUC in a transaction your queries do not use.
Fix: Add the alias to RLS_TENANTS["DATABASES"]. If the coroutine is called from
synchronous code, you can instead wrap that call in transaction.atomic(using=alias) and
run the coroutine with async_to_sync; the managers then use SET LOCAL on that alias.
See: USE_LOCAL_SET=True in async code.
A procedure that commits fails with invalid transaction termination¶
Symptom: With USE_LOCAL_SET=True, a CALL of a stored procedure, or a DO block,
that commits or rolls back fails with a database error whose message is invalid
transaction termination, when it runs inside an RLS context in autocommit.
Cause: Outside a transaction, the GUC affinity guard runs each statement under a context
in a transaction of its own (BEGIN, set_config(), the statement, COMMIT), and
PostgreSQL does not let a procedure or DO block end a transaction that its caller
controls. The guard runs VACUUM, CREATE INDEX CONCURRENTLY and similar commands
unwrapped, but it cannot tell which procedures commit.
Fix: Call the procedure outside any RLS context in this mode, or run it with
USE_LOCAL_SET=False.
See: Code Outside a Request Transaction.
RuntimeError: You cannot use ATOMIC_REQUESTS with async views.¶
Symptom: Every request to an async view fails with this error.
Cause: Django does not support ATOMIC_REQUESTS for async def views. It is often
turned on for USE_LOCAL_SET=True, which has not needed it since 1.3.2.
Fix: Turn ATOMIC_REQUESTS off, or exempt the async views with
@transaction.non_atomic_requests (add @transaction.non_atomic_requests(using=alias) for
each other alias that has ATOMIC_REQUESTS). Put the writes of an async view in
transaction.atomic() inside a synchronous function called with sync_to_async().
See: SET LOCAL without async transactions.
SynchronousOnlyOperation: You cannot call this from an async context in async code¶
Symptom: An async def view or task fails with this error when it enters
transaction.atomic(), tenant_context() / admin_context() or rls_bypass() (with the
default USE_LOCAL_SET=False), or a sync assertion helper such as assert_rls_enabled().
Cause: These run database calls on the event-loop thread, which Django forbids. Django has no async transaction API, and the sync context managers write the GUCs directly.
Fix: Use async_tenant_context() / async_admin_context() and the async testing
helpers in coroutines. Put transaction.atomic() and the sync managers inside a synchronous
function called with sync_to_async().
See: Async Code, Async Deployment.
TypeError: @with_rls_context cannot decorate the generator function ...¶
Symptom: Importing a module fails with this error after upgrading to 1.4.0.
Cause: A generator or async generator function is decorated with
@with_rls_context. Its body runs while the caller iterates, after the decorator has
left the context, so it never ran under the decorated user's context. Before 1.4.0 it
silently used whatever context the caller had.
Fix: Remove the decorator and enter tenant_context() / admin_context() (or
async_tenant_context() / async_admin_context()) inside the generator body.
See: What the Decorator Rejects.
TypeError: @with_rls_context: ... returned a coroutine, but it was decorated as a synchronous function¶
Symptom: Calling a decorated function raises this error.
Cause: The function is async, but the decorator could not tell. It is either an object
with async def __call__, or an async def that a decorator below @with_rls_context
turned into a plain function. The sync wrapper would have left the context before the
coroutine ran.
Fix: Put @with_rls_context directly on the async def, as the innermost decorator,
or enter async_tenant_context() / async_admin_context() inside the coroutine.
See: What the Decorator Rejects.
ValueError: ... Token was created in a different Context under ASGI¶
Symptom: Authenticated requests fail with a 500 and a ValueError from
ContextVar.reset() in the middleware's response or exception hook when running under
ASGI. A view's own exception is masked by the same error.
Cause: Bug #58, fixed in
1.3.4. Django runs each hook of a sync middleware through its own sync_to_async thread
hop, so the token created in process_request was reset in a different
contextvars.Context.
Fix: Upgrade to 1.3.4 or later; the middleware now clears the context when a token
cannot be reset. Since 1.4.0 it serves ASGI requests on its own async path, which keeps
the tokens in one context. On 1.3.3 and earlier, run the middleware under WSGI, or leave it out of
MIDDLEWARE and set the context explicitly in a synchronous view (tenant_context() /
.for_user()). For an async view on 1.4.0 or later, use async_tenant_context() /
async_admin_context(). On older versions, put both the context and query evaluation in
one synchronous function called with sync_to_async(..., thread_sensitive=True).
See: Running under ASGI, Using Without Middleware, Async Code.
RuntimeError: ... Token ... has already been used once¶
Symptom: An exception in a view (for example, an admin PermissionDenied) becomes
a 500 response because middleware cleanup tries to reset the same token twice.
Cause: Before 1.3.1, both the exception and response hooks reset the context tokens. This is separate from the ASGI "different Context" error above.
Fix: Upgrade to 1.3.1 or later.
See: 1.3.1 fixes.
ImportError: cannot import name 'NoTenantContextError' from 'django_rls_tenants'¶
Symptom: The application fails to import after upgrading from 1.2.0, or an old example checkout fails at startup.
Cause: The 1.2.1 public API cleanup removed exception re-exports from the package root. Older example code still used that import until the 1.3.1 fix (#72).
Fix: Import exceptions from their canonical module:
Update the example checkout if it still uses the old import.
See: 1.2.1 upgrade steps, Exceptions.
AttributeError: 'User' object has no attribute 'is_tenant_admin'¶
Symptom: Every authenticated request fails with this AttributeError (or the same
for rls_tenant_id), logged as Failed to set RLS GUC variables.
Cause: RLSTenantMiddleware reads the two TenantUser protocol properties from
request.user, and your user model does not define them.
Fix: Add is_tenant_admin and rls_tenant_id to the user model (a property on
AbstractUser is enough).
See: User Integration.
A form or serializer says ... instance with id 5 is not a valid choice (or Invalid pk "5" - object does not exist) for a row that exists¶
Symptom: full_clean(), a ModelForm, the admin or a DRF serializer rejects a foreign
key value, although SELECT as a superuser shows the row.
Cause: The row belongs to another tenant than the object being validated, or it has no
tenant (NULL). validate_cross_tenant_fks() (called by RLSProtectedModel.clean_fields()
and RLSTenantSerializerMixin.validate()) compares the target's tenant with the object's
own tenant field and reports a mismatch with the message of a missing row, so the error
does not reveal that the id exists in another tenant. When an admin edits a row of tenant
A, the foreign key must point at tenant A's rows too, whatever tenant the admin has
selected.
Fix: Use a target of the object's tenant. If the object has the wrong tenant, fix its
tenant field first. A foreign key that is meant to cross tenants should point at a model
without an RLSConstraint (shared data). Otherwise skip it in the model's clean_fields()
override: super().clean_fields({*(exclude or ()), "customer"}) (but Django then skips
that field's own validation too).
See: Cross-Tenant Foreign Keys.
RLSConfigurationError: RLS_TENANTS['TENANT_MODEL'] is required¶
Symptom: The error is raised the first time anything touches the configuration, often while importing models.
Cause: The RLS_TENANTS dict in settings has no TENANT_MODEL key; it is the only
required setting.
Fix: RLS_TENANTS = {"TENANT_MODEL": "myapp.Tenant"}.
See: TENANT_MODEL.
migrate fails with must be owner of table or permission denied¶
Symptom: The migration that adds the RLSConstraint fails inside
ALTER TABLE ... ENABLE ROW LEVEL SECURITY or CREATE POLICY.
Cause: Those statements require ownership of the table, and the role running
migrate does not own it (typically because a different role created the schema).
Fix: Run migrations as the table owner, and keep the application role a plain
NOSUPERUSER NOBYPASSRLS role. Two roles, two jobs. The owner is not a superuser, so
E001 does not apply to it. If the
migration job has to use a superuser and DEBUG is False, silence E001 in that job's
settings only (see E001), or pass --skip-checks to migrate, which skips every system
check.
See: PostgreSQL Setup.
Admin returns 403 or NoTenantContextError for a createsuperuser account¶
Symptom: You created an account with manage.py createsuperuser, logged into the
admin, and got PermissionDenied (403) on every RLSTenantModelAdmin page, or
NoTenantContextError on every request when the middleware is installed.
Cause: The account has tenant=None and is_tenant_admin=False, which is neither a
scoped user nor a cross-tenant admin. Django's is_superuser means nothing to RLS.
Fix: Set is_tenant_admin=True on the account, or assign it a tenant:
python manage.py shell -c \
"from accounts.models import User; User.objects.filter(username='me').update(is_tenant_admin=True)"
See: Admin: Fail-closed, example project README.
A request answers 403 "The selected tenant is not one you can access."¶
Symptom: A user who belongs to several tenants gets a 403 (under DRF a JSON detail)
from a page or an API call that used to work.
Cause: The request selected a tenant that is not rls_tenant_id or one of the user's
rls_tenant_ids: the X-Tenant header (RLS_TENANTS["TENANT_HEADER"]) named a tenant the
user may not select, a malformed or empty value, or a tenant that does not exist; or the
session selection from switch_tenant() is stale because a membership was revoked. The
message is the same for all of them on purpose. The context.denied audit record with the
reason tenant_not_selectable holds the value (rls_selected) and its source (rls_via).
Fix: Send a tenant from rls_tenant_ids, or none. A stale session selection is removed
when it is refused, so the user's next request runs in rls_tenant_id. If the tenant should
be selectable, check what rls_tenant_ids returns for the user. See Users in Several Tenants.
RemovedInDjango2028Warning: Calling select_related() with no arguments is deprecated¶
Symptom: On Django 6.1 or newer, a test run or the warnings log shows this deprecation for a queryset of an RLS-protected model.
Cause: Django 6.1 deprecates select_related() with no arguments ("follow every
non-null foreign key"); Django 2028 will raise TypeError for it. It is the calling
code that is deprecated, not the library: the warning names the line that made the
call. Releases before 1.4.0 attributed it to django_rls_tenants/tenants/managers.py
instead.
Fix: Name the relations to fetch, e.g. Order.objects.select_related("customer");
named relations also get the tenant filter on the joined RLS-protected tables. Or switch
the queryset to Django 6.1's fetch_mode(FETCH_PEERS), which loads related objects on
first access. For the admin, replace list_select_related = True with a list of fields
(Django warns about that setting itself).
See: Supported versions.
System check warnings¶
python manage.py check prints these; E001 is an error, the rest are warnings. IDs are
never renumbered; W003 and W007 are retired (see below). The full table is in
Configuration: System Checks.
W005: the connection role is a superuser or has BYPASSRLS¶
Symptom: (django_rls_tenants.W005) The connection role 'postgres' of
DATABASES['default'] is a PostgreSQL superuser. ... Often seen on a fresh Docker
database, or when tests asserting isolation (assert_rls_blocks_without_context) fail
because every query returns all rows.
Cause: PostgreSQL skips every RLS policy for superusers and BYPASSRLS roles, even
with FORCE ROW LEVEL SECURITY. The official postgres image's default user is a
superuser. The check runs on every alias in RLS_TENANTS["DATABASES"] and names the alias.
It is a warning while settings.DEBUG is True; with DEBUG = False the same condition is
reported as E001 instead.
Fix: Create a plain role and connect Django as it. The hint carries a ready-to-run statement:
CREATE ROLE app LOGIN PASSWORD 'secret' NOSUPERUSER NOBYPASSRLS;
GRANT ALL ON ALL TABLES IN SCHEMA public TO app;
-- or, for an existing role:
ALTER ROLE app NOSUPERUSER NOBYPASSRLS;
The example project's docker/init-db.sql does exactly this.
In a test suite, request the enforce_rls pytest fixture: it switches a superuser or
BYPASSRLS connection to a plain role for the test, and assert_rls_enforced() fails when
the connection still bypasses RLS. See
Enforcing RLS in Tests.
See: PostgreSQL Setup, Superuser and BYPASSRLS Roles.
E001: the connection role bypasses RLS with DEBUG off¶
Symptom: python manage.py check fails, and so does every management command that runs
the system checks (migrate, createsuperuser, check_rls, setup_m2m_rls, Django's test
runner):
SystemCheckError: System check identified some issues:
ERRORS:
?: (django_rls_tenants.E001) The connection role 'postgres' of DATABASES['default'] is a
PostgreSQL superuser. ... settings.DEBUG is False, so this is treated as a production
deployment.
Cause: The role of a DATABASES alias in RLS_TENANTS["DATABASES"] is a superuser or
has BYPASSRLS, so PostgreSQL skips every RLS policy, and DEBUG is False. The library
treats that as a production deployment and fails the checks instead of warning. With DEBUG
on, the same condition is W005. Django's test runner sets DEBUG = False before it runs
the checks, so a test database reached as a superuser fails here too. A server that does
not run the system checks (gunicorn, uvicorn) and pytest-django (which never runs them) are
not affected.
Fix: Connect as a NOSUPERUSER NOBYPASSRLS role, as for W005.
Migrations run as the table owner, which is an ordinary role and passes. Only when a
settings module has to use such a role on purpose (a migration job on a superuser, a test
database in CI), record that in that module:
from myproject.settings import * # noqa: F403
SILENCED_SYSTEM_CHECKS = ["django_rls_tenants.E001"]
or read it from the environment in a settings module that only that job uses. For a single
migrate run, manage.py migrate --skip-checks also works, but it skips every check. Never
silence E001 in the settings the application serves requests with: that is the
configuration it exists to catch.
See: Superuser and BYPASSRLS Roles, Testing: Django's test runner.
W013: a database check could not be determined¶
Symptom: (django_rls_tenants.W013) Could not determine whether the connection role
bypasses RLS on DATABASES['default']: the query failed with ProgrammingError: permission
denied for view pg_roles. Do not read the absence of W005 and E001 as a pass. The same
message exists for W011 ("... whether PostgreSQL applies a default for a GUC the RLS
policies read ...").
Cause: The database was reachable, but the check's catalogue query failed: a missing
privilege on a pg_catalog view, a statement timeout, or a failing connection hook. The
check could not run, so a clean result for W005, E001 or W011 proves nothing. Each
alias is checked separately. W013 stays a warning even with DEBUG = False. Inside a
transaction (call_command("check") in a test, for one) the query runs in a savepoint, so
the failure does not abort the transaction around it. (When no
connection can be opened at all, because the database is not provisioned yet or the command
runs without one, the checks are skipped quietly instead, with a DEBUG log line.)
Fix: Fix the cause named in the message (usually grant the connection role access to
the catalogue view, or raise the timeout) and run python manage.py check again.
See: Configuration: System Checks.
W001 / W002: guc_tenant_var or guc_admin_var does not match GUC_PREFIX¶
Symptom: RLSConstraint on Order uses guc_tenant_var='rls.current_tenant' but
GUC_PREFIX derives 'myapp.current_tenant'. Tenant isolation will silently break.
Cause: You changed GUC_PREFIX (or passed custom guc_*_var names to a constraint),
so the middleware sets one variable while the database policy reads another. A tenant
GUC mismatch (W001) can make tenant queries return zero rows; an admin GUC mismatch
(W002) breaks the admin bypass even if tenant queries still work.
Fix: Make them agree: either pass the derived names to the RLSConstraint, or set
GUC_PREFIX back so it derives the names the constraint uses. Policies already in the
database keep their old names until a migration recreates them.
See: GUC_PREFIX.
W004: CONN_MAX_AGE > 0 with USE_LOCAL_SET=False¶
Symptom: DATABASES['default'] has CONN_MAX_AGE=600 with USE_LOCAL_SET=False. This
risks GUC state leaking between requests on persistent connections.
Cause: Session-scoped GUCs survive on a persistent connection; if the response phase that clears them is skipped, the next request on that connection inherits the tenant.
Fix: Set USE_LOCAL_SET=True (the GUCs are scoped to transactions, so they never stay
on a connection), or set CONN_MAX_AGE=0. On the middleware's async path (ASGI),
USE_LOCAL_SET=True opens no request transaction: a WSGI request is atomic in that mode and
a request on the async path is not. Under ASGI, set CONN_MAX_AGE=0: each request runs on a
thread of its own, so persistent connections are not reused there. Use Django's pool or
PgBouncer instead.
See: W004 in Connection Pooling, Async Deployment: Connections and pooling.
W010: Django's connection pool without a reset callback¶
Symptom: DATABASES['default'] uses Django's connection pool with USE_LOCAL_SET=False
and no pool 'reset' callback. A connection that a request's cleanup did not reach goes back
to the pool still holding that request's RLS GUCs, until its next checkout clears them.
Cause: With OPTIONS["pool"], closing a connection hands it back to the pool, which
keeps its session GUCs. If the request's cleanup did not reach that connection, it sits in
the pool holding that request's tenant (or admin bypass) until its next checkout, where the
connection_created handler clears it. W004 does not cover this: Django rejects a
non-zero CONN_MAX_AGE together with a pool.
Fix: Set the pool's reset callback, so the GUCs are cleared when the connection goes
back:
from django_rls_tenants.pool import reset_rls_gucs
DATABASES = {
"default": {
# ...
"OPTIONS": {"pool": {"reset": reset_rls_gucs}},
},
}
Or set USE_LOCAL_SET=True. Any reset, including your own, silences the warning.
See: Django's Connection Pool, W010 in Connection Pooling.
W011: PostgreSQL applies a default for a policy GUC¶
Symptom: (django_rls_tenants.W011) PostgreSQL sets rls.is_admin = 'true' at the start
of every session for database 'app_db' (DATABASES['default']). The RLS policies read this GUC, so every session
starts with that value in force instead of no RLS context (fail-closed), and
django-rls-tenants never clears it: an admin value bypasses the policies, a tenant value
shows that tenant's rows to everyone.
Cause: A superuser ran ALTER DATABASE app_db SET rls.is_admin = 'true' or
ALTER ROLE app SET ..., often to make a psql session or a migration "just work"
(PostgreSQL 15+ lets only a superuser set such a default for a custom parameter).
PostgreSQL applies it when a session starts, before the middleware or a context manager
runs. The library assumes a new session holds no value for these GUCs, so it never
clears the default: with an admin default every connection bypasses RLS, with a tenant
default everyone sees that tenant's rows. The check reads pg_db_role_setting for the
connected database and for role-level defaults, for every GUC the installed policies read
(bypass flags included), on every alias in RLS_TENANTS["DATABASES"].
Fix: Remove the default with the statement the hint names (ALTER DATABASE app_db RESET
rls.is_admin;, ALTER ROLE app RESET rls.is_admin; or ALTER ROLE app IN DATABASE app_db
RESET rls.is_admin;) and reconnect. Set the RLS context per request or block instead (see
Context Managers); for a one-off maintenance session, use
admin_context(), or SET rls.is_admin = 'true' in that session only.
W012: a unique field or constraint spans all tenants¶
Symptom: billing.Company: (django_rls_tenants.W012) Field 'registration_number'
(unique=True) is unique across all tenants: it does not include the tenant field
'tenant'. PostgreSQL checks unique indexes outside the RLS policies, so a write whose
value exists only in another tenant fails with an IntegrityError, telling the writer
that the value exists there. The same check reports unique_together entries,
UniqueConstraint(fields=...) and UniqueConstraint on expressions that leave the
tenant field out, and a stale RLS_TENANTS["UNIQUE_ACROSS_TENANTS"] entry (...
lists 'registration_numbr', which matches no unique field or constraint of
billing.Company that omits the tenant field 'tenant', or a label that names no
RLS-protected model).
Cause: The unique index covers the whole table, and PostgreSQL enforces it without
applying the RLS policies. Tenant A saving a value that only tenant B holds gets an
IntegrityError, so tenant A learns that tenant B has a company with that registration
number, slug or tag. Django's validate_unique() and ModelForm validation query
through RLS, cannot see tenant B's row and let the save reach the database. After an
upgrade to 1.5.0 this check can report models that were already like this. It walks every
concrete model that has an RLSConstraint; primary keys, proxy models and a tenant field
that is itself unique are not reported.
Fix: Scope the rule to the tenant, with a migration (check that existing rows do not collide first):
class Company(RLSProtectedModel):
registration_number = models.CharField(max_length=50) # was unique=True
class Meta(RLSProtectedModel.Meta):
constraints = [
*RLSProtectedModel.Meta.constraints,
models.UniqueConstraint(
fields=["tenant", "registration_number"],
name="company_registration_number_per_tenant",
),
]
For a constraint on expressions, reference the tenant field in it
(UniqueConstraint(Lower("slug"), "tenant", name=...)). If uniqueness across tenants is
intended, list it, by field name, UniqueConstraint name or tuple of field names:
See: UNIQUE_ACROSS_TENANTS,
Cross-Tenant Unique Constraints.
W014: a protected model has no RLSConstraint¶
Symptom: shop.Invoice: (django_rls_tenants.W014) shop.Invoice inherits from
RLSProtectedModel but has no RLSConstraint in Meta.constraints, so no migration creates
an RLS policy for its table 'shop_invoice': the database does not isolate its rows by
tenant. Without the check, makemigrations writes no policy and check_rls reports
no RLS policies found on a migrated database.
Cause: The policy comes from the RLSConstraint in RLSProtectedModel.Meta.constraints.
A class Meta: that does not inherit RLSProtectedModel.Meta drops it, and so does a
Meta that inherits it but assigns its own constraints = [...], which replaces the
inherited list. The ORM still scopes queries by tenant, but the database does not, so a
raw query or a missing filter reads every tenant's rows. Proxy models share their
concrete model's table and are not reported. After an upgrade to 1.5.0 this check can
report models that were already like this.
Fix: Inherit the Meta, and keep the inherited constraints when you add your own:
class Invoice(RLSProtectedModel):
number = models.CharField(max_length=50)
class Meta(RLSProtectedModel.Meta):
db_table = "shop_invoice"
constraints = [
*RLSProtectedModel.Meta.constraints,
models.CheckConstraint(condition=models.Q(number__gt=""), name="invoice_number"),
]
Then run makemigrations and migrate; check_rls confirms the policy.
See: Meta Class Inheritance.
W015: a STRICT_MODE_EXCLUDE entry exempts nothing¶
Symptom: (django_rls_tenants.W015) RLS_TENANTS['STRICT_MODE_EXCLUDE'] lists
'accounts.Usr', which does not resolve to an installed model. or ... lists
'myapp.Country', which names a model without an RLSConstraint or EXTERNAL_MODELS entry,
which strict mode never guards, so the entry has no effect.
Cause: The label is not "<app_label>.<ModelName>" of an installed model (a typo, an
app missing from INSTALLED_APPS, no dot), or the model has neither an RLSConstraint
nor an EXTERNAL_MODELS entry, so
strict mode never guards it and listing it does nothing. The check runs whether or not
STRICT_MODE is on.
Fix: Correct the label, or remove the entry. A model without an RLSConstraint needs
no entry.
See: STRICT_MODE_EXCLUDE.
W016: an EXTERNAL_MODELS entry cannot be used¶
Added in 1.6.0.
Symptom: One of these, each starting (django_rls_tenants.W016) RLS_TENANTS['EXTERNAL_MODELS']
lists 'library.Book', which ...:
does not resolve to an installed model.names a swapped-out model (replaced by accounts.User), which has no table.names a proxy model, which has no table of its own.names a model with an RLSConstraint, whose migrations create its policy, so the entry has no effect.has no field 'owner'.maps it to 'title', a CharField, not a ForeignKey.maps it to 'owner', a foreign key to auth.User, not to RLS_TENANTS['TENANT_MODEL'] (myapp.Tenant).names the same model as 'library.book', so one of their fields is ignored.
Cause: The setting maps a model label to the name of the model's tenant foreign key,
and the library reads both when it checks the table, validates foreign keys to it and
filters joins into it. The label is not "<app_label>.<ModelName>" of an installed model
(a typo, an app missing from INSTALLED_APPS, no dot), or the model is swapped out (as
auth.User is under a custom AUTH_USER_MODEL: no table) or a proxy (its table is its
concrete model's), or it already carries an RLSConstraint (its migrations create
the policy, so an AddRLSPolicy would find one and change nothing), or the field is
missing, is not a ForeignKey, or points at another model than TENANT_MODEL (the policy
compares that column with the current tenant). An entry in this state has no effect or,
for a wrong field, would make the library filter on the wrong column. A TENANT_MODEL
that does not resolve is left to W008.
Fix: Correct the label or the field name, remove the entry, or list the model the hint names (the concrete model of a proxy; the model that replaces a swapped-out one). Keep one entry per model.
See: EXTERNAL_MODELS,
Tables You Do Not Own.
W017: an extra predicate cannot be built¶
Added in 1.6.0.
Symptom: (django_rls_tenants.W017) The extra predicate SharedWith(through='notes.NoteShare',
fk='note', tenant_fk='shared_with') of notes.Note's RLSConstraint cannot be built: ..., followed
by the reason (for example SharedWith on notes.Note: there is no model 'notes.NoteShare',
or field='partner': notes.Note has no field 'partner'.).
Cause: A predicate in the extra_predicates of an installed model's RLSConstraint
refers to something that does not exist: a SharedWith whose through model does not
resolve, whose fk is not a ForeignKey to the model (or a proxy of it) or whose
tenant_fk is not a ForeignKey, or a TenantField that names no foreign key. migrate
builds the sharing policy from the same predicate and would fail with the same
ValueError. Proxies are skipped; the concrete model is checked.
The check also reports The sharing policy of notes.Note cannot be created: the table name
'...' is 55 characters long. PostgreSQL cuts an identifier to 63 bytes, so from a table name
of 55 bytes on, the sharing policy's name is cut to the tenant policy's, and the CREATE
POLICY that is skipped when a policy of that name exists would never run. migrate refuses
such a table with a ValueError; set a shorter Meta.db_table.
Fix: Correct the predicate in the model's Meta.constraints, then makemigrations.
See: Sharing Rows Across Tenants.
W018: a SharedWith predicate never matches¶
Added in 1.6.0.
Symptom: (django_rls_tenants.W018) SharedWith(through='notes.NoteShare', fk='note',
tenant_fk='shared_with') on notes.Note never matches: the RLS policy of notes.NoteShare filters
its rows on 'tenant', so the tenant in 'shared_with' cannot read the rows shared with it, inside
the sharing policy either. Shared rows never appear in including_shared().
Cause: The sharing policy reads the share model (through) in a subquery, and the share
model's own RLS policy applies there. When it is protected by another field than tenant_fk
(the owner's tenant, say), the tenant a row is shared with cannot see the share row, so the
EXISTS is false.
Fix: Add TenantField("shared_with") (your tenant_fk) to the extra_predicates of the
share model's RLSConstraint and run makemigrations. If a RawPredicate of yours already
makes the share rows visible to that tenant, silence the warning
(SILENCED_SYSTEM_CHECKS = ["django_rls_tenants.W018"]): the check does not recognize it.
See: Sharing Rows Across Tenants.
W019: a predicate's foreign key does not point at the tenant model¶
Added in 1.6.0.
Symptom: (django_rls_tenants.W019) SharedWith(through='notes.NoteShare', fk='note',
tenant_fk='note') on notes.Note compares notes.NoteShare.note with the current tenant, but it
is a foreign key to notes.note, not to RLS_TENANTS['TENANT_MODEL'] (tenants.Tenant): rows
would be shared with whichever tenant has the same key.
Cause: A SharedWith's tenant_fk, or a TenantField's field, names a foreign key to
another model than the tenant model. The policy compares that column with the current
tenant's key, so with sequential integer keys a row whose note, user or other id happens to
equal a tenant's id is readable by that tenant. The library cannot tell the two apart at the
database: only the check does. Skipped while TENANT_MODEL does not resolve (W008).
Fix: Pass the foreign key to the tenant model (tenant_fk="shared_with",
TenantField("partner")) and run makemigrations.
See: Sharing Rows Across Tenants.
W006 / W008 / W009: alias, tenant model or tenant field not found¶
| ID | Message | Cause | Fix |
|---|---|---|---|
| W006 | RLS_TENANTS['DATABASES'] contains 'replca' which is not defined in settings.DATABASES |
Typo or stale alias in RLS_TENANTS["DATABASES"] |
Use an alias that exists in settings.DATABASES |
| W008 | RLS_TENANTS['TENANT_MODEL'] = 'myapp.Tenent' does not resolve to an installed model |
Typo in the path, or the app is missing from INSTALLED_APPS |
Use "<app_label>.<ModelName>" of an installed model |
| W009 | Order has no field 'tenant' referenced by its RLS policy |
The model lacks the FK the policy filters on, usually a custom RLSConstraint(field=...) or TENANT_FK_FIELD naming a field that does not exist |
Add the FK, or point field= / TENANT_FK_FIELD at an existing one |
See: DATABASES,
TENANT_MODEL,
Custom ForeignKey.
W003 or W007 in old output, or in SILENCED_SYSTEM_CHECKS¶
Symptom: Documentation or a settings file from an earlier version mentions
django_rls_tenants.W003 / W007, but manage.py check never prints them.
Cause: Both warned about USE_LOCAL_SET=True without ATOMIC_REQUESTS. W003 was
retired in 1.3.1 because W007 covered all configured aliases. W007 was retired in 1.3.2
when the middleware began opening its own transaction, making the combination valid.
Fix: Nothing. A leftover SILENCED_SYSTEM_CHECKS entry is harmless and can be
removed.
See: Retired checks.
check_rls findings¶
check_rls reports RLS not enabled or no RLS policies found¶
Symptom: Found 1 issue(s): Invoice (myapp_invoice): no RLS policies found, exit
status 1, followed by a How to fix: block with a line for the table.
Cause: One of several, and the How to fix: line (added in 1.5.0) tells them apart
from the migrations on disk and their record on the --database alias: the model defines
its own Meta without inheriting RLSProtectedModel.Meta (system check W014), no
migration adds the RLSConstraint, a migration that adds it is not applied on this
database, RLS was turned off (RLS not enabled), or an applied migration's policy or
table was dropped outside migrations.
Fix: Do what the line says: makemigrations <app> and migrate, or the ALTER TABLE
... ENABLE ROW LEVEL SECURITY statement it prints, or run the DO block that
sqlmigrate <app> <migration> prints. For a model without the constraint, make its Meta
inherit from RLSProtectedModel.Meta first. For a table listed in EXTERNAL_MODELS, the
line says which migration holds its AddRLSPolicy, or prints the AddRLSPolicy(...) to
add to a migration of your own app (see
Tables You Do Not Own).
See: Meta Class Inheritance, W014, check_rls: Failure Output.
check_rls reports an M2M through table with no RLS policies found after migrate¶
Symptom: Project.tags (auto M2M) (myapp_project_tags): no RLS policies found, even
though migrate ran cleanly and the Project table is protected.
Cause: migrate never creates the policy of an auto-created M2M through table. The
library adds its RLSM2MConstraint to the through model at startup, outside the migration
state, so no migration holds it. The policy comes from an AddM2MRLSPolicy migration, or
from setup_m2m_rls on one database.
Fix: Run python manage.py check_rls --generate, review the migration it writes, then
migrate. A table it skips (an app without migrations, a field no migration creates yet)
gets a warning that names the reason. setup_m2m_rls applies the policies to the
database directly, but a database built from the migrations has none.
See: Generating M2M policy migrations, Many-to-Many Relationships.
check_rls lists M2M through tables: you did not create¶
Symptom: The output has a section with tables such as accounts_user_groups and
accounts_user_user_permissions, and the table count is higher than the number of
models you protected.
Cause: An RLS-protected model has auto-created M2M through tables, and they are
protected too. The usual surprise is a User that inherits from RLSProtectedModel,
which pulls in the groups and user_permissions tables from AbstractUser.
Fix: This is correct behaviour. If you did not mean to protect User, make it a plain
AbstractUser with a nullable tenant FK; if you did, read the opt-in notes.
See: Protecting the User Model, Automatic Protection.
Behaviour that was a bug (upgrade to fix)¶
All of these are fixed in 1.3.2; the fix is pip install -U django-rls-tenants.
Details are in the changelog and the
upgrade notes.
| Symptom | Issue |
|---|---|
select_related("a__b") silently drops rows whose chain has a NULL at any hop |
#79 |
override_settings(RLS_TENANTS=...) or the pytest settings fixture has no effect |
#100 |
InternalError: current transaction is aborted hides the real IntegrityError when a block exits |
#102 |
An inner admin_context() keeps its scope after the block under USE_LOCAL_SET=True; raw SQL sees every tenant |
#94 |
RLSTenantMiddleware crashes with SET LOCAL ... outside a transaction on every request with USE_LOCAL_SET=True |
#95 |
If the symptom persists on 1.3.2 or later, open an issue with the version and a minimal reproduction.
Integrations¶
A Celery task sees no tenant, or the wrong tenant¶
Symptom: The task body returns zero rows, or acts on a different tenant than the request that enqueued it.
Cause: One of three:
- The task is a plain
@shared_task, so no context is captured on enqueue. - The task was enqueued outside any context (a scheduled beat task, a shell), so there was nothing to propagate; it runs unscoped and fail-closed.
- A plain
@shared_tasksits in the middle of a chain, which breaks propagation for itself and every later step.
Fix: Use @rls_task (or the RLSTask base class) for every step, and set
rls_require_context = True on tasks that must never run tenant-blind so they raise
instead of returning nothing. Enqueue those tasks inside an active context. Scheduled
tasks that pick their tenant inside the body must leave rls_require_context=False,
since the guard runs before the body can establish context.
See: Celery Tasks, Requiring a context.
Tenant context leaks between requests behind PgBouncer¶
Symptom: With pool_mode = transaction, a request occasionally sees another user's
tenant, or an unauthenticated request sees rows.
Cause: The default USE_LOCAL_SET=False sets session-scoped GUCs with
set_config(). In transaction pooling the server connection is handed to another client
as soon as the transaction ends, carrying the GUCs with it.
If USE_LOCAL_SET=True is already set, something else wrote a policy GUC for the session
through the same pool (a bypass flag without is_local=True, set_guc(), another client
with USE_LOCAL_SET=False, a psql session). That value stays on its server connection,
and every client PgBouncer gives the connection to runs with it.
Fix: Set USE_LOCAL_SET=True. The GUCs become transaction-local and die with their
transaction: the per-request transaction the middleware opens under WSGI, your own
transaction.atomic(), or, on the middleware's async path (ASGI) and for code outside a
transaction, the transaction the GUC affinity guard opens for each statement (since 1.4.0;
before that, this mode did not work under ASGI, and scripts had to open their own
transaction before setting context). Set a server_reset_query that clears the policy GUCs,
with server_reset_query_always = 1, so a session-level write by anyone ends with its
transaction, and run RECONNECT on PgBouncer's admin console to drop the server
connections that already hold one.
See: Troubleshooting GUC Leaks, PgBouncer: Transaction Mode, Connection-Level State, Async Deployment: Connections and pooling.
Zero rows, or new row violates row-level security policy, behind pgpool-II¶
Symptom: Behind pgpool-II with load_balance_mode = on, reads return no rows (with
USE_LOCAL_SET=False, or with disable_load_balance_on_write = 'off'), or writes fail
with new row violates row-level security policy (with set_config listed in
read_only_function_list). The same code works on a direct connection.
Cause: pgpool sends each statement to the primary or to a standby, and each backend
session has its own GUCs. It sends the library's SELECT set_config(...) to the primary
only, since set_config() is a volatile function, and plain reads to the standby, so the
query runs where the GUCs were never set (fail-closed).
Fix: Set load_balance_mode = off for the application, and read from a standby through
a Django alias of its own, routed with a database router and listed in
RLS_TENANTS["DATABASES"]. With USE_LOCAL_SET=True and pgpool's defaults load balancing
works without errors, but every statement run under a context stays on the primary.
See: pgpool-II: Load Balancing.
An async view committed some of its writes¶
Symptom: With USE_LOCAL_SET=True under ASGI, a view that failed, or whose client
disconnected, left its first writes in the database and not the rest.
Cause: On the middleware's async path there is no request transaction. Each statement in autocommit runs in a transaction of its own, which the GUC affinity guard opens to hold the context's GUCs, and commits on its own. Django also cancels an async view when the client disconnects. Under WSGI the middleware's request transaction would have rolled the writes back together.
Fix: Put writes that belong together in transaction.atomic() inside a synchronous
function and call it with sync_to_async(). It also saves three round trips per statement.
See: SET LOCAL without async transactions.
too many clients already or pool timeouts under ASGI¶
Symptom: Under load on an ASGI server, PostgreSQL refuses connections with
FATAL: sorry, too many clients already, or requests fail waiting for a connection from
Django's pool.
Cause: Under ASGI each request in flight runs on a thread of its own and holds its own
connection, and nothing caps the number of requests. Threads of the event loop's default
executor, which run ASGI error pages and thread_sensitive=False work, keep a connection
each. With CONN_MAX_AGE > 0 the connections are never reused, because the request's
thread ends with the request.
Fix: Keep CONN_MAX_AGE = 0. Cap the requests in flight at the server, or use
Django's pool and size its max_size for one process's concurrent requests plus its
executor threads. Size max_connections for every worker process, or put PgBouncer in
front (with USE_LOCAL_SET=True in transaction mode).
See: Async Deployment: ASGI servers.
Raw SQL returns rows from every tenant for a plain manager, or none at all¶
Symptom: cursor.execute("SELECT ... FROM myapp_order") returns nothing in a view,
or everything in an admin context, while the ORM result looked scoped.
Cause: Raw SQL is still subject to the policy, so it needs the same context as the
ORM (nothing → zero rows; admin_context() → all rows). Only the ORM's
TenantQuerySet adds the extra WHERE tenant_id = X; raw SQL does not.
Fix: Run it inside a context, and add safe_tenant_sql() to the WHERE clause to get
an explicit predicate that preserves admin bypass. For an indexable tenant match, use
safe_tenant_sql(include_admin=False) inside tenant_context(tenant_id). The default
form's OR expression does not provide a tenant index condition; it also continues to
return every tenant in admin_context().
See: Raw SQL.
Queries are slow: the policy is a Filter, never an Index Cond¶
Symptom: EXPLAIN shows the tenant predicate from the policy as a post-scan
Filter and a sequential scan on a large table.
Cause: The policy's CASE WHEN <admin> ... END shape cannot be used as an index
condition. Index usage comes from the ORM's WHERE tenant_id = X, which only exists
inside a tenant context on a TenantQuerySet.
Fix: Query through the model's manager inside tenant_context(), and give hot tables
a composite index with the tenant column first. For raw SQL, use
safe_tenant_sql(include_admin=False).
See: Performance & Indexing, Performance checklist.
A set_config(...) query you did not run¶
Symptom: A query log (assertNumQueries, CaptureQueriesContext, the Django Debug
Toolbar) shows SELECT set_config('rls.is_admin', ...), set_config('rls.current_tenant', ...)
before one of your statements. With USE_LOCAL_SET=True it may also show BEGIN before it
and COMMIT after it.
Cause: The GUC affinity guard ran it because the connection did not hold the active context's GUCs, or because it could not tell what they were. Code that stays on one thread and connection inside a context runs none. Typical reasons:
- the code ran on another thread, or in a sibling task with a different context;
- a rollback undid GUCs the guard had applied inside the transaction or savepoint;
- a transaction ended through the driver connection with GUCs different from before it
(the guard cannot tell a commit from a rollback there), a savepoint it did not see set
was rolled back, or a
RESET ALL/DISCARD ALLran; - an earlier test left a context open;
USE_LOCAL_SET=True: a statement run in autocommit while a context is active gets a transaction of its own, so the log showsBEGIN, theset_config(..., true)statement andCOMMITaround it. Django logs the guard'sBEGINandCOMMITas it logs its own, so they count: the statement counts as four queries, andconnection.querieslists it last, because Django logs it when it finishes. For a transaction opened inside a context the guard sends oneset_config()per transaction; a context entered insidetransaction.atomic()is applied the same way (1.5.0+: oneset_config()before its first statement, and one when it exits with the transaction still open, which puts the enclosing context's values back or clears them; one clear first after an earlier context has ended on that connection);USE_LOCAL_SET=True: the next statement run outside any context, in autocommit or in a transaction, after a context has ended (or after a statement run under a context's values on that connection) sends oneset_config()that clears what the session may hold for itself (code inside the context may have written the GUCs for the session).
At DEBUG level the django_rls_tenants logger names every write the guard makes except
the set_config() inside its own BEGIN ... COMMIT: GUC affinity guard: alias
'default' held ...; applied the active context's .... That one, which wraps a statement run
under a context, shows only in the query log and on the django.db.backends logger, with
the BEGIN and COMMIT.
Fix: Nothing is wrong. In a test that counts queries, run a statement before the counted
block (or count inside the context) so the connection already holds the context's GUCs, and
end every context that a test opens by hand, for example process_request() without
process_response(). With USE_LOCAL_SET=True, a statement under a context outside a
transaction (in a TransactionTestCase, say) carries BEGIN, a set_config() and
COMMIT of its own: add three per statement to the expected count, or run the counted
block in one transaction.atomic() inside the context (the count then includes Django's own
BEGIN and COMMIT, plus one set_config(); a block nested in an enclosing transaction,
as in a TestCase, shows only SAVEPOINT, your statements and RELEASE SAVEPOINT, because
the set_config() falls on the enclosing transaction's first statement). Inside a test
transaction (TestCase, django_db), a context entered in the test sends one
set_config() before its first statement and one when it exits (before 1.5.0 it wrote its
own GUCs: two current_setting() reads and two SET LOCAL statements on entry, two on
exit); run a statement under the context before the counted block so the count stays
flat.
See: GUC Affinity Guard.