What's changed
Faster schema cloning on databases with many tenants (#1288)
After copying each table, clone_schema() renames the copied indexes back to their source's names by pairing the source and destination indexes of that table. On a database with many schemas sharing table names, Postgres could plan that pairing as a join across every schema's indexes, running pg_get_indexdef() on every pair before filtering by schema. The cost grows with the square of the number of schemas.
This tends to happen with stale catalogue statistics, which are normal right after heavy DDL such as creating tenants or a test run that clones schemas. A minor Postgres release can tip the planner into the slow plan. In one project's test suite, the pairing query went from under 1 ms to as much as 300 ms per call after moving from Postgres 16.15 to 16.16, and the suite from about 3 to 8 minutes.
Each side of the pairing is now narrowed to its own schema and table before the join, in a subquery the planner cannot flatten. The clone's results are unchanged.
Thanks to @mtthwsar for the report and the fix.
Full changelog: v4.1.0...v4.1.1