Skip to content

fix(postgres): execute queries sequentially to avoid pg 8.19.0 deprecation warning - #12105

Merged
pkuczynski merged 2 commits into
masterfrom
fix/pg-concurrent-query-deprecation
Mar 6, 2026
Merged

pkuczynski merged 2 commits into
masterfrom
fix/pg-concurrent-query-deprecation

Conversation

@pkuczynski

Copy link
Copy Markdown
Member

Replaces Promise.all patterns that fire multiple queries concurrently on the same pg client connection with sequential execution. pg 8.19.0 deprecated calling client.query() while the client is already executing a query, and this will become a hard error in pg 9.0.

Affected methods in both PostgresQueryRunner and CockroachQueryRunner:

  • loadTables() — parallel metadata queries (columns, constraints, indices, foreign keys)
  • clearDatabase() — parallel DROP statements for views, materialized views, tables, sequences
  • dropEnumTypes() — parallel DROP TYPE statements

Closes #12055

@pkuczynski pkuczynski self-assigned this Mar 5, 2026
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Review Summary by Qodo

Execute database queries sequentially to avoid pg deprecation

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Replace concurrent Promise.all query patterns with sequential execution
• Avoid pg 8.19.0 deprecation warning for concurrent queries on same connection
• Affected methods: loadTables(), clearDatabase(), dropEnumTypes()
• Applied to both PostgresQueryRunner and CockroachQueryRunner classes
Diagram
flowchart LR
  A["Promise.all concurrent queries"] -->|"Replace with"| B["Sequential for-loop execution"]
  B -->|"Prevents"| C["pg 8.19.0 deprecation warning"]
  C -->|"Avoids"| D["pg 9.0 hard error"]
Loading

Grey Divider

File Changes

1. src/driver/postgres/PostgresQueryRunner.ts 🐞 Bug fix +16/-21

Sequential query execution in PostgreSQL runner

• Replaced Promise.all with sequential for loops in clearDatabase() method for dropping views,
 materialized views, and tables
• Converted parallel metadata queries in loadTables() to sequential execution for columns,
 constraints, indices, and foreign keys
• Updated dropEnumTypes() to execute DROP TYPE statements sequentially instead of concurrently

src/driver/postgres/PostgresQueryRunner.ts


2. src/driver/cockroachdb/CockroachQueryRunner.ts 🐞 Bug fix +17/-21

Sequential query execution in CockroachDB runner

• Replaced Promise.all with sequential for loops in clearDatabase() method for dropping views,
 tables, and sequences
• Converted parallel metadata queries in loadTables() to sequential execution for columns,
 constraints, indices, foreign keys, and enums
• Updated dropEnumTypes() to execute DROP TYPE statements sequentially instead of concurrently

src/driver/cockroachdb/CockroachQueryRunner.ts


Grey Divider

Qodo Logo

@pkuczynski pkuczynski added this to the 1.0 milestone Mar 5, 2026
@pkg-pr-new

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

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

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

commit: 507b133

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

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

Copy link
Copy Markdown

Code Review by Qodo

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

Grey Divider


Action required

1. Postgres loadTables still concurrent 🐞 Bug ⛯ Reliability
Description
PostgresQueryRunner.loadTables() still returns Promise.all(dbTables.map(...)) while the
per-table/per-column mapping performs additional await this.query(...) calls (e.g., enum and
spatial metadata lookups). Because connect() caches a single databaseConnection per query
runner, those queries can overlap and still trigger the pg concurrent-query deprecation/hard error.
Code

