Files
documentdb/pg_documentdb/sql/udfs/commands_crud/update--latest.sql
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

84 lines
3.1 KiB
SQL

/*
* __API_SCHEMA_V2__.update processes a Mongo update wire-protocol command.
*/
CREATE OR REPLACE FUNCTION __API_SCHEMA_V2__.update(
p_database_name text,
p_update __CORE_SCHEMA_V2__.bson,
p_insert_documents __CORE_SCHEMA_V2__.bsonsequence default NULL,
p_transaction_id text default NULL,
p_result OUT __CORE_SCHEMA_V2__.bson,
p_success OUT boolean)
RETURNS record
LANGUAGE C
VOLATILE
AS 'MODULE_PATHNAME', $$command_update$$;
COMMENT ON FUNCTION __API_SCHEMA_V2__.update(text,__CORE_SCHEMA_V2__.bson,__CORE_SCHEMA_V2__.bsonsequence,text)
IS 'update documents in a collection';
CREATE OR REPLACE PROCEDURE __API_SCHEMA_V2__.update_bulk(
p_database_name text,
p_update __CORE_SCHEMA_V2__.bson,
p_insert_documents __CORE_SCHEMA_V2__.bsonsequence DEFAULT NULL,
p_transaction_id text DEFAULT NULL,
p_result INOUT __CORE_SCHEMA_V2__.bson DEFAULT NULL,
p_success INOUT boolean DEFAULT NULL)
LANGUAGE C
AS 'MODULE_PATHNAME', $$command_update_bulk$$;
COMMENT ON PROCEDURE __API_SCHEMA_V2__.update_bulk(text,__CORE_SCHEMA_V2__.bson,__CORE_SCHEMA_V2__.bsonsequence,text,__CORE_SCHEMA_V2__.bson,boolean)
IS 'update documents in a collection in a non-transactional manner';
/* Command: update */
CREATE OR REPLACE FUNCTION __API_SCHEMA_INTERNAL_V2__.update_one(
p_collection_id bigint,
p_shard_key_value bigint,
p_query __CORE_SCHEMA_V2__.bson,
p_update __CORE_SCHEMA_V2__.bson,
p_shard_key __CORE_SCHEMA_V2__.bson,
p_is_upsert bool,
p_sort __CORE_SCHEMA_V2__.bson,
/*
* p_return_old_or_new: see update.c/UpdateReturnValue enum:
*
* NULL -> do not return
* false -> return old document
* true -> return new document
*/
p_return_old_or_new bool,
p_return_fields __CORE_SCHEMA_V2__.bson,
p_array_filters __CORE_SCHEMA_V2__.bson,
p_transaction_id text,
OUT o_is_row_updated bool,
OUT o_update_skipped bool,
OUT o_is_retry bool,
OUT o_reinsert_document __CORE_SCHEMA_V2__.bson,
OUT o_upserted_object_id bytea,
OUT o_result_document __CORE_SCHEMA_V2__.bson)
RETURNS record
LANGUAGE C
AS 'MODULE_PATHNAME', $$command_update_one$$;
COMMENT ON FUNCTION __API_SCHEMA_INTERNAL_V2__.update_one(bigint,bigint,__CORE_SCHEMA_V2__.bson,__CORE_SCHEMA_V2__.bson,__CORE_SCHEMA_V2__.bson,bool,__CORE_SCHEMA_V2__.bson,bool,__CORE_SCHEMA_V2__.bson,__CORE_SCHEMA_V2__.bson,text)
IS 'updates a single document in a collection';
/* Command: update_worker */
CREATE OR REPLACE FUNCTION __API_SCHEMA_INTERNAL_V2__.update_worker(
p_collection_id bigint,
p_shard_key_value bigint,
p_shard_oid regclass,
p_update_internal_spec __CORE_SCHEMA_V2__.bson,
p_update_internal_docs __CORE_SCHEMA_V2__.bsonsequence,
p_transaction_id text)
RETURNS __CORE_SCHEMA_V2__.bson
LANGUAGE C
AS 'MODULE_PATHNAME', $$command_update_worker$$;
/* Helper function for multi-update to track number of updated documents */
CREATE OR REPLACE FUNCTION __API_SCHEMA_INTERNAL_V2__.bson_update_returned_value(shard_key_id bigint)
RETURNS int
LANGUAGE c
IMMUTABLE PARALLEL SAFE STRICT
AS 'MODULE_PATHNAME', $function$bson_update_returned_value$function$;