mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-25 19:59:54 -05:00
### Does this PR have any customer impact? ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) ### Are you introducing any new config? If yes, do you have tests with and without them being set? ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description Currently bulk insert run as a function which means that the entire execution runs as a single transaction. Each batch insert 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 insert 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. ---- #### AI description (iteration 1) #### PR Classification Performance improvement for write commands. #### PR Summary This pull request introduces a new procedure for handling write commands, enhancing performance by optimizing user-specific command execution. It includes the addition of new functions and procedures to manage MongoDB operations more efficiently. - Added new SQL functions and procedures in `/pgmongo/sql/udfs/gateway_funcs/command_funcs--1.103-0.sql` for running commands, queries, DDL, and write operations as a specific user. - Introduced new insert functions and procedures in `/oss/pg_documentdb/sql/udfs/commands_crud/insert--0.103-0.sql` to handle MongoDB insert operations. - Implemented `pgmongo_run_write_procedure_as_user` and `ExecuteWriteCommandCore` in `/pgmongo/src/infrastructure/command_funcs.c` to manage write commands with user context. - Added `CommitOuterInsertTransactions` function in `/oss/pg_documentdb/src/commands/insert.c` to handle transaction commits outside of transaction blocks. - Introduced a feature flag `EnableWriteProcedure` in `/oss/pg_documentdb/src/configs/feature_flag_configs.c` to control the enabling of write procedures. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
55 lines
2.2 KiB
SQL
55 lines
2.2 KiB
SQL
|
|
/*
|
|
* processes a Mongo insert wire protocol command.
|
|
*/
|
|
CREATE OR REPLACE FUNCTION __API_SCHEMA_V2__.insert(
|
|
p_database_name text,
|
|
p_insert __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
|
|
AS 'MODULE_PATHNAME', $$command_insert$$;
|
|
COMMENT ON FUNCTION __API_SCHEMA_V2__.insert(text,__CORE_SCHEMA_V2__.bson,__CORE_SCHEMA_V2__.bsonsequence,text)
|
|
IS 'inserts documents into a collection for a mongo wire protocol command';
|
|
|
|
/* Command: insert */
|
|
CREATE OR REPLACE FUNCTION __API_SCHEMA_INTERNAL_V2__.insert_one(
|
|
p_collection_id bigint,
|
|
p_shard_key_value bigint,
|
|
p_document __CORE_SCHEMA_V2__.bson,
|
|
p_transaction_id text)
|
|
RETURNS bool
|
|
LANGUAGE C
|
|
AS 'MODULE_PATHNAME', $$command_insert_one$$;
|
|
COMMENT ON FUNCTION __API_SCHEMA_INTERNAL_V2__.insert_one(bigint,bigint,__CORE_SCHEMA_V2__.bson,text)
|
|
IS 'internal command to insert one document into a collection';
|
|
|
|
|
|
/* Command: insert */
|
|
CREATE OR REPLACE FUNCTION __API_SCHEMA_INTERNAL_V2__.insert_worker(
|
|
p_collection_id bigint,
|
|
p_shard_key_value bigint,
|
|
p_shard_oid regclass,
|
|
p_insert_internal_spec __CORE_SCHEMA_V2__.bson,
|
|
p_insert_internal_docs __CORE_SCHEMA_V2__.bsonsequence,
|
|
p_transaction_id text)
|
|
RETURNS __CORE_SCHEMA_V2__.bson
|
|
LANGUAGE C
|
|
AS 'MODULE_PATHNAME', $$command_insert_worker$$;
|
|
|
|
/* insert procedure will use when not called inside transaction */
|
|
CREATE OR REPLACE PROCEDURE __API_SCHEMA_V2__.insert_bulk(
|
|
IN p_database_name text,
|
|
IN p_insert __CORE_SCHEMA_V2__.bson,
|
|
IN p_insert_documents __CORE_SCHEMA_V2__.bsonsequence default NULL,
|
|
IN p_transaction_id text default NULL,
|
|
INOUT p_result __CORE_SCHEMA_V2__.bson default NULL,
|
|
INOUT p_success boolean default NULL)
|
|
LANGUAGE C
|
|
AS 'MODULE_PATHNAME', $$command_insert_bulk$$;
|
|
COMMENT ON PROCEDURE __API_SCHEMA_V2__.insert_bulk(text,__CORE_SCHEMA_V2__.bson,__CORE_SCHEMA_V2__.bsonsequence,text,__CORE_SCHEMA_V2__.bson,boolean)
|
|
IS 'inserts documents into a collection for a mongo wire protocol command outside transaction';
|