Files
documentdb/pg_helio_api/include/commands/insert.h
T
Vinod Sridharan df093213ad Merged PR 1511148: [Upsert] On Update with Upsert, convert the final insert to INSERT ON CONFLICT UPDATE
### Does this PR have any customer impact?
Yes
### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Bugfix
### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No
### Are you introducing any new config? If yes, do you have tests with and without them being set?
No
### Release Note (Refer [Template](../docs/release_notes/pgmongo_template.md))

### Description
On Update with Upsert, convert the final insert to INSERT ON CONFLICT UPDATE
This is to handle a race condition where two threads do SELECT for update -> don't find the doc -> one does the insert, and the other will get a unique conflict when it should just succeed.

----
#### AI description  (iteration 1)
#### PR Classification
New feature

#### PR Summary
This pull request introduces a new feature to handle upserts by converting the final insert to `INSERT ON CONFLICT UPDATE`.
- `oss/pg_helio_api/src/commands/insert.c`: Added `InsertOrUpdateDocument` function to handle upserts.
- `oss/pg_helio_api/src/commands/update.c`: Updated functions to use `InsertOrUpdateDocument` for upsert operations.
- `oss/pg_helio_api/include/commands/insert.h`: Declared the new `InsertOrUpdateDocument` function.
- `oss/internal/pg_helio_distributed/src/test/regress/expected/commands_update.out` and `pgmongo/src/test/helio_compat/expected/commands_update.out`: Added tests for the new upsert behavior.
- `oss/internal/pg_helio_distributed/src/test/regress/sql/commands_update.sql`: Added SQL tests for upsert functionality.
2024-11-19 17:25:36 +08:00

30 lines
1.0 KiB
C

/*-------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* include/commands/insert.h
*
* Functions for inserting documents.
*
*-------------------------------------------------------------------------
*/
#ifndef COMMANDS_INSERT_H
#define COMMANDS_INSERT_H
#include <io/helio_bson_core.h>
#include "commands/commands_common.h"
MongoCollection * CreateCollectionForInsert(Datum databaseNameDatum,
Datum collectionNameDatum);
bool InsertDocument(uint64 collectionId, const char *shardTableName, int64 shardKeyValue,
pgbson *objectId, pgbson *document);
bool InsertOrReplaceDocument(uint64 collectionId, const char *shardTableName, int64
shardKeyValue,
pgbson *objectId, pgbson *document);
bool InsertDocumentToTempCollection(MongoCollection *collection, int64 shardKeyValue,
pgbson *document);
bool TryInsertOne(MongoCollection *collection, pgbson *document, int64 shardKeyHash, bool
sameSourceAndTarget, WriteError *writeError);
#endif