Skip to content

Postgres UPSERT native support #4132

Closed

Description

@lefnire

Looks like Postgres recently added UPSERT (9.5alpha is it?). Seems Sequelize handles psql upsert fine presently with its workaround, so obviously no rush.

Me personally (and maybe I need re-architecting), my current project's most common SQL operation is UPSERT. That operation seems quite slow; this is the resultant SQL which looks like it would be slow:

CREATE OR REPLACE FUNCTION pg_temp.sequelize_upsert() RETURNS integer AS $func$ BEGIN INSERT INTO ... VALUES ...; RETURN 1; EXCEPTION WHEN unique_violation THEN UPDATE ... SET ... WHERE ...; RETURN 2; END; $func$ LANGUAGE plpgsql; SELECT * FROM pg_temp.sequelize_upsert();

So I'd personally upgrade to Postgres 9.5 if native upsert becomes supported in sequelize

Activity

  1. added
    type: feature
    on Jul 16, 2015
  2. mickhansen commented on Jul 16, 2015

    @mickhansen
    Contributor

    We still need a proper way to detect features based on the server version, but yeah ideally we'd want to use the built in UPSERT when possible.

  3. lefnire commented on Jul 16, 2015

    @lefnire
    Author

    Possible sloppy short-term solution: pass version constructor options? Eg: new Sequelize('database', 'username', 'password', {..., version:'9.5'})

  4. janmeier commented on Jul 17, 2015

    @janmeier
    Member

    @mickhansen All the query work regarding version detection is actually already in place - As far as I can see we just need to actually select the verison, and find a good datastructure for storing which features are supported by what versions

  5. janmeier commented on Jul 17, 2015

    @janmeier
    Member

    One problem though is that pg 9.5 is not supported by travis - and probably won't be before its officially releasesd

  6. added
    dialect: postgres
    on Aug 8, 2015
  7. lorenz commented on Aug 29, 2015

    @lorenz

    +1

  8. thynson commented on Feb 29, 2016

    @thynson

    Well, PostgreSQL 9.5 is released.

  9. ncwhale commented on May 27, 2016

    @ncwhale

    Sometimes, just need update one signal col when insert failed, right now I'll do it like this:

    Models.Somemodel.create().then(_=>{do something here.}).catch(_=>{do something when fail});

    but with pg 9.5 , how about add a suger option when insert fail? like this

    Models.Somemodel.create({}, {conflict: { add another sub update/nothing query here}}).then(_=>{do something here.});

    It most like the upsert on another way, but with other db also can also use it as is.(by add a catch and do the sub query.)

  10. tsheaff commented on Nov 28, 2016

    @tsheaff

    Any updates on this @mickhansen ? Our team is currently looking at how to optimize our upserts, as the native PostgreSQL 9.5 upsert is much faster. We'd strongly prefer not to re-implement upsert ourselves as a native query.

  11. mickhansen commented on Nov 28, 2016

    @mickhansen
    Contributor

    @tsheaff No one is working on it, but version detection is in place so it should not be a lot of work (alternative syntax based on version detected). We have support for testing against 9.5 aswell.

  12. tsheaff commented on Nov 28, 2016

    @tsheaff

    Awesome thanks for quick reply @mickhansen Any rough estimate on timeline?

  13. felixfbecker commented on Nov 28, 2016

    @felixfbecker
    Contributor

    As he said, noone is working on it. PRs welcome :)

  14. tsheaff commented on Nov 28, 2016

    @tsheaff

    PR is already out @felixfbecker here: #6325

    Waiting on code review and merge

  15. added a commit that references this issue on Jan 25, 2017
    05b83a8
  16. 7 remaining items

  17. tsheaff commented on Nov 1, 2018

    @tsheaff

    This is a really stale issue. Is there any update on this?

  18. deleted a comment from stale on Jul 27, 2019
  19. added
    status: wip
    type: refactor
    and removed
    type: feature
    on Aug 17, 2019
  20. removed
    status: wip
    on May 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: postgrestype: refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions