summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Davis2025-03-25 05:06:02 +0000
committerJeff Davis2025-03-25 05:06:02 +0000
commit626df47ad9db809dc8f93330175ab95b75914721 (patch)
treedb18183d49021a32b56ba0d42517bbb3f7f416c7
parenta0942f441ed651f6345d969b7a8f4774eda1fceb (diff)
Remove 'additional' pointer from TupleHashEntryData.
Reduces memory required for hash aggregation by avoiding an allocation and a pointer in the TupleHashEntryData structure. That structure is used for all buckets, whether occupied or not, so the savings is substantial. Discussion: https://postgr.es/m/AApHDvpN4v3t_sdz4dvrv1Fx_ZPw=twSnxuTEytRYP7LFz5K9A@mail.gmail.com Reviewed-by: David Rowley <[email protected]>
-rw-r--r--src/backend/executor/execGrouping.c17
-rw-r--r--src/include/executor/executor.h5
-rw-r--r--src/include/nodes/execnodes.h1
3 files changed, 16 insertions, 7 deletions
diff --git a/src/backend/executor/execGrouping.c b/src/backend/executor/execGrouping.c
index a9d212aaec6..255bd795361 100644
--- a/src/backend/executor/execGrouping.c
+++ b/src/backend/executor/execGrouping.c
@@ -485,11 +485,18 @@ LookupTupleHashEntry_internal(TupleHashTable hashtable, TupleTableSlot *slot,
MemoryContextSwitchTo(hashtable->tablecxt);
- entry->firstTuple = ExecCopySlotMinimalTuple(slot);
- if (hashtable->additionalsize > 0)
- entry->additional = palloc0(hashtable->additionalsize);
- else
- entry->additional = NULL;
+ /*
+ * Copy the first tuple into the table context, and request
+ * additionalsize extra bytes before the allocation.
+ *
+ * The caller can get a pointer to the additional data with
+ * TupleHashEntryGetAdditional(), and store arbitrary data there.
+ * Placing both the tuple and additional data in the same
+ * allocation avoids the need to store an extra pointer in
+ * TupleHashEntryData or allocate an additional chunk.
+ */
+ entry->firstTuple = ExecCopySlotMinimalTupleExtra(slot,
+ hashtable->additionalsize);
}
}
else
diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h
index 69396a9d7f8..6a1fec88928 100644
--- a/src/include/executor/executor.h
+++ b/src/include/executor/executor.h
@@ -188,7 +188,10 @@ TupleHashEntryGetTuple(TupleHashEntry entry)
static inline void *
TupleHashEntryGetAdditional(TupleHashTable hashtable, TupleHashEntry entry)
{
- return entry->additional;
+ if (hashtable->additionalsize > 0)
+ return (char *) entry->firstTuple - hashtable->additionalsize;
+ else
+ return NULL;
}
#endif
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index 7df25d7e64f..e42f9f9f957 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -840,7 +840,6 @@ typedef struct TupleHashTableData *TupleHashTable;
typedef struct TupleHashEntryData
{
MinimalTuple firstTuple; /* copy of first tuple in this group */
- void *additional; /* user data */
uint32 status; /* hash status */
uint32 hash; /* hash value (cached) */
} TupleHashEntryData;