MCPcopy Create free account
hub / github.com/documentdb/documentdb / DoSingleInsertWithSubTxn

Function DoSingleInsertWithSubTxn

pg_documentdb/src/commands/insert.c:772–824  ·  view source on GitHub ↗

* Applies a single insert in a single sub-transaction. */

Source from the content-addressed store, hash-verified

770 * Applies a single insert in a single sub-transaction.
771 */
772static bool
773DoSingleInsertWithSubTxn(MongoCollection *collection,
774 Oid insertShardOid,
775 const bson_value_t *document,
776 text *transactionId,
777 BatchInsertionResult *batchResult, int insertIndex,
778 ExprEvalState *evalState)
779{
780 /* declared volatile because of the longjmp in PG_CATCH */
781 volatile bool isSuccess = false;
782 volatile uint64 numDocsInserted = 0;
783
784 /* use a subtransaction to correctly handle failures */
785 MemoryContext oldContext = CurrentMemoryContext;
786 ResourceOwner oldOwner = CurrentResourceOwner;
787 BeginInternalSubTransaction(NULL);
788
789 PG_TRY();
790 {
791 numDocsInserted = ProcessInsertion(collection, insertShardOid, document,
792 transactionId, evalState);
793
794 /* Commit the inner transaction, return to outer xact context */
795 ReleaseCurrentSubTransaction();
796 MemoryContextSwitchTo(oldContext);
797 CurrentResourceOwner = oldOwner;
798 batchResult->rowsInserted += numDocsInserted;
799 isSuccess = true;
800 }
801 PG_CATCH();
802 {
803 MemoryContextSwitchTo(oldContext);
804 ErrorData *errorData = CopyErrorDataAndFlush();
805
806 /* Abort inner transaction */
807 RollbackAndReleaseCurrentSubTransaction();
808 CurrentResourceOwner = oldOwner;
809 if (IsOperatorInterventionError(errorData))
810 {
811 ReThrowError(errorData);
812 }
813
814 MemoryContextSwitchTo(batchResult->resultMemoryContext);
815 batchResult->writeErrors = lappend(batchResult->writeErrors,
816 GetWriteErrorFromErrorData(errorData,
817 insertIndex));
818 MemoryContextSwitchTo(oldContext);
819 FreeErrorData(errorData);
820 isSuccess = false;
821 }
822 PG_END_TRY();
823 return isSuccess;
824}
825
826
827/*

Callers 2

ProcessBatchInsertionFunction · 0.85

Calls 4

ProcessInsertionFunction · 0.85
CopyErrorDataAndFlushFunction · 0.85

Tested by

no test coverage detected