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

Function CreateCollectionForInsert

pg_documentdb/src/commands/insert.c:240–280  ·  view source on GitHub ↗

* CreateCollectionForInsert creates a new collection and takes the approriate * lock for insert, or errors if we disabled automatic creation creation. */

Source from the content-addressed store, hash-verified

238 * lock for insert, or errors if we disabled automatic creation creation.
239 */
240MongoCollection *
241CreateCollectionForInsert(Datum databaseNameDatum, Datum collectionNameDatum)
242{
243 /*
244 * If the collection does not exist, the client might prefer to handle that.
245 *
246 * This is primarily relevant when routing inserts via worker nodes. As long
247 * as Citus does not support create_distributed_table via worker nodes, we
248 * need to fall back to doing the insert via the coordinator when we get
249 * this error.
250 */
251 if (!EnableCreateCollectionOnInsert)
252 {
253 char *collectionName = TextDatumGetCString(collectionNameDatum);
254
255 ereport(ERROR, (errcode(ERRCODE_UNDEFINED_TABLE),
256 errmsg("The collection named %s cannot be found",
257 quote_literal_cstr(collectionName))));
258 }
259
260 /*
261 * Call ApiSchemaName.create_collection. It internally handles concurrent
262 * calls in an idempotent manner, which means that if a concurrent insert raced
263 * to create the collection before us, this will be a noop.
264 */
265 CreateCollection(databaseNameDatum, collectionNameDatum);
266
267 MongoCollection *collection = GetMongoCollectionByNameDatum(databaseNameDatum,
268 collectionNameDatum,
269 RowExclusiveLock);
270
271 if (collection == NULL)
272 {
273 ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR),
274 errmsg("Unable to create the specified collection"),
275 errdetail_log(
276 "Could not get collection from cache after creating the collection")));
277 }
278
279 return collection;
280}
281
282
283/*

Callers 5

CommandInsertCoreFunction · 0.85
command_find_and_modifyFunction · 0.85
PerformUpdateCoreFunction · 0.85
HandleMergeFunction · 0.85
HandleOutFunction · 0.85

Calls 2

CreateCollectionFunction · 0.85

Tested by

no test coverage detected