Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres UPSERT native support #4132

Closed
lefnire opened this issue Jul 16, 2015 · 16 comments · Fixed by #12301
Closed

Postgres UPSERT native support #4132

lefnire opened this issue Jul 16, 2015 · 16 comments · Fixed by #12301
Labels
dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). type: refactor For issues and PRs. Things that improve the code readability, maintainability, testability, etc.

Comments

@lefnire
Copy link

lefnire commented Jul 16, 2015

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

@mickhansen mickhansen added the type: feature For issues and PRs. For new features. Never breaking changes. label Jul 16, 2015
@mickhansen
Copy link
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.

@lefnire
Copy link
Author

lefnire commented Jul 16, 2015

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

@janmeier
Copy link
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

@janmeier
Copy link
Member

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

@janmeier janmeier added the dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). label Aug 8, 2015
@lorenz
Copy link

lorenz commented Aug 29, 2015

+1

@thynson
Copy link

thynson commented Feb 29, 2016

Well, PostgreSQL 9.5 is released.

@ncwhale
Copy link

ncwhale commented May 27, 2016

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.)

@tsheaff
Copy link

tsheaff commented Nov 28, 2016

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.

@mickhansen
Copy link
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.

@tsheaff
Copy link

tsheaff commented Nov 28, 2016

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

@felixfbecker
Copy link
Contributor

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

@tsheaff
Copy link

tsheaff commented Nov 28, 2016

PR is already out @felixfbecker here: #6325

Waiting on code review and merge

cuongdo pushed a commit to cuongdo/sequelize that referenced this issue Jan 25, 2017
cuongdo pushed a commit to cuongdo/sequelize that referenced this issue Jan 31, 2017
cuongdo pushed a commit to cuongdo/sequelize that referenced this issue Jan 31, 2017
cuongdo pushed a commit to cuongdo/sequelize that referenced this issue Jan 31, 2017
cuongdo pushed a commit to cuongdo/sequelize that referenced this issue Jan 31, 2017
@tsheaff
Copy link

tsheaff commented Feb 3, 2017

Seems like #6325 hasn't been worked on in over a month. Anyone know more around this issue?

@AlJohri
Copy link

AlJohri commented Mar 16, 2017

There is also #7174.

@haijianyang
Copy link

So, this issure when to be resolved?

@stale stale bot added the stale label Jun 29, 2017
@stale stale bot closed this as completed Jul 7, 2017
@sushantdhiman sushantdhiman reopened this Jan 20, 2018
@stale stale bot removed the stale label Jan 20, 2018
@tsheaff
Copy link

tsheaff commented Nov 1, 2018

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

@stale stale bot added the stale label Jul 23, 2019
@papb papb removed the stale label Jul 24, 2019
@sequelize sequelize deleted a comment from stale bot Jul 27, 2019
@papb papb added status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. type: refactor For issues and PRs. Things that improve the code readability, maintainability, testability, etc. and removed status: awaiting investigation type: feature For issues and PRs. For new features. Never breaking changes. labels Aug 17, 2019
@sushantdhiman sushantdhiman removed the status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. label May 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dialect: postgres For issues and PRs. Things that involve PostgreSQL (and do not involve all dialects). type: refactor For issues and PRs. Things that improve the code readability, maintainability, testability, etc.
Projects
None yet