mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-25 19:59:54 -05:00
Merged PR 1661768: [Perf] Write Optimization for new tables. Creating new tables with 3 columns
### Does this PR have any customer impact? ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) ### Are you introducing any new config? If yes, do you have tests with and without them being set? ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description 1. Creating New tables without creation_time column. This under a GUC. 2. support of 4 col table as well as 3 col table in all code path. ---- #### AI description (iteration 1) #### PR Classification Performance optimization for creating new tables with three columns. #### PR Summary This pull request introduces performance improvements for creating new tables with three columns in the document database. It includes tests for operations on both three-column and four-column tables. - Added `three_column_data_table.out` to test the creation and operations on tables with three columns, including insert, update, and aggregate operations. - Demonstrated the use of GUC (Grand Unified Configuration) to enable and disable creation time for data tables, affecting table operations. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
This commit is contained in:
1 parent
f6c9ecb9a7
commit
8233bf2fb3
10 files changed
+473
-49
No files matched your search
@@ -896,7 +896,7 @@ MakeActionWhenNotMatched(WhenNotMatchedAction whenNotMatched, Var *sourceDocVar,
|
||||
InvalidOid, COERCE_EXPLICIT_CALL);
|
||||
|
||||
/* for insert operation */
|
||||
action->targetList = list_make4(
|
||||
action->targetList = list_make3(
|
||||
makeTargetEntry((Expr *) sourceShardKeyVar,
|
||||
DOCUMENT_DATA_TABLE_SHARD_KEY_VALUE_VAR_ATTR_NUMBER,
|
||||
"target_shard_key_value", false),
|
||||
@@ -905,12 +905,18 @@ MakeActionWhenNotMatched(WhenNotMatchedAction whenNotMatched, Var *sourceDocVar,
|
||||
false),
|
||||
makeTargetEntry((Expr *) addObjecIdFuncExpr,
|
||||
DOCUMENT_DATA_TABLE_DOCUMENT_VAR_ATTR_NUMBER, "document",
|
||||
false),
|
||||
makeTargetEntry((Expr *) nowValue,
|
||||
targetCollection->mongoDataCreationTimeVarAttrNumber,
|
||||
"creation_time",
|
||||
false));
|
||||
|
||||
if (targetCollection->mongoDataCreationTimeVarAttrNumber != -1)
|
||||
{
|
||||
action->targetList = lappend(action->targetList,
|
||||
makeTargetEntry((Expr *) nowValue,
|
||||
targetCollection->
|
||||
mongoDataCreationTimeVarAttrNumber,
|
||||
"creation_time",
|
||||
false));
|
||||
}
|
||||
|
||||
return action;
|
||||
}
|
||||
|
||||
|
||||
@@ -6456,8 +6456,8 @@ GenerateBaseTableQuery(text *databaseDatum, const StringView *collectionNameView
|
||||
RangeTblEntry *rte = makeNode(RangeTblEntry);
|
||||
|
||||
/* Match spec for ApiSchema.collection() function */
|
||||
List *colNames = list_make4(makeString("shard_key_value"), makeString("object_id"),
|
||||
makeString("document"), makeString("creation_time"));
|
||||
List *colNames = list_make3(makeString("shard_key_value"), makeString("object_id"),
|
||||
makeString("document"));
|
||||
|
||||
const char *collectionAlias = "collection";
|
||||
if (context->numNestedLevels > 0 || context->nestedPipelineLevel > 0)
|
||||
@@ -6470,6 +6470,8 @@ GenerateBaseTableQuery(text *databaseDatum, const StringView *collectionNameView
|
||||
|
||||
if (collection == NULL)
|
||||
{
|
||||
colNames = lappend(colNames, makeString("creation_time"));
|
||||
|
||||
/* Here: Special case, if the database is config, try to see if we can create a base
|
||||
* table out of the system metadata.
|
||||
*/
|
||||
@@ -6516,6 +6518,11 @@ GenerateBaseTableQuery(text *databaseDatum, const StringView *collectionNameView
|
||||
rte->rtekind = RTE_RELATION;
|
||||
rte->relid = collection->relationId;
|
||||
|
||||
if (collection->mongoDataCreationTimeVarAttrNumber != -1)
|
||||
{
|
||||
colNames = lappend(colNames, makeString("creation_time"));
|
||||
}
|
||||
|
||||
if (context->allowShardBaseTable)
|
||||
{
|
||||
Oid shardOid = TryGetCollectionShardTable(collection, AccessShareLock);
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
|
||||
extern bool EnableNativeColocation;
|
||||
extern bool EnableNativeTableColocation;
|
||||
extern bool EnableDataTableWithoutCreationTime;
|
||||
|
||||
static bool CanColocateAtDatabaseLevel(text *databaseDatum);
|
||||
static const char * CreatePostgresDataTable(uint64_t collectionId,
|
||||
@@ -226,13 +227,20 @@ CreatePostgresDataTable(uint64_t collectionId, const char *colocateWith, const
|
||||
* defined in collection.h if you decide changing definiton
|
||||
* or position of document column.
|
||||
*/
|
||||
"document %s.bson not null,"
|
||||
|
||||
/* creation time (for TTL) */
|
||||
"creation_time timestamptz"
|
||||
")", dataTableNameInfo->data,
|
||||
"document %s.bson not null",
|
||||
dataTableNameInfo->data,
|
||||
CoreSchemaName, CoreSchemaName);
|
||||
|
||||
/* Let's add creation_time column only when EnableDataTableWithoutCreationTime GUC is off */
|
||||
if (EnableDataTableWithoutCreationTime)
|
||||
{
|
||||
appendStringInfo(createTableStringInfo, ")");
|
||||
}
|
||||
else
|
||||
{
|
||||
appendStringInfo(createTableStringInfo, ", creation_time timestamptz)");
|
||||
}
|
||||
|
||||
bool readOnly = false;
|
||||
bool isNull = false;
|
||||
int shardCount = 0;
|
||||
|
||||
@@ -129,6 +129,9 @@ static uint64 CallInsertWorkerForInsertOne(MongoCollection *collection, int64
|
||||
pgbson *document, text *transactionId);
|
||||
static Datum CommandInsertCore(PG_FUNCTION_ARGS, bool isTransactional, MemoryContext
|
||||
allocContext);
|
||||
static inline List * CreateValuesListForInsert(Const *shardKey, Expr *objectId,
|
||||
Expr *document, AttrNumber
|
||||
creationTimeVarAttNum);
|
||||
|
||||
/*
|
||||
* ApiGucPrefix.enable_create_collection_on_insert GUC determines whether
|
||||
@@ -538,13 +541,8 @@ DoMultiInsertWithoutTransactionId(MongoCollection *collection, List *inserts, Oi
|
||||
PG_TRY();
|
||||
{
|
||||
List *valuesList = NIL;
|
||||
|
||||
ListCell *insertCell;
|
||||
|
||||
TimestampTz nowValueTime = GetCurrentTimestamp();
|
||||
Const *nowValue = makeConst(TIMESTAMPTZOID, -1, InvalidOid, 8,
|
||||
TimestampTzGetDatum(nowValueTime), false, true);
|
||||
|
||||
/* Make params for all the BSONs - we have 2 per insert - objectId/insertDoc */
|
||||
int expectedNumParams = Min(list_length(inserts), BatchWriteSubTransactionCount);
|
||||
ParamListInfo paramListInfo = makeParamList(expectedNumParams * 2);
|
||||
@@ -571,8 +569,11 @@ DoMultiInsertWithoutTransactionId(MongoCollection *collection, List *inserts, Oi
|
||||
|
||||
Expr *documentParam = CreateBsonParam(paramIndex, paramListInfo, insertDoc);
|
||||
paramIndex++;
|
||||
List *values = list_make4(shardKeyConst, objectidParam, documentParam,
|
||||
nowValue);
|
||||
|
||||
List *values = CreateValuesListForInsert(shardKeyConst, objectidParam,
|
||||
documentParam,
|
||||
collection->
|
||||
mongoDataCreationTimeVarAttrNumber);
|
||||
|
||||
valuesList = lappend(valuesList, values);
|
||||
insertCount++;
|
||||
@@ -873,11 +874,12 @@ ProcessInsertion(MongoCollection *collection,
|
||||
Int64GetDatum(shardKeyHash), false, true);
|
||||
Expr *objectidParam = CreateBsonParam(0, paramListInfo, objectIdPtr);
|
||||
Expr *documentParam = CreateBsonParam(1, paramListInfo, insertDoc);
|
||||
TimestampTz nowValueTime = GetCurrentTimestamp();
|
||||
Const *nowValue = makeConst(TIMESTAMPTZOID, -1, InvalidOid, 8,
|
||||
TimestampTzGetDatum(nowValueTime), false, true);
|
||||
List *singleInsertList = list_make4(shardKeyConst, objectidParam, documentParam,
|
||||
nowValue);
|
||||
|
||||
List *singleInsertList = CreateValuesListForInsert(shardKeyConst, objectidParam,
|
||||
documentParam,
|
||||
collection->
|
||||
mongoDataCreationTimeVarAttrNumber);
|
||||
|
||||
Query *query = CreateInsertQuery(collection, optionalInsertShardOid, list_make1(
|
||||
singleInsertList));
|
||||
uint64_t insertResult = RunInsertQuery(query, paramListInfo);
|
||||
@@ -1345,13 +1347,17 @@ CreateInsertQuery(MongoCollection *collection, Oid shardOid, List *valuesLists)
|
||||
|
||||
/* Make the base table RTE */
|
||||
RangeTblEntry *rte = makeNode(RangeTblEntry);
|
||||
List *colNames = list_make4(makeString("shard_key_value"), makeString("object_id"),
|
||||
makeString("document"),
|
||||
makeString("creation_time"));
|
||||
List *colNames = list_make3(makeString("shard_key_value"), makeString("object_id"),
|
||||
makeString("document"));
|
||||
|
||||
/* If "creation_time" is the fifth column, then we should include "change_description" in the RTE. */
|
||||
if (collection->mongoDataCreationTimeVarAttrNumber == 5)
|
||||
|
||||
if (collection->mongoDataCreationTimeVarAttrNumber == 4)
|
||||
{
|
||||
colNames = lappend(colNames, makeString("creation_time"));
|
||||
}
|
||||
else if (collection->mongoDataCreationTimeVarAttrNumber == 5)
|
||||
{
|
||||
/* If "creation_time" is the fifth column, then we should include "change_description" in the RTE. */
|
||||
colNames = ModifyTableColumnNames(colNames);
|
||||
}
|
||||
|
||||
@@ -1385,10 +1391,15 @@ CreateInsertQuery(MongoCollection *collection, Oid shardOid, List *valuesLists)
|
||||
query->resultRelation = 1;
|
||||
|
||||
/* Make the VALUES RTE */
|
||||
List *valuesColNames = list_make4(makeString("shard_key_value"),
|
||||
List *valuesColNames = list_make3(makeString("shard_key_value"),
|
||||
makeString("object_id"),
|
||||
makeString("document"),
|
||||
makeString("creation_time"));
|
||||
makeString("document"));
|
||||
|
||||
if (collection->mongoDataCreationTimeVarAttrNumber != -1)
|
||||
{
|
||||
valuesColNames = lappend(valuesColNames, makeString("creation_time"));
|
||||
}
|
||||
|
||||
RangeTblEntry *valuesRte = makeNode(RangeTblEntry);
|
||||
valuesRte->rtekind = RTE_VALUES;
|
||||
valuesRte->alias = valuesRte->eref = makeAlias("values", valuesColNames);
|
||||
@@ -1398,11 +1409,20 @@ CreateInsertQuery(MongoCollection *collection, Oid shardOid, List *valuesLists)
|
||||
valuesRte->inh = false;
|
||||
valuesRte->inFromCl = true;
|
||||
|
||||
valuesRte->coltypes = list_make4_oid(INT8OID, BsonTypeId(), BsonTypeId(),
|
||||
TIMESTAMPTZOID);
|
||||
valuesRte->coltypmods = list_make4_int(-1, -1, -1, -1);
|
||||
valuesRte->colcollations = list_make4_oid(InvalidOid, InvalidOid, InvalidOid,
|
||||
InvalidOid);
|
||||
if (collection->mongoDataCreationTimeVarAttrNumber != -1)
|
||||
{
|
||||
valuesRte->coltypes = list_make4_oid(INT8OID, BsonTypeId(), BsonTypeId(),
|
||||
TIMESTAMPTZOID);
|
||||
valuesRte->coltypmods = list_make4_int(-1, -1, -1, -1);
|
||||
valuesRte->colcollations = list_make4_oid(InvalidOid, InvalidOid, InvalidOid,
|
||||
InvalidOid);
|
||||
}
|
||||
else
|
||||
{
|
||||
valuesRte->coltypes = list_make3_oid(INT8OID, BsonTypeId(), BsonTypeId());
|
||||
valuesRte->coltypmods = list_make3_int(-1, -1, -1);
|
||||
valuesRte->colcollations = list_make3_oid(InvalidOid, InvalidOid, InvalidOid);
|
||||
}
|
||||
query->rtable = lappend(query->rtable, valuesRte);
|
||||
|
||||
RangeTblRef *valuesRteRef = makeNode(RangeTblRef);
|
||||
@@ -1412,7 +1432,7 @@ CreateInsertQuery(MongoCollection *collection, Oid shardOid, List *valuesLists)
|
||||
query->jointree = makeFromExpr(fromList, NULL);
|
||||
|
||||
/* Now create the targetlist */
|
||||
query->targetList = list_make4(
|
||||
query->targetList = list_make3(
|
||||
makeTargetEntry((Expr *) makeVar(2, 1, INT8OID, -1, InvalidOid, 0),
|
||||
DOCUMENT_DATA_TABLE_SHARD_KEY_VALUE_VAR_ATTR_NUMBER,
|
||||
"shard_key_value", false),
|
||||
@@ -1421,12 +1441,19 @@ CreateInsertQuery(MongoCollection *collection, Oid shardOid, List *valuesLists)
|
||||
false),
|
||||
makeTargetEntry((Expr *) makeVar(2, 3, BsonTypeId(), -1, InvalidOid, 0),
|
||||
DOCUMENT_DATA_TABLE_DOCUMENT_VAR_ATTR_NUMBER, "document",
|
||||
false),
|
||||
makeTargetEntry((Expr *) makeVar(2, 4, TIMESTAMPTZOID, -1, InvalidOid, 0),
|
||||
collection->mongoDataCreationTimeVarAttrNumber, "creation_time",
|
||||
false)
|
||||
);
|
||||
|
||||
if (collection->mongoDataCreationTimeVarAttrNumber != -1)
|
||||
{
|
||||
query->targetList = lappend(query->targetList,
|
||||
makeTargetEntry((Expr *) makeVar(2, 4, TIMESTAMPTZOID,
|
||||
-1, InvalidOid, 0),
|
||||
collection->
|
||||
mongoDataCreationTimeVarAttrNumber,
|
||||
"creation_time", false));
|
||||
}
|
||||
|
||||
/* In order to use a portal & SPI we create a returning list of a const */
|
||||
query->returningList = list_make1(
|
||||
makeTargetEntry((Expr *) makeConst(INT4OID, -1, InvalidOid, 4, Int32GetDatum(1),
|
||||
@@ -1479,3 +1506,22 @@ RunInsertQuery(Query *insertQuery, ParamListInfo paramListInfo)
|
||||
|
||||
return numRowsProcessed;
|
||||
}
|
||||
|
||||
|
||||
/* indicates the presence of a creation_time column in the table, either at attribute number 4 or 5 */
|
||||
static inline List *
|
||||
CreateValuesListForInsert(Const *shardKey, Expr *objectId, Expr *document, AttrNumber
|
||||
creationTimeVarAttNum)
|
||||
{
|
||||
if (creationTimeVarAttNum != -1)
|
||||
{
|
||||
TimestampTz nowValueTime = (TimestampTz) 000000000000000LL; /* "2000-01-01 00:00:00+00" */
|
||||
Const *nowValue = makeConst(TIMESTAMPTZOID, -1, InvalidOid, 8,
|
||||
TimestampTzGetDatum(nowValueTime), false, true);
|
||||
return list_make4(shardKey, objectId, document, nowValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
return list_make3(shardKey, objectId, document);
|
||||
}
|
||||
}
|
||||
@@ -154,6 +154,10 @@ bool AlterCreationTimeInCompleteUpgrade = ALTER_CREATION_TIME_IN_COMPLETE_UPGRAD
|
||||
#define DEFAULT_ENABLE_USERNAME_PASSWORD_CONSTRAINTS true
|
||||
bool EnableUsernamePasswordConstraints = DEFAULT_ENABLE_USERNAME_PASSWORD_CONSTRAINTS;
|
||||
|
||||
#define DEFAULT_ENABLE_DATA_TABLES_WITHOUT_CREATION_TIME false
|
||||
bool EnableDataTableWithoutCreationTime =
|
||||
DEFAULT_ENABLE_DATA_TABLES_WITHOUT_CREATION_TIME;
|
||||
|
||||
/* Remove after v105 */
|
||||
#define DEFAULT_SKIP_ENFORCE_TRANSACTION_READ_ONLY false
|
||||
bool SkipEnforceTransactionReadOnly = DEFAULT_SKIP_ENFORCE_TRANSACTION_READ_ONLY;
|
||||
@@ -427,4 +431,12 @@ InitializeFeatureFlagConfigurations(const char *prefix, const char *newGucPrefix
|
||||
NULL, &SkipEnforceTransactionReadOnly,
|
||||
DEFAULT_SKIP_ENFORCE_TRANSACTION_READ_ONLY,
|
||||
PGC_USERSET, 0, NULL, NULL, NULL);
|
||||
|
||||
DefineCustomBoolVariable(
|
||||
psprintf("%s.enableDataTableWithoutCreationTime", newGucPrefix),
|
||||
gettext_noop(
|
||||
"Create data table without creation_time column."),
|
||||
NULL, &EnableDataTableWithoutCreationTime,
|
||||
DEFAULT_ENABLE_DATA_TABLES_WITHOUT_CREATION_TIME,
|
||||
PGC_USERSET, 0, NULL, NULL, NULL);
|
||||
}
|
||||
@@ -104,6 +104,7 @@ static const StringView SystemPrefix = { .length = 7, .string = "system." };
|
||||
extern bool UseLocalExecutionShardQueries;
|
||||
extern bool EnableSchemaValidation;
|
||||
extern int MaxSchemaValidatorSize;
|
||||
extern bool EnableDataTableWithoutCreationTime;
|
||||
|
||||
/* user-defined functions */
|
||||
PG_FUNCTION_INFO_V1(command_collection_table);
|
||||
@@ -1179,8 +1180,8 @@ GetMongoDataCreationTimeVarAttrNumber(Oid collectionOid)
|
||||
|
||||
if (!HeapTupleIsValid(tuple))
|
||||
{
|
||||
/* If collection doesn't exist, we'll arrive here */
|
||||
return (AttrNumber) 4;
|
||||
/* creation_time column is not present */
|
||||
return (AttrNumber) - 1;
|
||||
}
|
||||
|
||||
Form_pg_attribute targetatt = (Form_pg_attribute) GETSTRUCT(tuple);
|
||||
|
||||
@@ -1124,11 +1124,22 @@ ShardCollectionCore(ShardCollectionArgs *args)
|
||||
|
||||
insertArgValues[1] = UInt64GetDatum(collection->collectionId);
|
||||
|
||||
appendStringInfo(queryInfo,
|
||||
"INSERT INTO %s (shard_key_value, object_id, document, creation_time)"
|
||||
" SELECT %s.get_shard_key_value($1, $2, document), object_id, document, creation_time"
|
||||
" FROM %s",
|
||||
tmpDataTableName, ApiInternalSchemaName, qualifiedDataTableName);
|
||||
if (collection->mongoDataCreationTimeVarAttrNumber != -1)
|
||||
{
|
||||
appendStringInfo(queryInfo,
|
||||
"INSERT INTO %s (shard_key_value, object_id, document, creation_time)"
|
||||
" SELECT %s.get_shard_key_value($1, $2, document), object_id, document, creation_time"
|
||||
" FROM %s",
|
||||
tmpDataTableName, ApiInternalSchemaName, qualifiedDataTableName);
|
||||
}
|
||||
else
|
||||
{
|
||||
appendStringInfo(queryInfo,
|
||||
"INSERT INTO %s (shard_key_value, object_id, document)"
|
||||
" SELECT %s.get_shard_key_value($1, $2, document), object_id, document"
|
||||
" FROM %s",
|
||||
tmpDataTableName, ApiInternalSchemaName, qualifiedDataTableName);
|
||||
}
|
||||
|
||||
ExtensionExecuteQueryWithArgsViaSPI(queryInfo->data, nargs, insertArgTypes,
|
||||
insertArgValues, insertArgNulls, readOnly,
|
||||
|
||||
Reference in new issue
Block a user