Skip to content

Commit d52a57f

Browse files
committed
Actually there's a better way to do this, which is to count tuples
during the vacuumcleanup scan that we're going to do anyway. Should save a few cycles (one calculation per page, not per tuple) as well as not having to depend on assumptions about heap and index being in step. I think this could probably be made to work for GIST too, but that code looks messy enough that I'm disinclined to try right now.
1 parent fd267c1 commit d52a57f

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

src/backend/access/nbtree/nbtree.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
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)
539539
IndexBulkDeleteCallback callback = (IndexBulkDeleteCallback) PG_GETARG_POINTER(1);
540540
void *callback_state = (void *) PG_GETARG_POINTER(2);
541541
IndexBulkDeleteResult *result;
542-
double tuples_removed;
543-
double num_index_tuples;
542+
double tuples_removed = 0;
544543
OffsetNumber deletable[MaxOffsetNumber];
545544
int ndeletable;
546545
Buffer buf;
547546
BlockNumber 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)
574568
buf = _bt_get_endpoint(rel, 0, false);
575-
}
576569
else
577-
{
578-
/* skip scan and set flag for btvacuumcleanup */
579570
buf = InvalidBuffer;
580-
num_index_tuples = -1;
581-
}
582571

583572
if (BufferIsValid(buf)) /* check for empty index */
584573
{
@@ -634,8 +623,6 @@ btbulkdelete(PG_FUNCTION_ARGS)
634623
deletable[ndeletable++] = offnum;
635624
tuples_removed += 1;
636625
}
637-
else
638-
num_index_tuples += 1;
639626
}
640627
}
641628

@@ -663,7 +650,7 @@ btbulkdelete(PG_FUNCTION_ARGS)
663650

664651
result = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
665652
result->num_pages = num_pages;
666-
result->num_index_tuples = num_index_tuples;
653+
/* btvacuumcleanup will fill in num_index_tuples */
667654
result->tuples_removed = tuples_removed;
668655

669656
PG_RETURN_POINTER(result);
@@ -687,6 +674,7 @@ btvacuumcleanup(PG_FUNCTION_ARGS)
687674
BlockNumber *freePages;
688675
int nFreePages,
689676
maxFreePages;
677+
double num_index_tuples = 0;
690678
BlockNumber pages_deleted = 0;
691679
MemoryContext mycontext;
692680
MemoryContext oldcontext;
@@ -801,6 +789,12 @@ btvacuumcleanup(PG_FUNCTION_ARGS)
801789
MemoryContextSwitchTo(oldcontext);
802790
continue; /* 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 */
849843
stats->num_pages = num_pages;
844+
stats->num_index_tuples = num_index_tuples;
850845
stats->pages_deleted = pages_deleted;
851846
stats->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-
860848
PG_RETURN_POINTER(stats);
861849
}
862850

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy