On 03/12/2026 13:06, Peter Eisentraut wrote:
I have found several cases where a variable or C function argument was declared as an integer but should be a floating-point type, or vice versa.  See attached patches.  I think most of these are harmless in practice.

All looks good to me.

The one in gistbuild.c is technically a bug, but it's in quite rare circumstances and there is no correctness issue, so it's probably not worth bothering with backpatching.

So, that's this issue from the commit message:

This could also lead to a miscalculation further below in
gistInitBuffering(): If itupAvgSize is larger than half of
pageFreeSpace, then avgIndexTuplesPerPage becomes exactly 1, and the
subtreesize calculation below has a division by zero, which becomes a
NaN, and then the effective_cache_size limit is not applied as

avgIndexTuplesPerPage can become exactly 1 anyway, if the tuples have just the right sizes. It's rare, but can happen. avgIndexTuplesPerPage < 1.0 and maxIndexTuplesPerPage < 1.0 can happen too, but they're not very sensible because every page must have at least one tuple.

AI devised the attached little SQL script to demonstrate that. It gets stuck looping forever in the loop to determine levelStep, on master, and with your 0002-gistbuild-Fix-variable-type.patch, too. How about the attached (clamp-gistInitBuffering.patch), on top of your patch?

- Heikki

Attachment: repro-gist-bltone.sql
Description: application/sql

commit 959096f2d138d95da57a8e59feb28ff92d78e356
Author: Heikki Linnakangas <[email protected]>
Date:   Thu Dec 3 16:36:34 2026 +0200

    wip

diff --git a/src/backend/access/gist/gistbuild.c b/src/backend/access/gist/gistbuild.c
index dea31142f00..9a97e375f11 100644
--- a/src/backend/access/gist/gistbuild.c
+++ b/src/backend/access/gist/gistbuild.c
@@ -666,8 +666,8 @@ gistInitBuffering(GISTBuildState *buildstate)
 	}
 
 	/* Calculate average and maximal number of index tuples which fit to page */
-	avgIndexTuplesPerPage = pageFreeSpace / itupAvgSize;
-	maxIndexTuplesPerPage = pageFreeSpace / itupMinSize;
+	avgIndexTuplesPerPage = Max((double) pageFreeSpace / itupAvgSize, 1.0);
+	maxIndexTuplesPerPage = Max((double) pageFreeSpace / itupMinSize, 1.0);
 
 	/*
 	 * We need to calculate two parameters for the buffering algorithm:
@@ -723,9 +723,12 @@ gistInitBuffering(GISTBuildState *buildstate)
 		double		maxlowestlevelpages;
 
 		/* size of an average subtree at this levelStep (in pages). */
-		subtreesize =
-			(1 - pow(avgIndexTuplesPerPage, (double) (levelStep + 1))) /
-			(1 - avgIndexTuplesPerPage);
+		if (1.0 - avgIndexTuplesPerPage == 0)
+			subtreesize = (double) (levelStep + 1);	/* avoid division by zero */
+		else
+			subtreesize =
+				(1.0 - pow(avgIndexTuplesPerPage, (double) (levelStep + 1))) /
+				(1.0 - avgIndexTuplesPerPage);
 
 		/* max number of pages at the lowest level of a subtree */
 		maxlowestlevelpages = pow(maxIndexTuplesPerPage, (double) levelStep);
@@ -805,7 +808,7 @@ calculatePagesPerBuffer(GISTBuildState *buildstate, int levelStep)
 	itupAvgSize = (double) buildstate->indtuplesSize /
 		(double) buildstate->indtuples;
 
-	avgIndexTuplesPerPage = pageFreeSpace / itupAvgSize;
+	avgIndexTuplesPerPage = Max(pageFreeSpace / itupAvgSize, 1.0);
 
 	/*
 	 * Recalculate required size of buffers.

Reply via email to