-
Notifications
You must be signed in to change notification settings - Fork 93
Feat(Support): Migrating Support KB after-setting-pg_max_concurrent_queries #4938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+112
−0
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f97439
Migrating Support KB article:after-setting-pg_max_concurrent_queries-…
dascole ba7d753
Update app/_support/after-setting-pg_max_concurrent_queries-the-numbe…
dascole e38d302
Update app/_support/after-setting-pg_max_concurrent_queries-the-numbe…
dascole f3cf968
related resources fix
Guaris f22e7ff
Merge branch 'main' into feat/kb-pg_max_concurrent_queries
Guaris 2c11517
Merge branch 'main' into feat/kb-pg_max_concurrent_queries
Guaris 8b3f27c
Merge branch 'main' into feat/kb-pg_max_concurrent_queries
Guaris d43675e
rename file and title
Guaris 40b080d
vale
Guaris c52d752
vale
Guaris c724888
vale
Guaris 5ec55b1
vale again?
Guaris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
app/_support/pg_max_concurrent_queries-per-worker-process.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| --- | ||
| title: "pg_max_concurrent_queries is per worker process, not per node" | ||
| content_type: support | ||
| description: "How to limit the total number of PostgreSQL database connections when running {{site.base_gateway}} on multiple nodes in Kubernetes." | ||
| products: | ||
| - gateway | ||
|
|
||
| works_on: | ||
| - on-prem | ||
| - konnect | ||
|
|
||
| related_resources: | ||
| - text: "{{site.base_gateway}} configuration reference - `pg_max_concurrent_queries`" | ||
| url: /gateway/configuration/#pg-max-concurrent-queries | ||
| - text: "{{site.base_gateway}} configuration reference - `nginx_worker_processes`" | ||
| url: /gateway/configuration/#nginx-worker-processes | ||
|
|
||
| tldr: | ||
| q: Why does my PostgreSQL connection count exceed the limit I set with pg_max_concurrent_queries? | ||
| a: | | ||
| `pg_max_concurrent_queries` is applied **per nginx worker process**, so the actual maximum | ||
| number of concurrent database connections is: | ||
|
|
||
| ``` | ||
| pg_max_concurrent_queries × nginx_worker_processes × number of Kong nodes | ||
| ``` | ||
|
|
||
| To bring the total under your database connection limit, explicitly set | ||
| `nginx_worker_processes` to a fixed integer value rather than relying on the | ||
| default `auto` setting, which bases the count on the host's vCPUs and can | ||
| produce more workers than expected in Kubernetes. | ||
|
|
||
| --- | ||
|
|
||
| ## Problem | ||
|
|
||
| The PostgreSQL database used by {{site.base_gateway}} has a maximum number of permitted | ||
| connections. After setting `pg_max_concurrent_queries` to a value below that maximum, | ||
| the connection limit is still exceeded. {{site.base_gateway}} is running on multiple | ||
| nodes deployed in Kubernetes. | ||
|
|
||
| ## Cause | ||
|
|
||
| The `pg_max_concurrent_queries` setting is scoped **per nginx worker process**, not | ||
| per Kong node. The actual maximum number of concurrent database connections is therefore: | ||
|
|
||
| ``` | ||
| pg_max_concurrent_queries × nginx_worker_processes × number of Kong nodes | ||
| ``` | ||
|
|
||
| The `nginx_worker_processes` parameter defaults to `auto`, which sets the number of | ||
| worker processes equal to the number of available vCPUs. In a Kubernetes environment, | ||
| the calculation is based on the **host** vCPUs (not the container's CPU limit), which | ||
| can result in significantly more workers — and therefore more database connections — | ||
| than expected. | ||
|
|
||
| ## Solution | ||
|
|
||
| 1. Determine the current number of nginx worker processes on a running Kong node: | ||
|
|
||
| ```bash | ||
| ps aux | grep "[n]ginx: worker process" | wc -l | ||
| ``` | ||
|
|
||
| 2. Calculate the maximum connection count you need to stay within your database limit. | ||
| For example, if your database allows 200 connections, you have 4 Kong nodes, and | ||
| you want `pg_max_concurrent_queries` set to 10: | ||
|
|
||
| ``` | ||
| 200 connections ÷ (4 nodes × 10 queries) = 5 worker processes per node | ||
| ``` | ||
|
|
||
| 3. Explicitly set `nginx_worker_processes` to a fixed integer in `kong.conf`: | ||
|
|
||
| ``` | ||
| nginx_worker_processes = 5 | ||
| ``` | ||
|
|
||
| Or via an environment variable: | ||
|
|
||
| ```bash | ||
| KONG_NGINX_WORKER_PROCESSES=5 | ||
| ``` | ||
|
|
||
| 4. Reload or restart {{site.base_gateway}} for the change to take effect: | ||
|
|
||
| ```bash | ||
| kong reload | ||
| ``` | ||
|
|
||
| 5. Verify the worker count reflects the new value: | ||
|
|
||
| ```bash | ||
| ps aux | grep "[n]ginx: worker process" | wc -l | ||
| ``` | ||
|
|
||
| ## Validation | ||
|
|
||
| After restarting, confirm the total connection count stays within bounds by monitoring | ||
| your database connection metrics. For PostgreSQL, you can check active connections with: | ||
|
|
||
| ```sql | ||
| SELECT count(*) FROM pg_stat_activity WHERE datname = '<your_kong_database>'; | ||
| ``` | ||
|
|
||
| The count should remain at or below: | ||
|
|
||
| ``` | ||
| pg_max_concurrent_queries × nginx_worker_processes × number of Kong nodes | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.