Skip to content

perf(postgres): batch DROP statements in clearDatabase() - #12164

Merged
pkuczynski merged 3 commits into
masterfrom
perf/batch-postgres-drop-statements
Mar 11, 2026
Merged

pkuczynski merged 3 commits into
masterfrom
perf/batch-postgres-drop-statements

Conversation

@pkuczynski

Copy link
Copy Markdown
Member

Consolidate individual DROP VIEW/TABLE/TYPE queries into single batched statements, reducing N+1 round trips to 1 per object type. Same optimization as #12159 for CockroachDB.

  • Views: single DROP VIEW IF EXISTS a, b, c CASCADE
  • Materialized views: single DROP MATERIALIZED VIEW IF EXISTS a, b, c CASCADE
  • Tables: single DROP TABLE IF EXISTS a, b, c CASCADE
  • Enum types: single DROP TYPE IF EXISTS a, b, c CASCADE

Also switches enum name quoting from raw string concatenation to quote_ident() for correctness with special characters.

Consolidate individual DROP VIEW/TABLE/TYPE queries into single
batched statements, reducing N+1 round trips to 1 per object type.
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Review Summary by Qodo

Batch DROP statements in PostgreSQL clearDatabase()

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Batch DROP statements for views, materialized views, tables, and enum types
• Reduces N+1 database round trips to single query per object type
• Switches enum name quoting to quote_ident() for special character handling
• Improves clearDatabase() performance by consolidating individual DROP queries
Diagram
flowchart LR
  A["Individual DROP queries<br/>N+1 round trips"] -->|Consolidate| B["Batched DROP statements<br/>1 round trip per type"]
  C["Raw string concatenation<br/>for enum names"] -->|Switch to| D["quote_ident() function<br/>handles special chars"]
  B --> E["Improved performance"]
  D --> E
Loading

Grey Divider

File Changes

1. src/driver/postgres/PostgresQueryRunner.ts ✨ Enhancement +32/-27

Batch DROP statements and improve enum quoting

• Refactored clearDatabase() to batch DROP VIEW, DROP MATERIALIZED VIEW, and DROP TABLE statements
 into single queries
• Changed from generating individual DROP queries to collecting object names and executing single
 batched statements
• Updated dropEnumTypes() method to batch DROP TYPE statements using quote_ident() for proper
 name quoting
• Added conditional checks to only execute DROP statements when objects exist

src/driver/postgres/PostgresQueryRunner.ts


Grey Divider

Qodo Logo

@pkg-pr-new

pkg-pr-new Bot commented Mar 9, 2026 •

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm/typeorm@12164
    

commit: 4292735

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Mar 9, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Unescaped schema IN list 🐞 Bug ⛨ Security
Description
PostgresQueryRunner.clearDatabase() builds schemaNamesString by wrapping schema names in single
quotes without escaping and interpolates it into SQL; schema names containing a single quote will
produce invalid SQL (and can be exploited as SQL injection in environments where schema names are
user-controlled). This affects the catalog queries used to assemble the batched DROP statements and
dropEnumTypes().
Code

src/driver/postgres/PostgresQueryRunner.ts[1]

       if (!isAnotherTransactionActive) await this.startTransaction()
Evidence
clearDatabase() constructs an IN-list by concatenating raw schema strings into single-quoted
literals, then injects that string directly into SQL executed by query() (which forwards the SQL to
the driver without parameterization). Any schema name containing a single quote (valid when schema
identifiers are double-quoted in Postgres) will break the generated SQL, and if the schema name is
attacker-controlled it becomes an injection primitive.

src/driver/postgres/PostgresQueryRunner.ts[3369-3374]
src/driver/postgres/PostgresQueryRunner.ts[3380-3383]
src/driver/postgres/PostgresQueryRunner.ts[4546-4551]
src/driver/postgres/PostgresQueryRunner.ts[244-261]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`clearDatabase()` builds `schemaNamesString` by concatenating schema names into single-quoted SQL literals without escaping. This breaks when a schema name contains a single quote (valid in Postgres when the schema was created with double-quotes) and is also a SQL injection primitive if schema names are user-controlled.
### Issue Context
The PR batches DROP statements but still relies on `schemaNamesString` inside `WHERE ... IN (${schemaNamesString})` for views/materialized views/tables and inside `dropEnumTypes()`.
### Fix Focus Areas
- src/driver/postgres/PostgresQueryRunner.ts[3369-3420]
- src/driver/postgres/PostgresQueryRunner.ts[4545-4557]
### Implementation notes
- Replace `schemaNamesString` with a `schemaNames: string[]`.
- Resolve `current_schema()` to an actual string once (e.g., `await this.getCurrentSchema()`) and include it in the array.
- Change catalog queries to use `= ANY($1)` and pass `[schemaNames]` as parameters.
- Update `dropEnumTypes` signature to accept `schemaNames: string[]` and use the same parameterization pattern.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Grey Divider

