Files
documentdb/pg_documentdb/include/commands/commands_common.h
T
Vinod Sridharan 36bb3e2920 Merged PR 1650400: [Bulk Update] Setup bulk update infrastructure
### Does this PR have any customer impact?
No
### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
Feature
### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
Yes
### Are you introducing any new config? If yes, do you have tests with and without them being set?
No
### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

### Description
Currently bulk updates run as a function which means that the entire execution runs as a single transaction. Each batch update is handled as a subtransaction and rolled back to a save point if it fails.
This means that document locks are held for longer, and we can hit limits w.r.t. max documents updated per transaction.
In multi-node setups this also means distributed transactions are held longer causing loss of connection pools.

This change introduces a stored procedure for bulk update that periodically "Commits" the state to WAL and starts a new transaction. This more mirrors what the non-atomic bulk update is supposed to do. The old function is retained to be used within transactions.

Still TODO:
For unsharded collections in a multi-node setup, we need to send batches of updates to the worker. This is not wired up in this change.

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

#### PR Summary
This pull request introduces a new bulk update feature for handling non-transactional updates in the MongoDB wire-protocol. It adds new functions and procedures to support bulk updates and modifies existing update logic to accommodate this new functionality.
- Added `update_bulk` procedure and related functions in `sql/udfs/commands_crud/update--0.103-0.sql` for non-transactional bulk updates.
- Implemented `command_update_bulk` in `src/commands/update.c` to handle bulk updates outside of transactions.
- Enhanced `ProcessBatchUpdateCore` in `src/commands/update.c` to support transactional and non-transactional updates.
- Updated feature tracking in `include/utils/feature_counter.h` and `src/infrastructure/feature_counter.c` to include `FEATURE_COMMAND_UPDATE_BULK`.
- Modified SQL scripts `documentdb--0.102-1--0.103-0.sql` and `pgmongo--1.102-1--1.103-0.sql` to include the new update functionality.
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
2025-04-30 14:57:09 +08:00

90 lines
2.7 KiB
C

/*-------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* include/commands/commands_common.h
*
* Common declarations of Mongo commands.
*
*-------------------------------------------------------------------------
*/
#ifndef COMMANDS_COMMON_H
#define COMMANDS_COMMON_H
#include <utils/elog.h>
#include <metadata/collection.h>
#include <io/bson_core.h>
/*
* Maximum size of a output bson document is 16MB.
*/
#define BSON_MAX_ALLOWED_SIZE (16 * 1024 * 1024)
/*
* Maximum size of document produced by an intermediate stage of an aggregation pipeline.
* This is a Native Mongo constrains. For example, if the pipeline is [$facet, $unwind],
* $facet is allowed to generate document that is larger than 16MB, since $unwind can
* break them into smaller document. However, $facet is not allowed to generate a document
* that's larger than 100MB.
*/
#define BSON_MAX_ALLOWED_SIZE_INTERMEDIATE (100 * 1024 * 1024)
/* StringView that represents the _id field */
extern PGDLLIMPORT const StringView IdFieldStringView;
/*
* ApiGucPrefix.enable_create_collection_on_insert GUC determines whether
* an insert into a non-existent collection should create a collection.
*/
extern bool EnableCreateCollectionOnInsert;
/*
* Whether or not write operations are inlined or if they are dispatched
* to a remote shard. For single node scenarios like DocumentDB that don't need
* distributed dispatch. Reset in scenarios that need distributed dispatch.
*/
extern bool DefaultInlineWriteOperations;
extern int BatchWriteSubTransactionCount;
extern int MaxWriteBatchSize;
/*
* WriteError can be part of the response of a batch write operation.
*/
typedef struct WriteError
{
/* index in a write batch */
int index;
/* error code */
int code;
/* description of the error */
char *errmsg;
} WriteError;
bool FindShardKeyValueForDocumentId(MongoCollection *collection, pgbson *queryDoc,
bson_value_t *objectId, int64 *shardKeyValue);
bool IsCommonSpecIgnoredField(const char *fieldName);
WriteError * GetWriteErrorFromErrorData(ErrorData *errorData, int writeErrorIdx);
bool TryGetErrorMessageAndCode(ErrorData *errorData, int *code, char **errmessage);
pgbson * GetObjectIdFilterFromQueryDocument(pgbson *queryDoc, bool *hasNonIdFields);
pgbson * RewriteDocumentAddObjectId(pgbson *document);
pgbson * RewriteDocumentValueAddObjectId(const bson_value_t *value);
pgbson * RewriteDocumentWithCustomObjectId(pgbson *document,
pgbson *objectIdToWrite);
void ValidateIdField(const bson_value_t *idValue);
void SetExplicitStatementTimeout(int timeoutMilliseconds);
void CommitWriteProcedureAndReacquireCollectionLock(MongoCollection *collection,
bool setSnapshot);
#endif