*** a/src/backend/access/spgist/spgdoinsert.c
--- b/src/backend/access/spgist/spgdoinsert.c
***************
*** 826,832 **** doPickSplit(Relation index, SpGistState *state,
  	heapPtrs[in.nTuples] = newLeafTuple->heapPtr;
  	in.nTuples++;
  
! 	memset(&out, 0, sizeof(out));
  
  	if (!isNulls)
  	{
--- 826,832 ----
  	heapPtrs[in.nTuples] = newLeafTuple->heapPtr;
  	in.nTuples++;
  
! 	MemSet(&out, 0, sizeof(out));
  
  	if (!isNulls)
  	{
***************
*** 1861,1866 **** spgdoinsert(Relation index, SpGistState *state,
--- 1861,1874 ----
  	int			leafSize;
  	SPPageDesc	current,
  				parent;
+ 	FmgrInfo   *procinfo = NULL;
+ 
+ 	/*
+ 	 * Look up FmgrInfo of the user-defined choose function once, to save
+ 	 * cycles in the loop below.
+ 	 */
+ 	if (!isnull)
+ 		procinfo = index_getprocinfo(index, 1, SPGIST_CHOOSE_PROC);
  
  	/*
  	 * Since we don't use index_form_tuple in this AM, we have to make sure
***************
*** 2007,2013 **** spgdoinsert(Relation index, SpGistState *state,
  			SpGistInnerTuple innerTuple;
  			spgChooseIn in;
  			spgChooseOut out;
- 			FmgrInfo   *procinfo;
  
  			/*
  			 * spgAddNode and spgSplitTuple cases will loop back to here to
--- 2015,2020 ----
***************
*** 2030,2041 **** spgdoinsert(Relation index, SpGistState *state,
  			in.nNodes = innerTuple->nNodes;
  			in.nodeLabels = spgExtractNodeLabels(state, innerTuple);
  
! 			memset(&out, 0, sizeof(out));
  
  			if (!isnull)
  			{
  				/* use user-defined choose method */
- 				procinfo = index_getprocinfo(index, 1, SPGIST_CHOOSE_PROC);
  				FunctionCall2Coll(procinfo,
  								  index->rd_indcollation[0],
  								  PointerGetDatum(&in),
--- 2037,2047 ----
  			in.nNodes = innerTuple->nNodes;
  			in.nodeLabels = spgExtractNodeLabels(state, innerTuple);
  
! 			MemSet(&out, 0, sizeof(out));
  
  			if (!isnull)
  			{
  				/* use user-defined choose method */
  				FunctionCall2Coll(procinfo,
  								  index->rd_indcollation[0],
  								  PointerGetDatum(&in),
*** a/src/backend/access/spgist/spgutils.c
--- b/src/backend/access/spgist/spgutils.c
***************
*** 743,769 **** Datum *
  spgExtractNodeLabels(SpGistState *state, SpGistInnerTuple innerTuple)
  {
  	Datum	   *nodeLabels;
- 	int			nullcount = 0;
  	int			i;
  	SpGistNodeTuple node;
  
! 	nodeLabels = (Datum *) palloc(sizeof(Datum) * innerTuple->nNodes);
! 	SGITITERATE(innerTuple, i, node)
! 	{
! 		if (IndexTupleHasNulls(node))
! 			nullcount++;
! 		else
! 			nodeLabels[i] = SGNTDATUM(node, state);
! 	}
! 	if (nullcount == innerTuple->nNodes)
  	{
  		/* They're all null, so just return NULL */
- 		pfree(nodeLabels);
  		return NULL;
  	}
! 	if (nullcount != 0)
! 		elog(ERROR, "some but not all node labels are null in SPGiST inner tuple");
! 	return nodeLabels;
  }
  
  /*
--- 743,774 ----
  spgExtractNodeLabels(SpGistState *state, SpGistInnerTuple innerTuple)
  {
  	Datum	   *nodeLabels;
  	int			i;
  	SpGistNodeTuple node;
  
! 	/* Either all the labels must be NULL, or none. */
! 	node = SGITNODEPTR(innerTuple);
! 	if (IndexTupleHasNulls(node))
  	{
+ 		SGITITERATE(innerTuple, i, node)
+ 		{
+ 			if (!IndexTupleHasNulls(node))
+ 				elog(ERROR, "some but not all node labels are null in SPGiST inner tuple");
+ 		}
  		/* They're all null, so just return NULL */
  		return NULL;
  	}
! 	else
! 	{
! 		nodeLabels = (Datum *) palloc(sizeof(Datum) * innerTuple->nNodes);
! 		SGITITERATE(innerTuple, i, node)
! 		{
! 			if (IndexTupleHasNulls(node))
! 				elog(ERROR, "some but not all node labels are null in SPGiST inner tuple");
! 			nodeLabels[i] = SGNTDATUM(node, state);
! 		}
! 		return nodeLabels;
! 	}
  }
  
  /*
