{"id":20407823,"url":"https://github.com/restorando/sequel-pg_bulk_upsert","last_synced_at":"2025-10-11T04:48:13.051Z","repository":{"id":23279713,"uuid":"26638490","full_name":"restorando/sequel-pg_bulk_upsert","owner":"restorando","description":"Sequel extension to allow bulk upserts in postgresql","archived":false,"fork":false,"pushed_at":"2016-04-07T11:39:23.000Z","size":13,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-10-09T21:30:47.983Z","etag":null,"topics":[],"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/restorando.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-14T12:53:09.000Z","updated_at":"2020-10-21T20:16:52.000Z","dependencies_parsed_at":"2022-08-20T17:10:17.350Z","dependency_job_id":null,"html_url":"https://github.com/restorando/sequel-pg_bulk_upsert","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/restorando/sequel-pg_bulk_upsert","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/restorando%2Fsequel-pg_bulk_upsert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/restorando%2Fsequel-pg_bulk_upsert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/restorando%2Fsequel-pg_bulk_upsert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/restorando%2Fsequel-pg_bulk_upsert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/restorando","download_url":"https://codeload.github.com/restorando/sequel-pg_bulk_upsert/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/restorando%2Fsequel-pg_bulk_upsert/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279006216,"owners_count":26084062,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-15T05:26:26.314Z","updated_at":"2025-10-11T04:48:13.026Z","avatar_url":"https://github.com/restorando.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sequel::PgBulkUpsert\n\nThis Sequel extension implements `on_duplicate_key_update(*...).multi_insert(*...)` for postgresql. The\nsyntax is 100% with the one that Sequel provides for mysql with a couple of caveats.\n\nThere are different ways for implementing \"upsert\" in postgresql, some of them discussed in:\n\nhttp://johtopg.blogspot.com.ar/2014/04/upsertisms-in-postgres.html\n\nBecause we aim to do bulk upserts, this extension uses the one discribed here:\n\nhttp://tapoueh.org/blog/2013/03/15-batch-update\n\nIt consists in 3 parts:\n\n1. Create a temp table with the target table structure\n\n2. Fill the temp table with the rows that are going to be \"upserted\"\n\n3. Updates the records that exist in the target table with the temp table information, then, by doing an\n   \"anti-join\" between the temp table and the target table, the records that didn't exist are inserted.\n\n   The resulting SQL looks like this:\n\n   ```SQL\n      WITH \"update_cte\" AS\n        (UPDATE \"\u003ctarget_table\u003e\"\n            SET \"updatable_column\" = \"\u003ctemp_table\u003e\".\"updatable_column\"\n           FROM \"\u003ctemp_table\u003e\"\n           WHERE (\"\u003ctarget_table\u003e\".\"id\" = \"\u003ctemp_table\u003e\".\"id\")\n           RETURNING \"\u003ctarget_table\u003e\".\"id\")\n      INSERT INTO \"\u003ctarget_table\u003e\" (\"updatable_column\", \"insertable_column\")\n       SELECT \"updatable_column\", \"insertable_column\"\n       FROM \"\u003ctemp_table\u003e\" LEFT JOIN \"update_cte\" USING (\"id\")\n       WHERE (\"update_cte\".\"id\" IS NULL)\n       RETURNING \"\u003ctarget_table\u003e\".\"id\"\n   ```\n\n## Caveats\n\n1. The target table **must** have a primary key (this is the key used to decide when a record \"exist\"), it\n   won't work for any other unique constraint in the target table.\n\n2. This strategy for upsert isn't suitable for concurrent upserts on the same table. Because the concurrent\n  inserts will see the same table, and neither will do an update on the others records, which will result in\n  a duplicate key error.\n\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'sequel-pg_bulk_upsert'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install sequel-pg_bulk_upsert\n\n## Usage\n\n```ruby\nrequire 'sequel/pg_bulk_upsert'\n\n# This can also be done for a specify dataset\nDB.extension(:pg_bulk_upsert)\n\nDB[:target].on_duplicate_key_update(:column1, :column2).multi_insert([\n  {column1: '1', column2: '2', column3: '3'},\n  {column1: '4', column2: '4', column3: '4'}\n])\n```\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frestorando%2Fsequel-pg_bulk_upsert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frestorando%2Fsequel-pg_bulk_upsert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frestorando%2Fsequel-pg_bulk_upsert/lists"}