The parallel iteration node reports an error after running many times, and there are too many database connections. #20042

Closed
opened 2026-02-21 20:05:32 -05:00 by yindo · 1 comment
Owner

Originally created by @azooly on GitHub (Oct 30, 2025).

Originally assigned to: @laipz8200 on GitHub.

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.9.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

The parallel iteration node reports an error after running many times, and there are too many database connections.

Error message:
(psycopg2.OperationalError) FATAL: sorry, too many clients already (Background on this error at: https://sqlalche.me/e/20/e3q8)

Image

.env config :
SQLALCHEMY_POOL_SIZE = 20 and SQLALCHEMY_POOL_SIZE = 50
POSTGRES_MAX_CONNECTIONS = 100 and POSTGRES_MAX_CONNECTIONS = 200

I have tried it all, and if I execute too much in a row, this error will be reported.

✔️ Expected Behavior

No error is reported, and the database connection is released normally.

Actual Behavior

The parallel iteration node reports an error after running many times, and there are too many database connections.

Error message:
(psycopg2.OperationalError) FATAL: sorry, too many clients already (Background on this error at: https://sqlalche.me/e/20/e3q8)

Image

.env config :
SQLALCHEMY_POOL_SIZE = 20 and SQLALCHEMY_POOL_SIZE = 50
POSTGRES_MAX_CONNECTIONS = 100 and POSTGRES_MAX_CONNECTIONS = 200

I have tried it all, and if I execute too much in a row, this error will be reported.

Originally created by @azooly on GitHub (Oct 30, 2025). Originally assigned to: @laipz8200 on GitHub. ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.9.2 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce The parallel iteration node reports an error after running many times, and there are too many database connections. Error message: (psycopg2.OperationalError) FATAL: sorry, too many clients already (Background on this error at: https://sqlalche.me/e/20/e3q8) <img width="1225" height="565" alt="Image" src="https://github.com/user-attachments/assets/25a51fe9-c929-48a4-95ce-015f3ab22818" /> .env config : SQLALCHEMY_POOL_SIZE = 20 and SQLALCHEMY_POOL_SIZE = 50 POSTGRES_MAX_CONNECTIONS = 100 and POSTGRES_MAX_CONNECTIONS = 200 I have tried it all, and if I execute too much in a row, this error will be reported. ### ✔️ Expected Behavior No error is reported, and the database connection is released normally. ### ❌ Actual Behavior The parallel iteration node reports an error after running many times, and there are too many database connections. Error message: (psycopg2.OperationalError) FATAL: sorry, too many clients already (Background on this error at: https://sqlalche.me/e/20/e3q8) <img width="1225" height="565" alt="Image" src="https://github.com/user-attachments/assets/25a51fe9-c929-48a4-95ce-015f3ab22818" /> .env config : SQLALCHEMY_POOL_SIZE = 20 and SQLALCHEMY_POOL_SIZE = 50 POSTGRES_MAX_CONNECTIONS = 100 and POSTGRES_MAX_CONNECTIONS = 200 I have tried it all, and if I execute too much in a row, this error will be reported.
yindo added the 🐞 bug label 2026-02-21 20:05:32 -05:00
yindo closed this issue 2026-02-21 20:05:32 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 30, 2025):

This is a known issue in Dify when running parallel iteration nodes: the "too many clients" error happens because database connections are not always released quickly enough under high concurrency, especially if sessions aren't closed properly in parallel workflows. Adjusting SQLALCHEMY_POOL_SIZE and POSTGRES_MAX_CONNECTIONS helps, but isn't always enough if there's a connection leak or if parallel tasks hold connections too long [source 1] [source 2].

Best practices from the community and maintainers include: lowering SQLALCHEMY_POOL_SIZE (e.g., to 20), setting SQLALCHEMY_MAX_OVERFLOW=10, reducing SQLALCHEMY_POOL_RECYCLE (e.g., to 1800), and increasing POSTGRES_MAX_CONNECTIONS if your server can handle it. Always restart all Dify services after changing these settings. If you don't need maximum concurrency, reduce workflow parallelism by lowering the MAX_PARALLEL_LIMIT environment variable [source].

Recent Dify releases have improved connection handling by ensuring sessions are closed before starting threads or long-running tasks, but if you use custom plugins or background workers, make sure they also clean up sessions properly. In multi-threaded environments, each thread should use its own SQLAlchemy session (e.g., via scoped_session or creating a new session per thread) to avoid leaks [source].

If you still see the error, check for other clients using the same PostgreSQL instance, and make sure your server has enough memory for the increased connections. Monitor your active connections and logs for lingering sessions or plugin errors if the problem persists.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Oct 30, 2025): <!-- Answer --> This is a known issue in Dify when running parallel iteration nodes: the "too many clients" error happens because database connections are not always released quickly enough under high concurrency, especially if sessions aren't closed properly in parallel workflows. Adjusting `SQLALCHEMY_POOL_SIZE` and `POSTGRES_MAX_CONNECTIONS` helps, but isn't always enough if there's a connection leak or if parallel tasks hold connections too long [[source 1]](https://github.com/langgenius/dify/issues/23904) [[source 2]](https://github.com/langgenius/dify/issues/26537). Best practices from the community and maintainers include: lowering `SQLALCHEMY_POOL_SIZE` (e.g., to 20), setting `SQLALCHEMY_MAX_OVERFLOW=10`, reducing `SQLALCHEMY_POOL_RECYCLE` (e.g., to 1800), and increasing `POSTGRES_MAX_CONNECTIONS` if your server can handle it. Always restart all Dify services after changing these settings. If you don't need maximum concurrency, reduce workflow parallelism by lowering the `MAX_PARALLEL_LIMIT` environment variable [[source]](https://github.com/langgenius/dify/issues/26537). Recent Dify releases have improved connection handling by ensuring sessions are closed before starting threads or long-running tasks, but if you use custom plugins or background workers, make sure they also clean up sessions properly. In multi-threaded environments, each thread should use its own SQLAlchemy session (e.g., via `scoped_session` or creating a new session per thread) to avoid leaks [[source]](https://github.com/langgenius/dify/issues/21931). If you still see the error, check for other clients using the same PostgreSQL instance, and make sure your server has enough memory for the increased connections. Monitor your active connections and logs for lingering sessions or plugin errors if the problem persists. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/9f342fa5-694b-4980-9584-fc90beed4d43?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/9f342fa5-694b-4980-9584-fc90beed4d43?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/9f342fa5-694b-4980-9584-fc90beed4d43?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/9f342fa5-694b-4980-9584-fc90beed4d43?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/9f342fa5-694b-4980-9584-fc90beed4d43?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/9f342fa5-694b-4980-9584-fc90beed4d43?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/9f342fa5-694b-4980-9584-fc90beed4d43?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/27657)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#20042