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

Function command_compact

pg_documentdb/src/commands/compact.c:66–163  ·  view source on GitHub ↗

* command_compact implements the functionality of compact Database command * dbcommand/compact. */

Source from the content-addressed store, hash-verified

64 * dbcommand/compact.
65 */
66Datum
67command_compact(PG_FUNCTION_ARGS)
68{
69 pgbson *compactSpec = PG_GETARG_PGBSON(0);
70 if (!IsMetadataCoordinator())
71 {
72 StringInfo compactQueryCoordinator = makeStringInfo();
73 appendStringInfo(compactQueryCoordinator,
74 "SELECT %s.compact(%s::%s.bson)",
75 ApiSchemaNameV2,
76 quote_literal_cstr(
77 PgbsonToHexadecimalString(PG_GETARG_PGBSON(0))),
78 CoreSchemaNameV2);
79 DistributedRunCommandResult result = RunCommandOnMetadataCoordinator(
80 compactQueryCoordinator->data);
81 if (!result.success)
82 {
83 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR),
84 errmsg(
85 "Internal error while running compact in metadata coordinator %s",
86 text_to_cstring(result.response)),
87 errdetail_log(
88 "Internal error while running compact in metadata coordinator %s",
89 text_to_cstring(result.response))));
90 }
91 pgbson *response = PgbsonInitFromHexadecimalString(text_to_cstring(
92 result.response));
93 PG_RETURN_POINTER(response);
94 }
95
96 ReportFeatureUsage(FEATURE_COMMAND_COMPACT);
97
98 CompactArgs args;
99 memset(&args, 0, sizeof(CompactArgs));
100 ParseCompactCommandSpec(compactSpec, &args);
101
102 if (args.databaseName == NULL || args.collectionName == NULL)
103 {
104 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_BADVALUE),
105 errmsg(
106 "Invalid command compact specification, missing database or collection name")));
107 }
108
109 /*
110 * VACUUM FULL is a blocking operation and it takes AccessExclusiveLock on the table.
111 * Also we can only execute it on the top level (not within a function and procedure tranasction), so we can't
112 * take any lock on the collection here to avoid deadlock situation.
113 */
114 MongoCollection *collection = GetMongoCollectionByNameDatum(CStringGetTextDatum(
115 args.databaseName),
116 CStringGetTextDatum(
117 args.collectionName),
118 NoLock);
119
120 if (collection == NULL)
121 {
122 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_NAMESPACENOTFOUND),
123 errmsg("ns does not exist: %s.%s", args.databaseName,

Callers

nothing calls this directly

Calls 14

IsMetadataCoordinatorFunction · 0.85
ReportFeatureUsageFunction · 0.85
ParseCompactCommandSpecFunction · 0.85
PgbsonWriterInitFunction · 0.85
PgbsonWriterAppendDoubleFunction · 0.85
PgbsonWriterAppendInt64Function · 0.85

Tested by

no test coverage detected