Skip to content

Add sql_notifications connection config option to disable SQL notifications - #56899

Merged
rafaelfranca merged 1 commit into
rails:mainfrom
rosa:sql-notifier
Jun 5, 2026
Merged

rafaelfranca merged 1 commit into
rails:mainfrom
rosa:sql-notifier

Conversation

@rosa

@rosa rosa commented Feb 27, 2026 •

Copy link
Copy Markdown
Member

Motivation / Background

The motivation for this PR came up when discussing suppressing Active Record notifications with @djmb. This is desirable in gems like Solid Cache, where instrumentation might not be needed once a cache deployment is stable. In that case, it might just add overhead and noise.

Solid Cache has an option to disable instrumentation that reaches into Active Record internals, swapping the current per-thread instrumenter with a new instrumenter with an empty fanout as notifier. The problem of this approach, in addition to reaching into internals, is that it requires all the call sites that could trigger AR instrumentation to be wrapped with disable_instrumentation.

We had the idea of improving this and making the possibility available to other gems like Solid Queue, that use separate database connections by default (via connects_to).

Detail

With this change, you can disable instrumentation per connection, as part of the configuration:

    cache:
      adapter: postgresql
      database: myapp_cache
      sql_notifications: false

This is similar to the seeds option added in #53379.

This change introduces a NullInstrumenter into ActiveSupport::Notifications, which is a no-op, stateless and thread-safe instrumenter that just executes blocks without publishing anything. We don't need to worry about the per-thread registry for this one. This new instrumenter is used in the DB adapter when sql_notifications is disabled.

Additionally, this change skips event buffering for async queries when sql_notifications is disabled. We don't need the event buffer or to swap the instrumenter stored in the isolated execution state.

Checklist

Before submitting the PR, make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue, include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behaviour change or additional feature. Minor bug fixes and documentation changes should not be included.

@rosa
rosa force-pushed the sql-notifier branch 2 times, most recently from c8f6c78 to 6178cd9 Compare March 4, 2026 16:27
@rafaelfranca
rafaelfranca force-pushed the sql-notifier branch 4 times, most recently from 9f76c01 to 2b132a2 Compare June 4, 2026 21:41
…ations

This might be useful for gems like Solid Cache, where disabling
instrumentation is beneficial for performance and for practical reasons
where you don't care much about the SQL logs. It could be used like
this:

```yml
    cache:
      adapter: postgresql
      database: myapp_cache
      sql_notifications: false
```

This introduces a `NullInstrumenter` into
`ActiveSupport::Notifications`, which is a no-op, stateless and
thread-safe instrumenter that just executes blocks without publishing
anything. We don't need to worry about the per-thread registry for this
one.

This new instrumenter is used in the DB adapter when `sql_notifications`
is disabled.

Additionally, this change skips event buffering for async queries when
`sql_notifications` is disabled. We don't need the event buffer or to
swap the instrumenter stored in the isolated execution state.
@rafaelfranca
rafaelfranca merged commit 060b488 into rails:main Jun 5, 2026
2 checks passed
rosa added a commit to rails/solid_queue that referenced this pull request Aug 29, 2026
silence_polling only covers the polling queries; heartbeats, semaphores
and maintenance still show up in the logs and reach every
sql.active_record subscriber, which is what #210 asked to generalize.
Rails 8.2 solves this at the connection level with the sql_notifications
option (rails/rails#56899), so document that instead of growing our own
list of things to silence.

See #210.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CACej2M9mLVDwpE3Bk8V41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants