[Groonga-commit] pgroonga/pgroonga at fe7ab96 [master] Improve performance to compute max record size

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Nov 3 16:38:14 JST 2016


Kouhei Sutou	2016-11-03 16:38:14 +0900 (Thu, 03 Nov 2016)

  New Revision: fe7ab96703548b2e6797f0471f441a751aeddae0
  https://github.com/pgroonga/pgroonga/commit/fe7ab96703548b2e6797f0471f441a751aeddae0

  Message:
    Improve performance to compute max record size

  Modified files:
    src/pgroonga.c

  Modified: src/pgroonga.c (+87 -20)
===================================================================
--- src/pgroonga.c    2016-11-03 16:37:35 +0900 (caaa358)
+++ src/pgroonga.c    2016-11-03 16:38:14 +0900 (36f746f)
@@ -85,6 +85,8 @@ typedef struct PGrnBuildStateData
 	grn_obj	*sourcesTable;
 	grn_obj	*sourcesCtidColumn;
 	double nIndexedTuples;
+	bool needMaxRecordSizeUpdate;
+	uint32_t maxRecordSize;
 } PGrnBuildStateData;
 
 typedef PGrnBuildStateData *PGrnBuildState;
@@ -1923,6 +1925,64 @@ pgroonga_prefix_rk_contain_text_array(PG_FUNCTION_ARGS)
 	PG_RETURN_BOOL(matched);
 }
 
+static bool
+PGrnNeedMaxRecordSizeUpdate(Relation index)
+{
+	TupleDesc desc = RelationGetDescr(index);
+	unsigned int nVarCharColumns = 0;
+	unsigned int i;
+
+	for (i = 0; i < desc->natts; i++)
+	{
+		Form_pg_attribute attribute;
+
+		attribute = desc->attrs[i];
+		switch (attribute->atttypid)
+		{
+		case VARCHAROID:
+			nVarCharColumns++;
+			break;
+		case TEXTOID:
+		case VARCHARARRAYOID:
+		case TEXTARRAYOID:
+			return true;
+			break;
+		default:
+			break;
+		}
+	}
+
+	return nVarCharColumns >= 2;
+}
+
+static void
+PGrnUpdateMaxRecordSizeRaw(Relation index,
+						   uint32_t recordSize)
+{
+	uint32_t currentMaxRecordSize;
+
+	if (recordSize < INDEX_SIZE_MASK)
+		return;
+
+	currentMaxRecordSize = PGrnIndexStatusGetMaxRecordSize(index);
+	if (recordSize < currentMaxRecordSize)
+		return;
+
+	PGrnIndexStatusSetMaxRecordSize(index, recordSize);
+}
+
+static void
+PGrnUpdateMaxRecordSize(Relation index,
+						Datum *values,
+						bool *isnull)
+{
+	TupleDesc desc = RelationGetDescr(index);
+	Size recordSize;
+
+	recordSize = heap_compute_data_size(desc, values, isnull);
+	PGrnUpdateMaxRecordSizeRaw(index, recordSize);
+}
+
 static void
 PGrnInsert(Relation index,
 		   grn_obj *sourcesTable,
@@ -1936,22 +1996,6 @@ PGrnInsert(Relation index,
 	PGrnWALData *walData;
 	unsigned int i;
 
-	{
-		uint32_t currentMaxRecordSize;
-
-		currentMaxRecordSize = PGrnIndexStatusGetMaxRecordSize(index);
-		if (currentMaxRecordSize < INDEX_SIZE_MASK)
-		{
-			Size recordSize;
-
-			recordSize = heap_compute_data_size(desc, values, isnull);
-			if (recordSize >= INDEX_SIZE_MASK)
-			{
-				PGrnIndexStatusSetMaxRecordSize(index, recordSize);
-			}
-		}
-	}
-
 	if (desc->natts == 1 && PGrnAttributeIsJSONB(desc->attrs[0]->atttypid))
 	{
 		PGrnJSONBInsert(index,
@@ -2017,6 +2061,8 @@ pgroonga_insert_raw(Relation index,
 	sourcesCtidColumn = PGrnLookupSourcesCtidColumn(index, ERROR);
 	PGrnInsert(index, sourcesTable, sourcesCtidColumn,
 			   values, isnull, ctid);
+	if (PGrnNeedMaxRecordSizeUpdate(index))
+		PGrnUpdateMaxRecordSize(index, values, isnull);
 	grn_db_touch(ctx, grn_ctx_db(ctx));
 
 	return false;
@@ -3559,11 +3605,23 @@ PGrnBuildCallbackRaw(Relation index,
 {
 	PGrnBuildState bs = (PGrnBuildState) state;
 
-	if (tupleIsAlive) {
-		PGrnInsert(index, bs->sourcesTable, bs->sourcesCtidColumn,
-				   values, isnull, ctid);
-		bs->nIndexedTuples++;
+	if (!tupleIsAlive)
+		return;
+
+	PGrnInsert(index, bs->sourcesTable, bs->sourcesCtidColumn,
+			   values, isnull, ctid);
+	if (bs->needMaxRecordSizeUpdate)
+	{
+		TupleDesc desc = RelationGetDescr(index);
+		Size recordSize;
+
+		recordSize = heap_compute_data_size(desc, values, isnull);
+		if (recordSize > bs->maxRecordSize)
+		{
+			bs->maxRecordSize = recordSize;
+		}
 	}
+	bs->nIndexedTuples++;
 }
 
 #ifdef PGRN_IS_GREENPLUM
@@ -3618,6 +3676,10 @@ pgroonga_build_raw(Relation heap,
 
 	bs.sourcesTable = NULL;
 	bs.nIndexedTuples = 0.0;
+	bs.needMaxRecordSizeUpdate =
+		(PGrnNeedMaxRecordSizeUpdate(index) &&
+		 PGrnIndexStatusGetMaxRecordSize(index) < INDEX_SIZE_MASK);
+	bs.maxRecordSize = 0;
 
 	GRN_PTR_INIT(&supplementaryTables, GRN_OBJ_VECTOR, GRN_ID_NIL);
 	GRN_PTR_INIT(&lexicons, GRN_OBJ_VECTOR, GRN_ID_NIL);
@@ -3667,6 +3729,11 @@ pgroonga_build_raw(Relation heap,
 	result->heap_tuples = nHeapTuples;
 	result->index_tuples = bs.nIndexedTuples;
 
+	if (bs.needMaxRecordSizeUpdate)
+	{
+		PGrnUpdateMaxRecordSizeRaw(index, bs.maxRecordSize);
+	}
+
 	return result;
 }
 
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index