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

Function UpdateDocumentByTID

pg_documentdb/src/commands/update.c:3705–3762  ·  view source on GitHub ↗

* UpdateDocumentByTID performs a TID update on a single shard. * * The TID must be obtained via SelectUpdateCandidate in the current transaction. */

Source from the content-addressed store, hash-verified

3703 * The TID must be obtained via SelectUpdateCandidate in the current transaction.
3704 */
3705static bool
3706UpdateDocumentByTID(uint64 collectionId, const char *shardTableName,
3707 int64 shardKeyHash, ItemPointer tid,
3708 pgbson *updatedDocument)
3709{
3710 StringInfoData updateQuery;
3711 int argCount = 3;
3712 Oid argTypes[3];
3713 Datum argValues[3];
3714
3715 /* whitespace means not null, n means null */
3716 char argNulls[3] = { ' ', ' ', ' ' };
3717
3718 SPI_connect();
3719
3720 initStringInfo(&updateQuery);
3721
3722 appendStringInfo(&updateQuery, "UPDATE ");
3723
3724 if (shardTableName != NULL && shardTableName[0] != '\0')
3725 {
3726 appendStringInfo(&updateQuery, "%s.%s", ApiDataSchemaName, shardTableName);
3727 }
3728 else
3729 {
3730 appendStringInfo(&updateQuery, "%s.documents_" UINT64_FORMAT,
3731 ApiDataSchemaName, collectionId);
3732 }
3733
3734 appendStringInfo(&updateQuery,
3735 " SET document = $3::%s"
3736 " WHERE ctid = $2 AND shard_key_value = $1",
3737 FullBsonTypeName);
3738
3739 argTypes[0] = INT8OID;
3740 argValues[0] = Int64GetDatum(shardKeyHash);
3741
3742 argTypes[1] = TIDOID;
3743 argValues[1] = ItemPointerGetDatum(tid);
3744
3745 /* we use bytea because bson may not have the same OID on all nodes */
3746 argTypes[2] = BYTEAOID;
3747 argValues[2] = PointerGetDatum(CastPgbsonToBytea(updatedDocument));
3748
3749 bool readOnly = false;
3750 long maxTupleCount = 0;
3751
3752 SPIPlanPtr plan = GetSPIQueryPlanWithLocalShard(collectionId, shardTableName,
3753 QUERY_ID_UPDATE_BY_TID,
3754 updateQuery.data, argTypes, argCount);
3755
3756 SPI_execute_plan(plan, argValues, argNulls, readOnly, maxTupleCount);
3757 Assert(SPI_processed == 1);
3758
3759 SPI_finish();
3760
3761 return true;
3762}

Callers 1

UpdateOneInternalFunction · 0.85

Calls 2

CastPgbsonToByteaFunction · 0.85

Tested by

no test coverage detected