src/driver/postgres/PostgresQueryRunner.ts[R3622-3623]

      return Promise.all(
Evidence
The query runner holds onto a single pooled connection (this.databaseConnection) and query()
executes against it; therefore, concurrent this.query() calls from Promise.all(...) can overlap
on the same client connection. In loadTables(), even after making the initial metadata queries
sequential, the code still processes tables/columns via Promise.all(...) and performs further
this.query() calls inside those async callbacks (enum and geometry/geography lookups), which
reintroduces concurrent queries.

src/driver/postgres/PostgresQueryRunner.ts[82-85]
src/driver/postgres/PostgresQueryRunner.ts[244-262]
src/driver/postgres/PostgresQueryRunner.ts[3616-3645]
src/driver/postgres/PostgresQueryRunner.ts[3785-3797]
src/driver/postgres/PostgresQueryRunner.ts[3819-3835]

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

## Issue description
`PostgresQueryRunner.loadTables()` still issues additional database queries inside async callbacks executed via `Promise.all(...)`. Since PostgresQueryRunner caches a single pg connection per query runner, concurrent `this.query()` calls can overlap and still trigger the pg 8.19 deprecation warning / pg 9 hard error.
### Issue Context
- `connect()` caches `this.databaseConnection`.
- `query()` always executes against that cached connection.
- `loadTables()` uses `Promise.all(dbTables.map(async ...))` and `Promise.all(...map(async dbColumn => ... await this.query(...)))`, which can interleave.
### Fix Focus Areas
- src/driver/postgres/PostgresQueryRunner.ts[3616-3645]
- src/driver/postgres/PostgresQueryRunner.ts[3785-3835]
### Suggested fix approach
Choose one:
1) **Local sequencing in `loadTables()`**
- Replace `return Promise.all(dbTables.map(...))` with a sequential `for...of` that pushes built `Table` objects into an array.
- Replace `table.columns = await Promise.all(...map(async ...))` with a `for...of` loop that builds columns sequentially **at least for branches that call `this.query()`** (enum/spatial/user-defined lookups).
2) **Global sequencing in `query()` (more robust)**
- Add a private promise-chain/lock on the query runner (e.g., `private queryQueue = Promise.resolve()`), and wrap the actual `databaseConnection.query(...)` execution so each call waits for the previous to finish.
- Ensure the queue advances even when a query throws (use `finally`).

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


2. Postgres constraints still concurrent 🐞 Bug ⛯ Reliability
Description
PostgresQueryRunner still executes some DDL operations concurrently via Promise.all (check +
exclusion constraints). Since the query runner uses a single cached pg client connection, these
methods can still issue overlapping client.query() calls and continue to hit the pg 8.19+
deprecation (pg 9.0 error).
Code

src/driver/postgres/PostgresQueryRunner.ts[R3616-3619]

+        const dbColumns: ObjectLiteral[] = await this.query(columnsSql)
+        const dbConstraints: ObjectLiteral[] = await this.query(constraintsSql)
+        const dbIndices: ObjectLiteral[] = await this.query(indicesSql)
+        const dbForeignKeys: ObjectLiteral[] = await this.query(foreignKeysSql)
Evidence
The PR is making queries sequential in loadTables, but
createCheckConstraints/dropCheckConstraints and
createExclusionConstraints/dropExclusionConstraints still use Promise.all, which can run
multiple executeQueries() calls at once. executeQueries() calls this.query() internally, and
query() uses the single cached databaseConnection from connect() and calls
databaseConnection.query(...) directly—so overlapping promises can overlap pg client queries on
the same connection.

src/driver/postgres/PostgresQueryRunner.ts[2901-2909]
src/driver/postgres/PostgresQueryRunner.ts[2942-2950]
src/driver/postgres/PostgresQueryRunner.ts[2984-2992]
src/driver/postgres/PostgresQueryRunner.ts[3030-3038]
src/query-runner/BaseQueryRunner.ts[732-748]
src/driver/postgres/PostgresQueryRunner.ts[82-85]
src/driver/postgres/PostgresQueryRunner.ts[244-262]

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

## Issue description
`PostgresQueryRunner` still has several methods that run multiple DDL operations concurrently using `Promise.all(...)`. Because a QueryRunner caches a single pg client connection and `executeQueries()` ultimately calls `databaseConnection.query(...)`, these `Promise.all` blocks can still overlap `client.query()` calls on the same connection, defeating the PR’s goal (pg 8.19 deprecation / pg 9 error).
### Issue Context
This PR already sequentializes queries in `loadTables`, `clearDatabase`, and `dropEnumTypes`. For completeness (and to ensure the deprecation is avoided across common schema/migration flows), update the remaining constraint helpers to be sequential as well.
### Fix Focus Areas
- src/driver/postgres/PostgresQueryRunner.ts[2901-2909]
- src/driver/postgres/PostgresQueryRunner.ts[2942-2950]
- src/driver/postgres/PostgresQueryRunner.ts[2984-2992]
- src/driver/postgres/PostgresQueryRunner.ts[3030-3038]

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