Previous review results

Review updated until commit 4292735

Results up to commit 9bc9176


🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider
Action required
1. Unescaped schema IN list 🐞 Bug ⛨ Security
Description
PostgresQueryRunner.clearDatabase() builds schemaNamesString by wrapping schema names in single
quotes without escaping and interpolates it into SQL; schema names containing a single quote will
produce invalid SQL (and can be exploited as SQL injection in environments where schema names are
user-controlled). This affects the catalog queries used to assemble the batched DROP statements and
dropEnumTypes().
Code

↗ src/driver/postgres/PostgresQueryRunner.ts

        if (!isAnotherTransactionActive) await this.startTransaction()
Evidence
clearDatabase() constructs an IN-list by concatenating raw schema strings into single-quoted
literals, then injects that string directly into SQL executed by query() (which forwards the SQL to
the driver without parameterization). Any schema name containing a single quote (valid when schema
identifiers are double-quoted in Postgres) will break the generated SQL, and if the schema name is
attacker-controlled it becomes an injection primitive.

src/driver/postgres/PostgresQueryRunner.ts[3369-3374]
src/driver/postgres/PostgresQueryRunner.ts[3380-3383]
src/driver/postgres/PostgresQueryRunner.ts[4546-4551]
src/driver/postgres/PostgresQueryRunner.ts[244-261]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`clearDatabase()` builds `schemaNamesString` by concatenating schema names into single-quoted SQL literals without escaping. This breaks when a schema name contains a single quote (valid in Postgres when the schema was created with double-quotes) and is also a SQL injection primitive if schema names are user-controlled.

### Issue Context
The PR batches DROP statements but still relies on `schemaNamesString` inside `WHERE ... IN (${schemaNamesString})` for views/materialized views/tables and inside `dropEnumTypes()`.

### Fix Focus Areas
- src/driver/postgres/PostgresQueryRunner.ts[3369-3420]
- src/driver/postgres/PostgresQueryRunner.ts[4545-4557]

### Implementation notes
- Replace `schemaNamesString` with a `schemaNames: string[]`.
- Resolve `current_schema()` to an actual string once (e.g., `await this.getCurrentSchema()`) and include it in the array.
- Change catalog queries to use `= ANY($1)` and pass `[schemaNames]` as parameters.
- Update `dropEnumTypes` signature to accept `schemaNames: string[]` and use the same parameterization pattern.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider Grey Divider
Results up to commit f67e7de


🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review Grey Divider Grey Divider
Qodo Logo

@coveralls

coveralls commented Mar 9, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 74.899% (+0.003%) from 74.896%
when pulling 4292735 on perf/batch-postgres-drop-statements
into 3b4313e on master.

@pkuczynski
pkuczynski added this pull request to the merge queue Mar 9, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Mar 9, 2026
@pkuczynski
pkuczynski added this pull request to the merge queue Mar 9, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Mar 10, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Mar 11, 2026 •

Copy link
Copy Markdown

Deploying typeorm with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9bc9176
Status: ✅  Deploy successful!
Preview URL: https://8c9e5497.typeorm.pages.dev
Branch Preview URL: https://perf-batch-postgres-drop-sta.typeorm.pages.dev

View logs

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 9bc9176

@pkuczynski
pkuczynski enabled auto-merge (squash) March 11, 2026 13:41
@sonarqubecloud

Copy link
Copy Markdown

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 4292735

@pkuczynski
pkuczynski merged commit c4dc431 into master Mar 11, 2026
38 checks passed
@pkuczynski
pkuczynski deleted the perf/batch-postgres-drop-statements branch March 11, 2026 14:07
@github-actions github-actions Bot added this to the 1.0 milestone Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

5 participants