{"id":13878168,"url":"https://github.com/tines/rails-pg-adapter","last_synced_at":"2025-07-16T14:31:37.594Z","repository":{"id":148761939,"uuid":"620798234","full_name":"tines/rails-pg-adapter","owner":"tines","description":"Rails Postgres ActiveRecord patches for common production workloads","archived":false,"fork":false,"pushed_at":"2024-02-06T21:38:22.000Z","size":46,"stargazers_count":51,"open_issues_count":1,"forks_count":0,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-11-15T05:06:02.861Z","etag":null,"topics":["activerecord","availability","monkeypatch","postgresql","rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tines.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-03-29T11:51:50.000Z","updated_at":"2024-04-28T01:50:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"cbd3f838-f143-49cb-ae65-cee8606b4b65","html_url":"https://github.com/tines/rails-pg-adapter","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tines%2Frails-pg-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tines%2Frails-pg-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tines%2Frails-pg-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tines%2Frails-pg-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tines","download_url":"https://codeload.github.com/tines/rails-pg-adapter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226138849,"owners_count":17579496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","availability","monkeypatch","postgresql","rails"],"created_at":"2024-08-06T08:01:41.667Z","updated_at":"2024-11-24T07:30:49.731Z","avatar_url":"https://github.com/tines.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# RailsPgAdapter\n\nThis project allows you to monkey patch `ActiveRecord` (PostgreSQL) and auto-heal applications in production when PostgreSQL database fails over or when a cached column (in `ActiveRecord` schema cache) is removed from the database from a migration in another process.\n\n## Installation\n\nInstall the gem and add to the application's Gemfile by executing:\n\n    $ bundle add rails-pg-adapter\n\nIf bundler is not being used to manage dependencies, install the gem by executing:\n\n    $ gem install rails-pg-adapter\n\n## Usage\n\n### Auto healing connections when PostgreSQL database fails over\n\n```ruby\n# config/initializers/rails_pg_adapter.rb\n\nRailsPgAdapter.configure do |c|\n  c.add_failover_patch = true\nend\n```\n\nThis will add the monkey patch which resets the `ActiveRecord` connections in the connection pool when the database fails over. The patch will reset the connection and re-raise the error each time it detects that an exception related to a database failover is detected.\n\n### Retrying queries\n\nWhen the database is failing you can retry queries that are not in a transaction. The gem will perform a back off retry in establishing the connection.\nOnce the back off is reached and no connection is found, it will bubble up the exception. Otherwise, the query will be retried with a new connection.\n\nIt is an opt-in functionality. You can supply your own back off figures for retries (in seconds) as following:\n\n```ruby\n# config/initializers/rails_pg_adapter.rb\n\nRailsPgAdapter.configure do |c|\n  c.add_failover_patch = true\n  c.reconnect_with_backoff = [0.5, 1, 2, 4, 8, 16] # seconds\n...\nend\n\n```\n\n### Refresh model column information on the fly after an existing column is dropped\n\n```ruby\n# config/initializers/rails_pg_adapter.rb\n\nRailsPgAdapter.configure do |c|\n  c.add_reset_column_information_patch = true\nend\n```\n\nThis will clear the `ActiveRecord` schema cache and reset the `ActiveRecord` column information memoized on the model. The patch will reset the relevant information and re-raise the error each time it detects that an exception related to a dropped column is raised.\n\n## How does it work\n\nDuring a database failover in production, the `ActiveRecord` connection pool can become exhausted as queries are made against the database during the failover process. This can leave the `ActiveRecord` connection pools with stale or bad connections, even after the database has successfully recovered. Recovering from this issue usually requires a rolling restart of the application processes or containers.\n\n`RailsPgAdapter` addresses this problem by resetting the connection pool and re-raises the original exception from an `ActiveRecord` monkey patch. This allows the application to auto-heal from stale connections on its own (after database recovery) when performing queries for a new request, without requiring manual intervention.\n\nAnother issue with `ActiveRecord` queries is `PG::UndefinedColumn`, which occurs when an `ActiveRecord` model includes a `SELECT` query with the name of a column that has been dropped from a Rails migration. This can happen even if the column isn't being referenced anywhere in the code. It occurs when a model is using `ignored_columns`, which prompts `ActiveRecord` to perform a dedicated lookup of the allowed columns in a select, such as `SELECT \"users\".name, \"users\".template_id....\"`, instead of `SELECT \"users\".*`. When a column like `template_id` is dropped, PostgreSQL throws an undefined column error, which is bubbled up by `ActiveRecord` into `PG::UndefinedColumn`. Recovering from this issue also usually requires a rolling restart of the application processes or containers.\n\n`RailsPgAdapter` solves this second issue by resetting the `ActiveRecord` schema cache and memoized model column information when it detects a `PG::UndefinedColumn` raised from a monkey patch. Resetting the column information forces `ActiveRecord` to refresh its schema cache by loading the table information from the database and no longer reference the dropped column for new queries, without requiring manual intervention.\n\n## Development\n\n- Install ruby 3.0\n\n```\n\\curl -sSL https://get.rvm.io | bash\n\nrvm install 3.0.0\n\nrvm use 3.0.0\n```\n\n- `docker compose up -d` - to spin up postgres locally\n- `bundle exec rspec` to run the tests.\n- You can also run `bin/console` for an interactive prompt.\n\nTo install this gem onto your local machine, run `bundle exec rake install`.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/tines/rails-pg-adapter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tines/rails-pg-adapter/blob/main/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the `RailsPgAdapter` project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tines/rails-pg-adapter/blob/main/CODE_OF_CONDUCT.md).\n\n## Releasing a new version\n\n- Bump version in `version.rb`\n- Update `CHANGELOG.md`\n- Push the changes to `main`\n- Run the release script with the new version `./bin/release.sh 0.2.0`\n  - Note: It will ask for MFA.\n- Create a new release - https://github.com/tines/rails-pg-adapter/releases/new\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftines%2Frails-pg-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftines%2Frails-pg-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftines%2Frails-pg-adapter/lists"}