From c759c42b0fb9d9c0555965d93f6d2dc31cd99a0d Mon Sep 17 00:00:00 2001
From: David Rowley <dgrowley@gmail.com>
Date: Tue, 6 Jul 2021 00:54:54 +1200
Subject: [PATCH v3 4/4] Reduce the number of pallocs in partition_bounds_copy

The Datum pointer arrays in the resulting PartitionBoundInfo's datums
array can be allocated as a single chunk of memory rather than allocating
a series of small arrays for each Datum.

Author: David Rowley
Discussion: https://postgr.es/m/flat/CAMm1aWYFTqEio3bURzZh47jveiHRwgQTiSDvBORczNEz2duZ1Q@mail.gmail.com
---
 src/backend/partitioning/partbounds.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/backend/partitioning/partbounds.c b/src/backend/partitioning/partbounds.c
index 64bbec05ea..2a1ed5a49d 100644
--- a/src/backend/partitioning/partbounds.c
+++ b/src/backend/partitioning/partbounds.c
@@ -928,6 +928,7 @@ partition_bounds_copy(PartitionBoundInfo src,
 	int			partnatts;
 	bool		hash_part;
 	int			natts;
+	Datum	   *boundDatums;
 
 	dest = (PartitionBoundInfo) palloc(sizeof(PartitionBoundInfoData));
 
@@ -963,12 +964,13 @@ partition_bounds_copy(PartitionBoundInfo src,
 	 */
 	hash_part = (key->strategy == PARTITION_STRATEGY_HASH);
 	natts = hash_part ? 2 : partnatts;
+	boundDatums = palloc(ndatums * natts * sizeof(Datum));
 
 	for (i = 0; i < ndatums; i++)
 	{
 		int			j;
 
-		dest->datums[i] = (Datum *) palloc(sizeof(Datum) * natts);
+		dest->datums[i] = &boundDatums[i * natts];
 
 		for (j = 0; j < natts; j++)
 		{
-- 
2.30.2

