* Applies a single insert in a single sub-transaction. */
| 770 | * Applies a single insert in a single sub-transaction. |
| 771 | */ |
| 772 | static bool |
| 773 | DoSingleInsertWithSubTxn(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 | /* |
no test coverage detected