Skip to main content

Database connection pools and the PostgreSQL connection limit

Info

The page describes the pools of JDBC connections of Mercury DB (HgDB) 3.0, the setting defaultAutoCommit of the data sources and the connection limit of PostgreSQL that a cluster needs.

Pools of connections of a node​

Every node of the cluster keeps pools of connections to the database. The definitions are in context.xml of Tomcat (in the Docker image context.xml.docker-init):

Data sourceDatabasemaxTotalminIdle
jdbc/MercuryDBmercury-db14030
jdbc/MercuryDBCommonmercury-common405
jdbc/MercuryLdapDBPoSqlmercury-ldap201
together per node20036

In the Docker image the values of maxTotal are set by the environment variables POSTGRESQL_POOL_MAX_TOTAL (jdbc/MercuryDB, default 140), POSTGRESQL_POOL_COMMON_MAX_TOTAL (jdbc/MercuryDBCommon, default 40) and POSTGRESQL_POOL_LDAP_MAX_TOTAL (jdbc/MercuryLdapDBPoSql, default 20). A value must be a positive integer, otherwise the node does not start. Choose them so that the sum of all pools of all nodes fits in max_connections of PostgreSQL (see below). The change needs the image with this support (Mercury 3.0.4.23 and later; earlier images have the fixed values).

The compositions of the repository mercury-docker set the values so that the pools of all nodes fit in max_connections=400:

CompositionmaxTotal (main, common, authentication)Per nodeAll nodesmax_connections
single node140, 40, 20200200250
two nodes120, 40, 20180360400
three nodes85, 25, 15125375400

The values can be overridden in the .env file of the composition (the same variable names).

Connection limit of PostgreSQL​

PostgreSQL accepts 100 connections by default (max_connections). The pools of one node can use up to 200 connections, and three nodes up to 600, so with several nodes the default limit is too low. The symptom is an error of a write (for example CaseBusinessRest/save):

FATAL: sorry, too many clients already

It appeared when three nodes accepted 8 parallel writes each. Single requests and writes to one node passed.

How much to set:

  • 400 covers three nodes under a heavy parallel write (the compositions of the three-node and the two-node cluster in mercury-docker start postgres-db with max_connections=400).
  • The rule: max_connections should be not lower than the number of nodes multiplied by the sum of maxTotal of the pools, or lower maxTotal in context.xml so that the sum of the pools of all nodes fits in the limit. Every connection takes several MB of the server memory, so do not set more than needed. For larger clusters consider a pool in front of the database (for example PgBouncer).
  • A single node can use up to 200 connections, so under a heavy load the default limit of 100 can be tight even for one node.

The change requires a restart of PostgreSQL (a reload of the configuration is not enough). The parameter is set in postgresql.conf (not in pg_hba.conf, which concerns only authentication). On a running cluster, without changing the composition:

docker exec postgres-db psql -U <superuser> -c "ALTER SYSTEM SET max_connections = 400;"
docker restart postgres-db
docker exec postgres-db psql -U <superuser> -c "SHOW max_connections;"

ALTER SYSTEM saves the value in postgresql.auto.conf in the data directory, so it survives recreating the container. After the restart of the database restart the Mercury nodes so that the pools open the connections again.

You can check the use of connections with:

SELECT datname, count(*) FROM pg_stat_activity GROUP BY datname ORDER BY 2 DESC;
SHOW max_connections;

defaultAutoCommit of the data sources​

The data sources jdbc/MercuryDB and jdbc/MercuryDBCommon must have defaultAutoCommit="false":

context.xml
<Resource name="jdbc/MercuryDB" auth="Container" type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/mercury-db"
username="mercury" password="..." maxTotal="140" maxIdle="100"
minIdle="30" maxWaitMillis="1200000" defaultAutoCommit="false" />

The JDBC access layer (for example archiving a package of the index buffer) commits the transaction explicitly. With the default autoCommit of the pool a node logs the error PSQLException: Cannot commit when autoCommit is enabled for every package. Images of the application from the version 3.0.4.18 and the sample context.xml of the installation package have this setting; add it to your own context.xml if it is missing.