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

Ignore conflicting concurrent storage writes of the same data with version="*" #1118

Closed
wants to merge 6 commits into from
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions server/core_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,15 @@ func storagePrepBatch(batch *pgx.Batch, authoritativeWrite bool, op *StorageOpWr
// Existing permission checks are not applicable for new storage objects.
query = `
INSERT INTO storage (collection, key, user_id, value, version, read, write, create_time, update_time)
VALUES ($1, $2, $3, $4, $5, $6, $7, now(), now())
SELECT $1, $2, $3, $4, $5, $6, $7, now(), now()
WHERE NOT EXISTS(SELECT 1 FROM storage WHERE collection = $1 AND key = $2 AND user_id = $3) -- avoids ON CONFLICT hitting pre-existing object, and immediately cancels insert
ON CONFLICT (collection, key, user_id) DO UPDATE
redbaron marked this conversation as resolved.
Show resolved Hide resolved
SET version = excluded.version WHERE storage.version = excluded.version AND storage.read = excluded.read AND storage.write = excluded.write -- needed to return a row on concurrent write of same object
RETURNING read, write, version, create_time, update_time`

// Outcomes:
// - NoRows - insert failed due to constraint violation (concurrent insert)
// - Row is returned if object was new or if concurrent writer wrote object with the same values (ON CONFLICT UPDATE needed)
// - NoRows - insert was ignored due to constraint violation (non-OCC already existing row), or concurrent write wrote object with different values (UPDATE did not happen)
}

batch.Queue(query, params...)
Expand Down
Loading