pypi django-tenants 4.1.3

one hour ago

What's changed

Much faster schema cloning on databases with many schemas (#1293)

Two parts of clone_schema() read the catalogue of every schema in the database, not just the one being cloned, so each clone got slower as tenants were added. On a database with 38 tenant schemas, cloning a 587-table schema took 21 s:

  • The sequence lookup joined pg_depend on objid alone. That join can't use the table's indexes, so each of the 468 runs scanned every schema's dependencies (10.8 s in total). Both joins now name classid and refclassid, and the query takes 64 ms.
  • pg_get_tabledef() checked whether each column name needed quoting with a self-join of information_schema.columns, which reads every schema's columns (7.8 s in total). It now writes every name with quote_ident(), so it does no lookup.

The same clone now takes 2.3 s. Comparing the clones made before and after the change, the schema dumps, sequence values and row counts all match.

Names that need quoting

Some table, column and constraint names used to make clone_schema() fail. Tables using these names now clone:

  • a mixed-case column in a schema whose name needs quoting, such as s8-hyphen;
  • names with a hyphen, a leading digit or a double quote, and keywords such as left;
  • constraint names written inside CREATE TABLE, which were not quoted;
  • the rows of a table whose name needs quoting and that has a user-defined-type column (get_insert_stmt_ddl() failed);
  • the rows of a mixed-case table with a GENERATED ALWAYS identity column (cannot insert a non-DEFAULT value into column "id").

Constraint names are now quoted, so a mixed-case CHECK constraint keeps its case in the clone (Chk_A used to come out as chk_a). A partitioned table with a mixed-case CHECK constraint, which used to fail at ATTACH PARTITION, now clones.

Full changelog: v4.1.2...v4.1.3

Don't miss a new django-tenants release

NewReleases is sending notifications on new releases.