Drop the tenant schemas one transaction at a time

The cleanup I added dropped every tenant schema in a single transaction, and a
hundred runs' worth of them exhausts the lock table: the session teardown failed
with 'out of shared memory ... max_locks_per_transaction', which reads like a
PostgreSQL defect and is not one. It is the same constraint the 0005 rollback
documents and I wrote there before hitting it here.

Committing after each drop releases the locks, and the teardown now leaves zero
schemas behind, so a run does not make the next one slower.
This commit is contained in:
umbra audio
2026-10-03 13:59:08 -07:00
parent 9a84a9d227
commit 0179355fe7

View File

@@ -820,6 +820,12 @@ def _drop_tenant_schemas(dsn: str) -> None:
schemas themselves would survive, and a hundred runs would leave a hundred
schemas for organizations that no longer exist. Only reached when the guard
above established the database held nothing to begin with.
One schema per transaction, and that is load-bearing rather than tidy. Dropping
several hundred schemas in a single transaction exhausts the lock table and
fails with "out of shared memory ... max_locks_per_transaction" - a message
that reads like a PostgreSQL defect and is not one. It is the same constraint
the 0005 rollback documents, learned here first.
"""
pool = ConnectionPool(dsn, min_size=1, max_size=2, open=True, kwargs={"row_factory": dict_row})
@@ -830,6 +836,7 @@ def _drop_tenant_schemas(dsn: str) -> None:
).fetchall()
for schema in schemas:
connection.execute(f'DROP SCHEMA IF EXISTS "{schema["nspname"]}" CASCADE')
connection.commit()
finally:
pool.close()