Search code examples
rubypostgresqlsinatrasequel

How to set application_name with sinatra/sequel


I'm setting application_name in my connection string like so:

DB = Sequel.connect(:adapter=>'postgres', :host=> 'localhost',
:database=>'blah', :user=>'gator',
:application_name => 'blahwebapp')

However, when I view the pg_stat_activity or any other metric to filter down by application_name I'm not seeing it assigned properly.

My Pg gem is "pg (0.17.0)" and I believe that since 0.16 it's been able to handle the application name. sequel_pg is (1.6.8) and Sequel is (4.2.0).

An application name is set, but it is path/webserver related rather than what is set in the config:

/Users/gator/.rvm/gems/rub...47@blahwebapp/bin/shotgun

Even when I use a URL-type connection string it still doesn't register the application_name:

DB = Sequel.connect('postgres://gator@localhost/blah?application_name=blahwebapp')

That is the same URL I use in psql to connect and it shows up fine.

In the documentation for Sequel I don't see much about application_name as an option which has me worried:

Any thoughts/ideas on how to get it to respect the application_name?


Solution

  • Sequel's PostgreSQL adapter doesn't pass the URL directly to PostgreSQL. It can't really do so and keep backwards compatibility.

    It looks like you can just set the application_name at runtime. The best way to do this with Sequel is via after_connect:

    DB = Sequel.connect('postgres://gator@localhost/blah', :after_connect=>proc{|c| c.execute("SET application_name TO 'blahwebapp'")})
    

    It is possible to integrate this feature into Sequel so that using it inside the 'postgres://' URL would work correctly. I'm open to that if other people think it would be useful.