MCPcopy Create free account
hub / github.com/apache/cloudberry / ExecHashIncreaseNumBatches

Function ExecHashIncreaseNumBatches

src/backend/executor/nodeHash.c:1132–1329  ·  view source on GitHub ↗

* ExecHashIncreaseNumBatches * increase the original number of batches in order to reduce * current memory consumption */

Source from the content-addressed store, hash-verified

1130 * current memory consumption
1131 */
1132static void
1133ExecHashIncreaseNumBatches(HashJoinTable hashtable)
1134{
1135 int oldnbatch = hashtable->nbatch;
1136 int curbatch = hashtable->curbatch;
1137 int nbatch;
1138 MemoryContext oldcxt;
1139 long ninmemory;
1140 long nfreed;
1141 HashMemoryChunk oldchunks;
1142 Size spaceUsedBefore = hashtable->spaceUsed;
1143 Size spaceFreed = 0;
1144 HashJoinTableStats *stats = hashtable->stats;
1145
1146 /* do nothing if we've decided to shut off growth */
1147 if (!hashtable->growEnabled)
1148 return;
1149
1150 /* safety check to avoid overflow */
1151 if (oldnbatch > Min(INT_MAX / 2, MaxAllocSize / (sizeof(void *) * 2)))
1152 return;
1153
1154 /* A reusable hash table can only respill during first pass */
1155 AssertImply(hashtable->hjstate->reuse_hashtable, hashtable->first_pass);
1156
1157 nbatch = oldnbatch * 2;
1158 Assert(nbatch > 1);
1159
1160#ifdef HJDEBUG
1161 printf("Hashjoin %p: increasing nbatch to %d because space = %zu\n",
1162 hashtable, nbatch, hashtable->spaceUsed);
1163#endif
1164
1165 oldcxt = MemoryContextSwitchTo(hashtable->hashCxt);
1166
1167 if (hashtable->innerBatchFile == NULL)
1168 {
1169 /* we had no file arrays before */
1170 hashtable->innerBatchFile = (BufFile **)
1171 palloc0(nbatch * sizeof(BufFile *));
1172 hashtable->outerBatchFile = (BufFile **)
1173 palloc0(nbatch * sizeof(BufFile *));
1174 /* time to establish the temp tablespaces, too */
1175 PrepareTempTablespaces();
1176 }
1177 else
1178 {
1179 /* enlarge arrays and zero out added entries */
1180 hashtable->innerBatchFile = (BufFile **)
1181 repalloc(hashtable->innerBatchFile, nbatch * sizeof(BufFile *));
1182 hashtable->outerBatchFile = (BufFile **)
1183 repalloc(hashtable->outerBatchFile, nbatch * sizeof(BufFile *));
1184 MemSet(hashtable->innerBatchFile + oldnbatch, 0,
1185 (nbatch - oldnbatch) * sizeof(BufFile *));
1186 MemSet(hashtable->outerBatchFile + oldnbatch, 0,
1187 (nbatch - oldnbatch) * sizeof(BufFile *));
1188 }
1189

Callers 2

ExecHashTableInsertFunction · 0.85
ExecHashSkewTableInsertFunction · 0.85

Calls 8

MemoryContextSwitchToFunction · 0.85
PrepareTempTablespacesFunction · 0.85
dense_allocFunction · 0.85
ExecHashJoinSaveTupleFunction · 0.85
palloc0Function · 0.50
repallocFunction · 0.50
pfreeFunction · 0.50

Tested by

no test coverage detected