@@ -50,7 +50,7 @@ static bool _bt_mark_page_halfdead(Relation rel, Buffer leafbuf,
5050static bool _bt_unlink_halfdead_page (Relation rel , Buffer leafbuf ,
5151 BlockNumber scanblkno ,
5252 bool * rightsib_empty ,
53- uint32 * ndeleted );
53+ BTVacState * vstate );
5454static bool _bt_lock_subtree_parent (Relation rel , BlockNumber child ,
5555 BTStack stack ,
5656 Buffer * subtreeparent ,
@@ -1760,28 +1760,31 @@ _bt_rightsib_halfdeadflag(Relation rel, BlockNumber leafrightsib)
17601760 * should never pass a buffer containing an existing deleted page here. The
17611761 * lock and pin on caller's buffer will be dropped before we return.
17621762 *
1763- * Returns the number of pages successfully deleted (zero if page cannot
1764- * be deleted now; could be more than one if parent or right sibling pages
1765- * were deleted too). Note that this does not include pages that we delete
1766- * that the btvacuumscan scan has yet to reach; they'll get counted later
1767- * instead.
1763+ * Maintains bulk delete stats for caller, which are taken from vstate. We
1764+ * need to cooperate closely with caller here so that whole VACUUM operation
1765+ * reliably avoids any double counting of subsidiary-to-leafbuf pages that we
1766+ * delete in passing. If such pages happen to be from a block number that is
1767+ * ahead of the current scanblkno position, then caller is expected to count
1768+ * them directly later on. It's simpler for us to understand caller's
1769+ * requirements than it would be for caller to understand when or how a
1770+ * deleted page became deleted after the fact.
17681771 *
17691772 * NOTE: this leaks memory. Rather than trying to clean up everything
17701773 * carefully, it's better to run it in a temp context that can be reset
17711774 * frequently.
17721775 */
1773- uint32
1774- _bt_pagedel (Relation rel , Buffer leafbuf )
1776+ void
1777+ _bt_pagedel (Relation rel , Buffer leafbuf , BTVacState * vstate )
17751778{
1776- uint32 ndeleted = 0 ;
17771779 BlockNumber rightsib ;
17781780 bool rightsib_empty ;
17791781 Page page ;
17801782 BTPageOpaque opaque ;
17811783
17821784 /*
17831785 * Save original leafbuf block number from caller. Only deleted blocks
1784- * that are <= scanblkno get counted in ndeleted return value.
1786+ * that are <= scanblkno are added to bulk delete stat's pages_deleted
1787+ * count.
17851788 */
17861789 BlockNumber scanblkno = BufferGetBlockNumber (leafbuf );
17871790
@@ -1843,7 +1846,7 @@ _bt_pagedel(Relation rel, Buffer leafbuf)
18431846 RelationGetRelationName (rel ))));
18441847
18451848 _bt_relbuf (rel , leafbuf );
1846- return ndeleted ;
1849+ return ;
18471850 }
18481851
18491852 /*
@@ -1873,7 +1876,7 @@ _bt_pagedel(Relation rel, Buffer leafbuf)
18731876 Assert (!P_ISHALFDEAD (opaque ));
18741877
18751878 _bt_relbuf (rel , leafbuf );
1876- return ndeleted ;
1879+ return ;
18771880 }
18781881
18791882 /*
@@ -1922,8 +1925,7 @@ _bt_pagedel(Relation rel, Buffer leafbuf)
19221925 if (_bt_leftsib_splitflag (rel , leftsib , leafblkno ))
19231926 {
19241927 ReleaseBuffer (leafbuf );
1925- Assert (ndeleted == 0 );
1926- return ndeleted ;
1928+ return ;
19271929 }
19281930
19291931 /* we need an insertion scan key for the search, so build one */
@@ -1964,7 +1966,7 @@ _bt_pagedel(Relation rel, Buffer leafbuf)
19641966 if (!_bt_mark_page_halfdead (rel , leafbuf , stack ))
19651967 {
19661968 _bt_relbuf (rel , leafbuf );
1967- return ndeleted ;
1969+ return ;
19681970 }
19691971 }
19701972
@@ -1979,7 +1981,7 @@ _bt_pagedel(Relation rel, Buffer leafbuf)
19791981 {
19801982 /* Check for interrupts in _bt_unlink_halfdead_page */
19811983 if (!_bt_unlink_halfdead_page (rel , leafbuf , scanblkno ,
1982- & rightsib_empty , & ndeleted ))
1984+ & rightsib_empty , vstate ))
19831985 {
19841986 /*
19851987 * _bt_unlink_halfdead_page should never fail, since we
@@ -1990,7 +1992,7 @@ _bt_pagedel(Relation rel, Buffer leafbuf)
19901992 * lock and pin on leafbuf for us.
19911993 */
19921994 Assert (false);
1993- return ndeleted ;
1995+ return ;
19941996 }
19951997 }
19961998
@@ -2026,8 +2028,6 @@ _bt_pagedel(Relation rel, Buffer leafbuf)
20262028
20272029 leafbuf = _bt_getbuf (rel , rightsib , BT_WRITE );
20282030 }
2029-
2030- return ndeleted ;
20312031}
20322032
20332033/*
@@ -2262,9 +2262,10 @@ _bt_mark_page_halfdead(Relation rel, Buffer leafbuf, BTStack stack)
22622262 */
22632263static bool
22642264_bt_unlink_halfdead_page (Relation rel , Buffer leafbuf , BlockNumber scanblkno ,
2265- bool * rightsib_empty , uint32 * ndeleted )
2265+ bool * rightsib_empty , BTVacState * vstate )
22662266{
22672267 BlockNumber leafblkno = BufferGetBlockNumber (leafbuf );
2268+ IndexBulkDeleteResult * stats = vstate -> stats ;
22682269 BlockNumber leafleftsib ;
22692270 BlockNumber leafrightsib ;
22702271 BlockNumber target ;
@@ -2674,12 +2675,17 @@ _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf, BlockNumber scanblkno,
26742675 _bt_relbuf (rel , buf );
26752676
26762677 /*
2677- * If btvacuumscan won't revisit this page in a future btvacuumpage call
2678- * and count it as deleted then, we count it as deleted by current
2679- * btvacuumpage call
2678+ * Maintain pages_newly_deleted, which is simply the number of pages
2679+ * deleted by the ongoing VACUUM operation.
2680+ *
2681+ * Maintain pages_deleted in a way that takes into account how
2682+ * btvacuumpage() will count deleted pages that have yet to become
2683+ * scanblkno -- only count page when it's not going to get that treatment
2684+ * later on.
26802685 */
2686+ stats -> pages_newly_deleted ++ ;
26812687 if (target <= scanblkno )
2682- ( * ndeleted ) ++ ;
2688+ stats -> pages_deleted ++ ;
26832689
26842690 return true;
26852691}
0 commit comments