Skip to content

Instantly share code, notes, and snippets.

@guizmaii
Last active April 2, 2024 15:59
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save guizmaii/1cacffef793c2ba9645083c3e18b3d8c to your computer and use it in GitHub Desktop.
Save guizmaii/1cacffef793c2ba9645083c3e18b3d8c to your computer and use it in GitHub Desktop.
AWS PostgreSQL max_connection per instance type

The default formula use by AWS RDS to calculate the max_connections parameter is: LEAST({DBInstanceClassMemory/9531392},5000)

But It's hard to find the exact value of DBInstanceClassMemory.

So, here are the values I got when I ran the SQL commmand: show max_connections; in some RDS instances:

Instance type RAM (GB) max_connections
db.t2.small 2 198
db.t2.medium 4 413
db.t2.large 8 856
db.m4.large 8 856
db.r4.large 15.25 1660

These are default values. You can always change the max_connections value in the parameters group of your DB.

@pthieu
Copy link

pthieu commented Dec 30, 2018

@guizmaii: any chance you can grab the max_connections for a t2.micro or t2.nano?

@danielrangelsa
Copy link

@guizmaii
Copy link
Author

@danielrangelsa Thanks! :)

@aneema-mdsol
Copy link

aneema-mdsol commented Apr 28, 2021

@danielrangelsa how can I get the value of DBInstanceClassMemory in code (using ROR) considering I have db instance class (db.m4.2xlarge) ?

LEAST({DBInstanceClassMemory/9531392},5000)

@danielrangelsa
Copy link

@aneema-mdsol
DBInstanceClassMemory returns an integer with the number of memory bytes allocated for the database instance class associated with the current database instance, minus the memory used by the RDS processes that manage the instance.

I don't know if it is possible to consult directly.
In its place I would create a procedure to save this information in a table and from there, I would work with the information.

@aneema-mdsol
Copy link

Using describe_db_instances I'm getting the value of db_instance_class which is db.m4.2xlarge.

  1. Depends on the value of db_instance_class, is there a way I can directly get the value of max_connections ?
  2. There is also another way using describe_db_instances or running below cmd in AWS CLI, I can get the value of max_connections:
    aws rds describe-db-parameters --db-parameter-group-name mdsol-postgres11 --source system which is as below:
{
    "Parameters": [
     ...., 
          {
            "ParameterName": "max_connections",
            "ParameterValue": "LEAST({DBInstanceClassMemory/9531392},5000)",
            "Description": "Sets the maximum number of concurrent connections.",
            "Source": "system",
            "ApplyType": "static",
            "DataType": "integer",
            "AllowedValues": "6-8388607",
            "IsModifiable": true,
            "ApplyMethod": "pending-reboot"
        },
...,

But, now I am not sure what I should pass to LEAST({DBInstanceClassMemory/9531392},5000) how can I can get the value of max_connections as show here

@danielrangelsa
Copy link

danielrangelsa commented Apr 28, 2021

You can use the query below to collect the value that is the result of the equation LEAST({DBInstanceClassMemory/9531392},5000)

select * from pg_settings where name='max_connections';

max_connections is the result of an equation

In some cases, depending on the way your system was built, you may end up reaching 100% CPU usage before you even reach half of the possible connections.
keep your eyes open for this.

@stafot
Copy link

stafot commented Nov 11, 2021

Does this LEAST({DBInstanceClassMemory/9531392},5000) apply to graviton instances too. As it seems they support more max connections than the others based on https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Managing.html but doesn't have data for t4g family.

@ajhodgson
Copy link

Graviton - db.r6g.xlarge (32 GB) = 3484. Looks like the same formula.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment