Search code examples
hasura

How to limit the number of open db connections hasura makes?


I'm running hasura in an e2e test environment in a docker compose setup. I have my hasura service configured like so:

  hasura:
    restart: always
    build:
      context: .
      dockerfile: hasura.Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - postgres
      - postgres-second
    environment:
      HASURA_GRAPHQL_DATABASE_URL: postgresql://username:password@postgres:5432/client__dev?application_name=hasura
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
      HASURA_GRAPHQL_DEV_MODE: "true"
    # wait for postgres to be online: https://github.com/vishnubob/wait-for-it/tree/81b1373f17855a4dc21156cfe1694c31d7d1792e
    # `graphql-engine serve` was discovered by running `docker inspect hasura/graphql-engine:v2.6.0`
    command: /bin/sh -c "./wait-for-it.sh postgres:5432 && ./wait-for-it.sh postgres-second:5432 && graphql-engine serve"

I'm seeing that it's opening many many connections to my postgres database. When I run the following query against my database

select application_name, count(*) from pg_stat_activity
group by application_name
order by 2 desc

I see the following output

+---------------------+-----+
|application_name     |count|
+---------------------+-----+
|hasura-enterprise    |35   |
|hasura               |35   |
|graph-node-enterprise|13   |
|graph-node           |13   |
|                     |5    |
|IntelliJ IDEA 2023.2 |1    |
|api-server-prisma    |1    |
|postgres_fdw         |1    |
|api-database-url     |1    |
+---------------------+-----+

For context, the particular test cases that I'm going through use the hasura webhook delivery feature for a high throughput table.

Is there a way to limit how many connections Hasura will open to the database defined by HASURA_GRAPHQL_DATABASE_URL?


Solution

  • Yes in your config simply define variable

    HASURA_GRAPHQL_PG_CONNECTIONS: 10
    

    Where 10 is the value of the max number of connections. Documentation for more details.