* command_compact implements the functionality of compact Database command * dbcommand/compact. */
| 64 | * dbcommand/compact. |
| 65 | */ |
| 66 | Datum |
| 67 | command_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, |
nothing calls this directly
no test coverage detected