On 2019-Apr-28, Peter Geoghegan wrote:

> Commit ab0dfc961b6 used a "long" variable within _bt_load() to count
> the number of tuples entered into a B-Tree index as it is built. This
> will not work as expected on Windows, even on 64-bit Windows, because
> "long" is only 32-bits wide. It's far from impossible that you'd have
> ~2 billion index tuples when building a new index.

Agreed.  Here's a patch.  I see downthread that you also discovered the
same mistake in _h_indexbuild by grepping for "long"; I got to it by
examining callers of pgstat_progress_update_param and
pgstat_progress_update_multi_param.  I didn't find any other mistakes of
the same ilk.  Some codesites use "double" instead of "int64", but those
are not broken.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
>From ed4662882dc1377ac9bf60e149b462c4f5b6bfec Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvhe...@alvh.no-ip.org>
Date: Mon, 29 Apr 2019 14:15:19 -0400
Subject: [PATCH] Widen tuple counter variable to 64 bits

Progress reporting would have wrapped around for indexes created
with more than 2^31 tuples.
---
 src/backend/access/hash/hashsort.c  | 2 +-
 src/backend/access/nbtree/nbtsort.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/backend/access/hash/hashsort.c b/src/backend/access/hash/hashsort.c
index 00a57470a77..7b7fdb62b51 100644
--- a/src/backend/access/hash/hashsort.c
+++ b/src/backend/access/hash/hashsort.c
@@ -118,7 +118,7 @@ void
 _h_indexbuild(HSpool *hspool, Relation heapRel)
 {
 	IndexTuple	itup;
-	long		tups_done = 0;
+	uint64		tups_done = 0;
 #ifdef USE_ASSERT_CHECKING
 	uint32		hashkey = 0;
 #endif
diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c
index 9ac4c1e1c08..2e287d35dcb 100644
--- a/src/backend/access/nbtree/nbtsort.c
+++ b/src/backend/access/nbtree/nbtsort.c
@@ -1130,7 +1130,7 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
 	int			i,
 				keysz = IndexRelationGetNumberOfKeyAttributes(wstate->index);
 	SortSupport sortKeys;
-	long		tuples_done = 0;
+	uint64		tuples_done = 0;
 
 	if (merge)
 	{
-- 
2.17.1

Reply via email to