3. Cockroach loadTables still concurrent 🐞 Bug ⛯ Reliability
Description
CockroachQueryRunner.loadTables() still returns Promise.all(dbTables.map(...)) while the
per-column mapping performs extra await this.query(...) calls (generated column expression and
spatial metadata). Since the query runner caches a single connection, these can overlap and still
violate the “no concurrent queries per client” requirement.
Code

src/driver/cockroachdb/CockroachQueryRunner.ts[R3180-3182]

      return Promise.all(
Evidence
Like the Postgres runner, CockroachQueryRunner caches a single databaseConnection and query()
executes against it. loadTables() still uses Promise.all(dbTables.map(...)) and builds columns
with Promise.all(...), but inside those callbacks it runs additional this.query() calls
(generated columns metadata, spatial metadata). This can still cause overlapping
databaseConnection.query(...) calls on a single connection.

src/driver/cockroachdb/CockroachQueryRunner.ts[100-105]
src/driver/cockroachdb/CockroachQueryRunner.ts[274-300]
src/driver/cockroachdb/CockroachQueryRunner.ts[3173-3203]
src/driver/cockroachdb/CockroachQueryRunner.ts[3495-3506]
src/driver/cockroachdb/CockroachQueryRunner.ts[3522-3538]

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

## Issue description
`CockroachQueryRunner.loadTables()` still performs `this.query()` inside async callbacks executed via `Promise.all(...)`. Because CockroachQueryRunner caches a single connection per query runner, this can still create concurrent `databaseConnection.query(...)` calls.
### Issue Context
- `connect()` caches `this.databaseConnection`.
- `query()` executes on that cached connection.
- `loadTables()` uses `Promise.all(dbTables.map(...))` and `Promise.all` for columns while calling `this.query()` for generated column expressions and spatial metadata.
### Fix Focus Areas
- src/driver/cockroachdb/CockroachQueryRunner.ts[3173-3203]
- src/driver/cockroachdb/CockroachQueryRunner.ts[3495-3506]
- src/driver/cockroachdb/CockroachQueryRunner.ts[3522-3538]
### Suggested fix approach
Choose one:
1) **Local sequencing in `loadTables()`**
- Replace `return Promise.all(dbTables.map(...))` with a `for...of` loop assembling `Table[]`.
- Replace `table.columns = await Promise.all(...)` with sequential column construction when branches may execute `this.query()`.
2) **Global sequencing in `query()`**
- Add a per-query-runner queue/lock so even if callers use `Promise.all`, actual wire queries are executed sequentially.
- Ensure errors don’t break the queue (advance in `finally`).

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


View more (1)
4. Postgres constraints still concurrent 🐞 Bug ⛯ Reliability
Description
PostgresQueryRunner still executes some DDL operations concurrently via Promise.all (check +
exclusion constraints). Since the query runner uses a single cached pg client connection, these
methods can still issue overlapping client.query() calls and continue to hit the pg 8.19+
deprecation (pg 9.0 error).
Code

src/driver/postgres/PostgresQueryRunner.ts[R3616-3619]

+        const dbColumns: ObjectLiteral[] = await this.query(columnsSql)
+        const dbConstraints: ObjectLiteral[] = await this.query(constraintsSql)
+        const dbIndices: ObjectLiteral[] = await this.query(indicesSql)
+        const dbForeignKeys: ObjectLiteral[] = await this.query(foreignKeysSql)
Evidence
The PR is making queries sequential in loadTables, but
createCheckConstraints/dropCheckConstraints and
createExclusionConstraints/dropExclusionConstraints still use Promise.all, which can run
multiple executeQueries() calls at once. executeQueries() calls this.query() internally, and
query() uses the single cached databaseConnection from connect() and calls
databaseConnection.query(...) directly—so overlapping promises can overlap pg client queries on
the same connection.

