@@ -310,14 +310,12 @@ spg_range_quad_inner_consistent(PG_FUNCTION_ARGS)
310310 spgInnerConsistentOut * out = (spgInnerConsistentOut * ) PG_GETARG_POINTER (1 );
311311 int which ;
312312 int i ;
313+ MemoryContext oldCtx ;
313314
314315 /*
315316 * For adjacent search we need also previous centroid (if any) to improve
316317 * the precision of the consistent check. In this case needPrevious flag
317- * is set and centroid is passed into reconstructedValues. This is not the
318- * intended purpose of reconstructedValues (because we already have the
319- * full value available at the leaf), but it's a convenient place to store
320- * state while traversing the tree.
318+ * is set and centroid is passed into traversalValue.
321319 */
322320 bool needPrevious = false;
323321
@@ -565,9 +563,9 @@ spg_range_quad_inner_consistent(PG_FUNCTION_ARGS)
565563 * for lower or upper bounds to be adjacent. Deserialize
566564 * previous centroid range if present for checking this.
567565 */
568- if (in -> reconstructedValue != (Datum ) 0 )
566+ if (in -> traversalValue != (Datum ) 0 )
569567 {
570- prevCentroid = DatumGetRangeType (in -> reconstructedValue );
568+ prevCentroid = DatumGetRangeType (in -> traversalValue );
571569 range_deserialize (typcache , prevCentroid ,
572570 & prevLower , & prevUpper , & prevEmpty );
573571 }
@@ -746,19 +744,37 @@ spg_range_quad_inner_consistent(PG_FUNCTION_ARGS)
746744 /* We must descend into the quadrant(s) identified by 'which' */
747745 out -> nodeNumbers = (int * ) palloc (sizeof (int ) * in -> nNodes );
748746 if (needPrevious )
749- out -> reconstructedValues = (Datum * ) palloc (sizeof (Datum ) * in -> nNodes );
747+ out -> traversalValues = (void * * ) palloc (sizeof (void * ) * in -> nNodes );
750748 out -> nNodes = 0 ;
749+
750+ /*
751+ * Elements of traversalValues should be allocated in
752+ * traversalMemoryContext
753+ */
754+ oldCtx = MemoryContextSwitchTo (in -> traversalMemoryContext );
755+
751756 for (i = 1 ; i <= in -> nNodes ; i ++ )
752757 {
753758 if (which & (1 << i ))
754759 {
755760 /* Save previous prefix if needed */
756761 if (needPrevious )
757- out -> reconstructedValues [out -> nNodes ] = in -> prefixDatum ;
758- out -> nodeNumbers [out -> nNodes ++ ] = i - 1 ;
762+ {
763+ Datum previousCentroid ;
764+
765+ /* We know, that in->prefixDatum in this place is varlena,
766+ * because it's range
767+ */
768+ previousCentroid = datumCopy (in -> prefixDatum , false, -1 );
769+ out -> traversalValues [out -> nNodes ] = (void * )previousCentroid ;
770+ }
771+ out -> nodeNumbers [out -> nNodes ] = i - 1 ;
772+ out -> nNodes ++ ;
759773 }
760774 }
761775
776+ MemoryContextSwitchTo (oldCtx );
777+
762778 PG_RETURN_VOID ();
763779}
764780
0 commit comments