Skip to content

Upsert with returning: true requires extra read from DB #12051

Closed

Description

@gabegorelick

Issue Description

Model.upsert accepts a returning option since #8924. However, the way this is implemented is via a platform-specific upsert that returns the record's primary key and then a subsequent SELECT call to fetch the record.

Relevant code:

sequelize/lib/model.js

Lines 2495 to 2501 in c66663e

.then(() => {
return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), this, options);
})
.then(([created, primaryKey]) => {
if (options.returning === true && primaryKey) {
return this.findByPk(primaryKey, options).then(record => [record, created]);
}

This is problematic for a few reasons:

  1. Extra round trip to database. This should be unnecessary on platforms that support returning the full record from UPDATE/INSERTs like Postgres.

  2. Subsequent read may hit a read replica instead: Upsert with returning:true and read replication returns null instance #9216

  3. Use of a non-repeatable read means upsert can return a different result than what was actually upserted. For example, on Postgres's default READ COMMITTED transaction isolation level, a transaction may "re-reads data it has previously read and finds that data has been modified by another transaction (that committed since the initial read)." The fact that the SELECT runs in the same transaction as the INSERT/UPDATE doesn't help. Thus, upsert isn't returning the record that was created or the record as it existed as of the update, it's returning the record that existed when the subsequent findByPk was executed. In an extreme case, the record could have even been deleted, which would mean upsert returns null.

What are you doing?

Sorry, no SSCCE since reproducing the non-repeatable read requires precise timing. It would probably require stubbing findByPk.

const [record] = Model.upsert({id: 1}, {returning: true});
// record may be null if the `destroy` call below ran in between the underlying update/insert and the subsequent findByPk

// concurrently
Model.destroy({where: {id: 1}});

What do you expect to happen?

upsert returns record that was inserted

What is actually happening?

upsert returns null

Environment

  • Sequelize version: master
  • Node.js version: n/a
  • Operating System: n/a

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. added
    hard
    on Apr 18, 2020
  2. sushantdhiman commented on Apr 18, 2020

    @sushantdhiman
    Contributor

    Yes I am aware of this issue. It is mainly because not all dialect support returning.

    With dialects like Postgres even though returning is supported I think we lack native upsert support, which forces use to use some syntax which can only return primary key

  3. added
    performance
    type: refactor
    on Apr 18, 2020
  4. gabegorelick commented on Apr 20, 2020

    @gabegorelick
    ContributorAuthor

    we lack native upsert support

    That makes sense. Here is the issue for native upsert in Postgres: #4132

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

    hardperformancetype: refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions