1212 * Portions Copyright (c) 1994, Regents of the University of California
1313 *
1414 * IDENTIFICATION
15- * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.139 2006/02/11 23:31:33 tgl Exp $
15+ * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.140 2006/02/12 00:18:17 tgl Exp $
1616 *
1717 *-------------------------------------------------------------------------
1818 */
@@ -539,16 +539,12 @@ btbulkdelete(PG_FUNCTION_ARGS)
539539IndexBulkDeleteCallback callback = (IndexBulkDeleteCallback )PG_GETARG_POINTER (1 );
540540void * callback_state = (void * )PG_GETARG_POINTER (2 );
541541IndexBulkDeleteResult * result ;
542- double tuples_removed ;
543- double num_index_tuples ;
542+ double tuples_removed = 0 ;
544543OffsetNumber deletable [MaxOffsetNumber ];
545544int ndeletable ;
546545Buffer buf ;
547546BlockNumber num_pages ;
548547
549- tuples_removed = 0 ;
550- num_index_tuples = 0 ;
551-
552548/*
553549 * The outer loop iterates over index leaf pages, the inner over items on
554550 * a leaf page. We issue just one _bt_delitems() call per page, so as to
@@ -566,19 +562,12 @@ btbulkdelete(PG_FUNCTION_ARGS)
566562 * could be stopped on those.
567563 *
568564 * We can skip the scan entirely if there's nothing to delete (indicated
569- * by callback_state == NULL) and the index isn't partial. For a partial
570- * index we must scan in order to derive a trustworthy tuple count.
565+ * by callback_state == NULL).
571566 */
572- if (callback_state || vac_is_partial_index (rel ))
573- {
567+ if (callback_state )
574568buf = _bt_get_endpoint (rel ,0 , false);
575- }
576569else
577- {
578- /* skip scan and set flag for btvacuumcleanup */
579570buf = InvalidBuffer ;
580- num_index_tuples = -1 ;
581- }
582571
583572if (BufferIsValid (buf ))/* check for empty index */
584573{
@@ -634,8 +623,6 @@ btbulkdelete(PG_FUNCTION_ARGS)
634623deletable [ndeletable ++ ]= offnum ;
635624tuples_removed += 1 ;
636625}
637- else
638- num_index_tuples += 1 ;
639626}
640627}
641628
@@ -663,7 +650,7 @@ btbulkdelete(PG_FUNCTION_ARGS)
663650
664651result = (IndexBulkDeleteResult * )palloc0 (sizeof (IndexBulkDeleteResult ));
665652result -> num_pages = num_pages ;
666- result -> num_index_tuples = num_index_tuples ;
653+ /* btvacuumcleanup will fill in num_index_tuples */
667654result -> tuples_removed = tuples_removed ;
668655
669656PG_RETURN_POINTER (result );
@@ -687,6 +674,7 @@ btvacuumcleanup(PG_FUNCTION_ARGS)
687674BlockNumber * freePages ;
688675int nFreePages ,
689676maxFreePages ;
677+ double num_index_tuples = 0 ;
690678BlockNumber pages_deleted = 0 ;
691679MemoryContext mycontext ;
692680MemoryContext oldcontext ;
@@ -801,6 +789,12 @@ btvacuumcleanup(PG_FUNCTION_ARGS)
801789MemoryContextSwitchTo (oldcontext );
802790continue ;/* pagedel released buffer */
803791}
792+ else if (P_ISLEAF (opaque ))
793+ {
794+ /* Count the index entries of live leaf pages */
795+ num_index_tuples += PageGetMaxOffsetNumber (page )+ 1 -
796+ P_FIRSTDATAKEY (opaque );
797+ }
804798_bt_relbuf (rel ,buf );
805799}
806800
@@ -847,16 +841,10 @@ btvacuumcleanup(PG_FUNCTION_ARGS)
847841
848842/* update statistics */
849843stats -> num_pages = num_pages ;
844+ stats -> num_index_tuples = num_index_tuples ;
850845stats -> pages_deleted = pages_deleted ;
851846stats -> pages_free = nFreePages ;
852847
853- /* if btbulkdelete skipped the scan, use heap's tuple count */
854- if (stats -> num_index_tuples < 0 )
855- {
856- Assert (info -> num_heap_tuples >=0 );
857- stats -> num_index_tuples = info -> num_heap_tuples ;
858- }
859-
860848PG_RETURN_POINTER (stats );
861849}
862850