We have an AWS RDS PostgreSQL (v14.10) instance running on a db.t3.micro
instance. Randomly throughout the day (some days more than others), our .NET Core application starts failing with the following exceptions:
An exception occurred while iterating over the results of a query for context type 'App.Server.Database.DbContext'.
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
---> Npgsql.PostgresException (0x80004005): 53300: sorry, too many clients already
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure.
---> Npgsql.NpgsqlException (0x80004005): Exception while reading from stream
---> System.TimeoutException: Timeout during reading attempt
We haven't been able to track down the root cause yet, thinking we have an application side connection leak somewhere, but that hasn't yielded any results.
With the t3.micro
instance type, our max_connections
is set to 81 (using this query select * from pg_settings where name='max_connections';
), but we never see the DatabaseConnection metric in RDS monitoring get above 25-30. See the following screenshot of our metrics. We started throwing these errors just after 12:30 (see red arrow on right)
Has anyone see this issue before, or is there something else I should be looking into? Can anyone explain why the DatabaseConnections metric shows a count much lower than our max?