Prevent synchronous scan during GIN index build, because GIN is optimized
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 13 Nov 2008 17:42:19 +0000 (17:42 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 13 Nov 2008 17:42:19 +0000 (17:42 +0000)
for inserting tuples in increasing TID order.  It's not clear whether this
fully explains Ivan Sergio Borgonovo's complaint, but simple testing
confirms that a scan that doesn't start at block 0 can slow GIN build by
a factor of three or four.

Backpatch to 8.3.  Sync scan didn't exist before that.

src/backend/access/gin/gininsert.c
src/backend/access/gist/gist.c
src/backend/access/hash/hash.c
src/backend/access/nbtree/nbtree.c
src/backend/catalog/index.c
src/include/catalog/index.h

index 1942da1a45d022693bc6d51c216a2fd7b14701c8..dd805c9f8f890d27cb7c6d09e6d72b1b32a36e7e 100644 (file)
@@ -340,8 +340,11 @@ ginbuild(PG_FUNCTION_ARGS)
        buildstate.accum.ginstate = &buildstate.ginstate;
        ginInitBA(&buildstate.accum);
 
-       /* do the heap scan */
-       reltuples = IndexBuildHeapScan(heap, index, indexInfo,
+       /*
+        * Do the heap scan.  We disallow sync scan here because dataPlaceToPage
+        * prefers to receive tuples in TID order.
+        */
+       reltuples = IndexBuildHeapScan(heap, index, indexInfo, false,
                                                                   ginBuildCallback, (void *) &buildstate);
 
        /* dump remaining entries to the index */
index 9a53941715ea28013cc13291b941d5f9b9037480..58050803be332f9eff0d306007720f7d931f4f31 100644 (file)
@@ -152,7 +152,7 @@ gistbuild(PG_FUNCTION_ARGS)
        buildstate.tmpCtx = createTempGistContext();
 
        /* do the heap scan */
-       reltuples = IndexBuildHeapScan(heap, index, indexInfo,
+       reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
                                                                   gistbuildCallback, (void *) &buildstate);
 
        /* okay, all heap tuples are indexed */
index 84e6617aab8cf7443eefbcc0028a27050e81c8d3..c2c2759fc88a8d2bcbb80cfa300b2283ae68ff6f 100644 (file)
@@ -93,7 +93,7 @@ hashbuild(PG_FUNCTION_ARGS)
        buildstate.indtuples = 0;
 
        /* do the heap scan */
-       reltuples = IndexBuildHeapScan(heap, index, indexInfo,
+       reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
                                                                   hashbuildCallback, (void *) &buildstate);
 
        if (buildstate.spool)
index 366da36517f4cf4e4167185fae9298f114cf480a..825ff5c6a217d7251655a8746e42e0aaf9d6ff85 100644 (file)
@@ -122,7 +122,7 @@ btbuild(PG_FUNCTION_ARGS)
                buildstate.spool2 = _bt_spoolinit(index, false, true);
 
        /* do the heap scan */
-       reltuples = IndexBuildHeapScan(heap, index, indexInfo,
+       reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
                                                                   btbuildCallback, (void *) &buildstate);
 
        /* okay, all heap tuples are indexed */
index 65da5239a990a79063aa0e12844cdd404852922d..90c07b6b0b25f8e01ce281edef78145ae3d042e8 100644 (file)
@@ -1513,6 +1513,7 @@ double
 IndexBuildHeapScan(Relation heapRelation,
                                   Relation indexRelation,
                                   IndexInfo *indexInfo,
+                                  bool allow_sync,
                                   IndexBuildCallback callback,
                                   void *callback_state)
 {
@@ -1575,7 +1576,12 @@ IndexBuildHeapScan(Relation heapRelation,
                OldestXmin = GetOldestXmin(heapRelation->rd_rel->relisshared, true);
        }
 
-       scan = heap_beginscan(heapRelation, snapshot, 0, NULL);
+       scan = heap_beginscan_strat(heapRelation,       /* relation */
+                                                               snapshot,               /* snapshot */
+                                                               0,                              /* number of keys */
+                                                               NULL,                   /* scan key */
+                                                               true,                   /* buffer access strategy OK */
+                                                               allow_sync);    /* syncscan OK? */
 
        reltuples = 0;
 
index b18ccb3bea6530999e84e12ee6c76975baf06993..322f1a991c852afd8b7d81a5cce0bc2065b173ce 100644 (file)
@@ -63,6 +63,7 @@ extern void index_build(Relation heapRelation,
 extern double IndexBuildHeapScan(Relation heapRelation,
                                   Relation indexRelation,
                                   IndexInfo *indexInfo,
+                                  bool allow_sync,
                                   IndexBuildCallback callback,
                                   void *callback_state);