5

I am trying to reduce some logging noise I am getting from PostgreSQL on my Heroku/Rails application. Specifically, I am trying to configure the client_min_messages setting to warning instead of the default notice.

I followed the steps in this post and specified min_messages: warning in my database.yml file but that doesn't seem to have any effect on my Heroku PostgreSQL instance. I'm still seeing NOTICE messages in my logs and when I run SHOW client_min_messages on the database it still returns notice.

Here is a redacted example of the logs I'm seeing in Papertrail:

Nov 23 15:04:51 my-app-name-production app/postgres.123467 [COLOR] [1234-5]  sql_error_code = 00000 log_line="5733" application_name="puma: cluster worker 0: 4 [app]" NOTICE:  text-search query contains only stop words or doesn't contain lexemes, ignored

I can also confirm that the setting does seem to be in the Rails configuration - Rails.application.config.database_configuration[Rails.env] in a production console does show a hash containing "min_messages"=>"warning"

I also tried manually updating that via the PostgreSQL console - so SET client_min_messages TO WARNING; - but that setting doesn't 'stick'. It seems to be reset on the next session.

How do I configure client_min_messages to be warning on Heroku/Rails?

6
  • 1
    Hmmm it seems like min_messages: warning should work according to the docs api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/… Commented Dec 1, 2022 at 17:35
  • Right, that seems to be true from the blog post I referenced as well. BUT I can confirm that I have min_messages: warning in my database.yml and it's not working on Heroku. Does Heroku actually use your database.yml at all? Commented Dec 1, 2022 at 18:48
  • 1
    When you issue the SET client_min_messages TO WARNING; it applies only to your current session/connection. Setting it on the database level by ALTER DATABASE your_database SET client_min_messages TO 'warning'; sets it as a default for newly established connections from that point onward. Those new connections can still change it within their scope. Also, something else can still restore the database setting to the initial, default-default when you reset/rebuild the server. Commented Dec 1, 2022 at 19:00
  • "I'm still seeing NOTICE messages in my logs" You are aware that there is a separate setting for log output? log_min_messages (But the default for that is WARNING, so maybe you just phrased confusingly ...) Commented Dec 2, 2022 at 10:35
  • Good point: which logs are my logs? Chris' command set the default level for client log, database.yml specifies the same thing on the client side, Erwin's sets that for the server log saved to log_destination Commented Dec 2, 2022 at 10:50

2 Answers 2

2

If all else fails and your log is flooded by the server logs you can't control or client logs you can't trace and switch off, Papertrail lets you filter out anything you don't want. The database/client still generate them, Heroku still passes them to Papertrail, but Papertrail discards them once they come in.

Shotgun method, PostgreSQL-specific

REVOKE SET ON PARAMETER client_min_messages,log_min_messages FROM your_app_user;
REVOKE GRANT OPTION FOR SET ON PARAMETER client_min_messages,log_min_messages FROM your_app_user;
ALTER SYSTEM   SET client_min_messages=WARNING;
ALTER SYSTEM   SET log_min_messages   =WARNING;
ALTER DATABASE db_user_by_your_app SET client_min_messages=WARNING;
ALTER DATABASE db_user_by_your_app SET log_min_messages   =WARNING;
ALTER ROLE your_app_user SET client_min_messages=WARNING;
ALTER ROLE your_app_user SET log_min_messages   =WARNING;

And then you need to either wait, restart the app, force it to re-connect or restart the db/instance/server/cluster it connects to.

If your app opens and closes connections - just wait and with time old connections will be replaced by new ones using the new settings.

If it uses a pool, it'll keep re-using connections it already has, so you'll have to force it to re-open them for the change to propagate. You might need to restart the app, or they can be force-closed:

SELECT pg_terminate_backend(pid) from pg_stat_activity where pid<>pg_backend_pid();

The reason is that there's no way for you to alter session-level settings on the fly, from the outside - and all of the above only affects defaults for new sessions. The REVOKE will prevent the app user from changing the setting but it'll also throw an error if they actually try. I'm leaving this in for future reference, keeping in mind that at the moment Heroku supports PostgreSQL versions up to 14, and REVOKE SET ON PARAMETER was added in version 15.

To need all these at once, you'd have to be seeing logs from both ends of each connection in your Papertrail, connecting to multiple databases, using different users, who also can keep changing the settings. Check one by one to isolate the root cause.


Context

There's one log written to each client, one or more written on the server.

Depending on what feeds the log into your Papertrail, you might need to change only one of these. Manipulating settings can always be tricky because of how and when they apply. You have multiple levels where parameters can be specified, then overriden:

  1. system-level parameters, loaded from postgresql.conf, then postgresql -c/pg_ctl -o and postgresql.auto.conf, which reflects changes applied using ALTER SYSTEM SET ... or directly.
  2. database overrides system. Applied with ALTER DATABASE db SET...
  3. user/role overrides database. ALTER ROLE user SET...
  4. session overrides user/role. Changed with SET... that clients also use upon connection init. If the value for client_min_messages set under min_messages is specified both in database.yml and ENV['DATABASE_URL'], Rails will use the env setting, overriding the one found in .yml with it DATABASE_URL=postgres://localhost/rails_event_store_active_record?min_messages=warning
  5. transaction-level parameters are the most specific, overriding all else - they are basically session-level parameters that will change back to their initial setting at the end of transaction. SET LOCAL...

When a new session opens, it loads the system defaults, overrides them with the database-level, then role-level, after which clients typically issue their own SETs.

It might be a good idea to make sure you're using the settings in database.yml that you think you're using, since it's possible to have multiple sets of them. There can be some logic in your app that keeps altering the setting.

Sign up to request clarification or add additional context in comments.

2 Comments

A bunch of this won't be permitted on Heroku's hosted Postgres service, but this looks like a great answer in general.
Thanks. The main goal was to clarify how these settings propagate and affect the db behaviour. With that in mind, it's easier to figure your way around any managed db provider and how they manipulate the settings.
-2
+500

I think you want log_min_messages, not client_min_messages:

Controls which message levels are written to the server log. Valid values are DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, INFO, NOTICE, WARNING, ERROR, LOG, FATAL, and PANIC. Each level includes all the levels that follow it. The later the level, the fewer messages are sent to the log. The default is WARNING. Note that LOG has a different rank here than in client_min_messages. Only superusers and users with the appropriate SET privilege can change this setting.

I'm not sure if your database user will be allowed to set it, but you can try doing so at the database level:

ALTER DATABASE your_database
    SET log_min_messages TO 'warning';

If this doesn't work, and setting at the role or connection level doesn't work, and heroku pg:settings doesn't work (confirmed via other answers and comments), the answer might unfortunately be that this isn't possible on Heroku.

Heroku Postgres is a managed service, so the vendor makes certain decisions that aren't configurable. This might be one of those situations.

15 Comments

This doesn't seem to have any effect on the Heroku PostgreSQL instance
@JacoPretorius, I have updated my answer. (a) Try log_min_messages instead of client_min_messages. (b) Try using heroku pg:settings. I no longer have a Heroku Postgres database on which to test since they removed their free tier, so I haven't tested it.
Note that while alter database db set... will override the system setting, it's below and will be overriden by the role and session settings.
@Chris I get the same message - "pg:settings:log-min-messages is not a heroku command" That's a good try tho :(
@Chris I just tried that - that gives me "ERROR: permission denied to set parameter "log_min_messages"" :( I honestly think the answer here is that you can't do this with Heroku. You don't get access to these config settings if they're managing the DB for you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.