Skip to content

Add support for configuring database application_name for Rails

Stan Hu requested to merge sh-db-application-name into master

By default, the Ruby PostgreSQL driver will specify a fallback_application_name based on the process name and use that as part of the appname connection string. This appname gets sent to PgBouncer, which calls SET application_name every time a connection is used by Rails.

To reduce the load on the database, we want to have the ability to disable the setting of application_name. This can be done by setting this value to a blank string. Note that setting the environment variable PGAPPNAME to a blank string does not do the same thing because an empty environment variable is not defined.

Relates to https://gitlab.com/gitlab-com/gl-infra/infrastructure/-/issues/12054

Testing

  1. Set gitlab_rails['db_application_name'] = ''.
  2. Run gitlab-ctl reconfigure.
  3. Run gitlab-psql and notice blank values:
gitlabhq_production=# select pid, application_name from pg_stat_activity;
  pid  | application_name
-------+------------------
 14425 |
 14427 |
  4519 |
  2859 |
  1551 | psql
  4639 |
  4640 |
  4641 |
  2896 |
  2897 |
  2898 |
  2902 |
  2900 |
  2903 |
  2909 |
 14423 |
 14422 |
 14424 |
(18 rows)

To truly validate this, you can use strace and watch the output here:

params = {:user=>"gitlab", :dbname=>"gitlabhq_production", :host=>"/var/opt/gitlab/postgresql", :port=>5432, :sslcompression=>0, :application_name=>""}
PG.connect(params)

The strace shows:

4821  sendto(6, "\0\0\0002\0\3\0\0user\0gitlab\0database\0gitlabhq_production\0\0", 50, MSG_NOSIGNAL, NULL, 0) = 50

If you omit the :application_name key, you see bin/rails being sent:

4821  sendto(14, "\0\0\0M\0\3\0\0user\0gitlab\0database\0gitlabhq_production\0application_name\0bin/rails\0\0", 77, MSG_NOSIGNAL, NULL, 0) = 77

If you repeat the experiment with setting the parameter to mytest, you see:

gitlabhq_production=# select pid, application_name from pg_stat_activity;
  pid  | application_name
-------+------------------
 14425 |
 14427 |
  4519 |
  3570 | mytest
  1551 | psql
  4639 |
  4640 |
  4641 |
  3580 | mytest
 14423 |
 14422 |
 14424 |
(12 rows)

If you comment out the line, then you see:

gitlabhq_production=# select pid, application_name from pg_stat_activity;
  pid  |                        application_name
-------+-----------------------------------------------------------------
 14425 |
 14427 |
  4519 |
  4666 | sidekiq 5.2.9 queues:authorize...export_deletion [0 of 50 busy]
  1551 | psql
  4639 |
  4640 |
  4641 |
  4700 | sidekiq 5.2.9 queues:authorize...export_deletion [0 of 50 busy]
  4702 | sidekiq 5.2.9 queues:authorize...export_deletion [0 of 50 busy]
  4705 | sidekiq 5.2.9 queues:authorize...export_deletion [0 of 50 busy]
  4707 | sidekiq 5.2.9 queues:authorize...export_deletion [0 of 50 busy]
  4709 | sidekiq 5.2.9 queues:authorize...export_deletion [0 of 50 busy]
  4710 | sidekiq 5.2.9 queues:authorize...export_deletion [0 of 50 busy]
 14423 |
 14422 |
 14424 |
(17 rows)
Edited by Stan Hu

Merge request reports