I have a Rails API with a PostgreSQL database.
Some requests to the API show a strange behavior that doesn't depend on the endpoint.
These requests (around 5-10% of total requests) start with the same 7 database queries :
- SET client_min_messages TO ?
- SET standard_conforming_strings = on
- SET SESSION timezone TO ?
- SELECT t.oid, t.typname FROM pg_type WHERE t.typname IN ( ? ) ...
The request also takes a long time to start before the 7 queries are executed.
It seems to be the database adapter initiating a connection. ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
This significantly slows down the query.
I am using a PostegreSQL 11.6 AWS RDS instance, with default parameters.
Here is my database.yml config :
default: &default
adapter: postgresql
encoding: unicode
username: *****
password: *****
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
production:
<<: *default
database: *****
username: *****
password: *****
pool: 50
How do I reduce the number of connections initiating ? Is there a way to cache the queries ?
Thank you,