The PostgreSQL database used by Kong 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. Kong Gateway is running on multiple
nodes deployed in Kubernetes.
pg_max_concurrent_queries is per worker process, not per node
Why does my PostgreSQL connection count exceed the limit I set with pg_max_concurrent_queries?
pg_max_concurrent_queries is per Nginx worker process, not per node.
Set nginx_worker_processes to a fixed integer so the total connection count stays within your database limit.
Problem
Cause
The pg_max_concurrent_queries setting is scoped per Nginx worker process, not
per Kong Gateway node. The actual maximum number of concurrent database connections is therefore:
pg_max_concurrent_queries × nginx_worker_processes × number of Kong nodesThe 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
-
Determine the current number of Nginx worker processes on a running Kong Gateway node:
ps aux | grep "[n]ginx: worker process" | wc -l -
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 Gateway nodes, and you want
pg_max_concurrent_queriesset to 10:200 connections ÷ (4 nodes × 10 queries) = 5 worker processes per node -
Explicitly set
nginx_worker_processesto a fixed integer inkong.conf:nginx_worker_processes = 5Or via an environment variable:
KONG_NGINX_WORKER_PROCESSES=5 -
Reload or restart Kong Gateway for the change to take effect:
kong reload -
Verify the worker count reflects the new value:
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:
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