src/driver/postgres/PostgresQueryRunner.ts[2901-2909]
src/driver/postgres/PostgresQueryRunner.ts[2942-2950]
src/driver/postgres/PostgresQueryRunner.ts[2984-2992]
src/driver/postgres/PostgresQueryRunner.ts[3030-3038]
src/query-runner/BaseQueryRunner.ts[732-748]
src/driver/postgres/PostgresQueryRunner.ts[82-85]
src/driver/postgres/PostgresQueryRunner.ts[244-262]

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

## Issue description
`PostgresQueryRunner` still has several methods that run multiple DDL operations concurrently using `Promise.all(...)`. Because a QueryRunner caches a single pg client connection and `executeQueries()` ultimately calls `databaseConnection.query(...)`, these `Promise.all` blocks can still overlap `client.query()` calls on the same connection, defeating the PR’s goal (pg 8.19 deprecation / pg 9 error).
### Issue Context
This PR already sequentializes queries in `loadTables`, `clearDatabase`, and `dropEnumTypes`. For completeness (and to ensure the deprecation is avoided across common schema/migration flows), update the remaining constraint helpers to be sequential as well.
### Fix Focus Areas
- src/driver/postgres/PostgresQueryRunner.ts[2901-2909]
- src/driver/postgres/PostgresQueryRunner.ts[2942-2950]
- src/driver/postgres/PostgresQueryRunner.ts[2984-2992]
- src/driver/postgres/PostgresQueryRunner.ts[3030-3038]

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



Remediation recommended

5. No functional regression test 📘 Rule violation ⛯ Reliability
Description
This PR fixes an issue caused by concurrent client.query() calls but does not add/update a
functional test to prevent regressions. Without coverage, future refactors could reintroduce the
deprecated concurrent query pattern.
Code

src/driver/postgres/PostgresQueryRunner.ts[R3616-3619]

+        const dbColumns: ObjectLiteral[] = await this.query(columnsSql)
+        const dbConstraints: ObjectLiteral[] = await this.query(constraintsSql)
+        const dbIndices: ObjectLiteral[] = await this.query(indicesSql)
+        const dbForeignKeys: ObjectLiteral[] = await this.query(foreignKeysSql)
Evidence
The checklist expects issue fixes to be covered by functional tests; the PR description indicates
this is an issue fix for concurrent query deprecation, while the changed code shows the behavior
change but no accompanying functional test update is present in the provided PR diff.

Rule 3: Prefer functional tests over per-issue tests
src/driver/postgres/PostgresQueryRunner.ts[3616-3619]

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

## Issue description
This PR changes Postgres/Cockroach query execution order to avoid concurrent `client.query()` calls, but it does not add/update a functional test to guard against regressions.
## Issue Context
The PR is explicitly an issue fix for pg 8.19.0 concurrent query deprecation (future hard error in pg 9.0). The fix should be backed by a functional test that exercises the affected QueryRunner paths (e.g., metadata loading via `getTable()` / schema introspection) so concurrency regressions are caught.
## Fix Focus Areas
- src/driver/postgres/PostgresQueryRunner.ts[3616-3619]
- test/functional/query-runner/drop-table.test.ts[10-77]ান

ⓘ 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

Qodo Logo

@pkuczynski
pkuczynski enabled auto-merge (squash) March 6, 2026 22:28
@cloudflare-workers-and-pages

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

Copy link
Copy Markdown

Deploying typeorm with  Cloudflare Pages  Cloudflare Pages

Latest commit: 507b133
Status: ✅  Deploy successful!
Preview URL: https://c54b8ea1.typeorm.pages.dev
Branch Preview URL: https://fix-pg-concurrent-query-depr.typeorm.pages.dev

View logs

@sonarqubecloud

sonarqubecloud Bot commented Mar 6, 2026

Copy link
Copy Markdown

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

Copy link
Copy Markdown

Persistent review updated to latest commit 507b133

1 similar comment
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 507b133

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.

pg 8.19.0 deprecation issue

3 participants