LCOV - code coverage report
Current view: top level - src/backend/access/hash - hash.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 291 303 96.0 %
Date: 2020-12-05 17:06:23 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * hash.c
       4             :  *    Implementation of Margo Seltzer's Hashing package for postgres.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/access/hash/hash.c
      12             :  *
      13             :  * NOTES
      14             :  *    This file contains only the public interface routines.
      15             :  *
      16             :  *-------------------------------------------------------------------------
      17             :  */
      18             : 
      19             : #include "postgres.h"
      20             : 
      21             : #include "access/hash.h"
      22             : #include "access/hash_xlog.h"
      23             : #include "access/relscan.h"
      24             : #include "access/tableam.h"
      25             : #include "catalog/index.h"
      26             : #include "commands/progress.h"
      27             : #include "commands/vacuum.h"
      28             : #include "miscadmin.h"
      29             : #include "optimizer/plancat.h"
      30             : #include "pgstat.h"
      31             : #include "utils/builtins.h"
      32             : #include "utils/index_selfuncs.h"
      33             : #include "utils/rel.h"
      34             : 
      35             : /* Working state for hashbuild and its callback */
      36             : typedef struct
      37             : {
      38             :     HSpool     *spool;          /* NULL if not using spooling */
      39             :     double      indtuples;      /* # tuples accepted into index */
      40             :     Relation    heapRel;        /* heap relation descriptor */
      41             : } HashBuildState;
      42             : 
      43             : static void hashbuildCallback(Relation index,
      44             :                               ItemPointer tid,
      45             :                               Datum *values,
      46             :                               bool *isnull,
      47             :                               bool tupleIsAlive,
      48             :                               void *state);
      49             : 
      50             : 
      51             : /*
      52             :  * Hash handler function: return IndexAmRoutine with access method parameters
      53             :  * and callbacks.
      54             :  */
      55             : Datum
      56        1382 : hashhandler(PG_FUNCTION_ARGS)
      57             : {
      58        1382 :     IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
      59             : 
      60        1382 :     amroutine->amstrategies = HTMaxStrategyNumber;
      61        1382 :     amroutine->amsupport = HASHNProcs;
      62        1382 :     amroutine->amoptsprocnum = HASHOPTIONS_PROC;
      63        1382 :     amroutine->amcanorder = false;
      64        1382 :     amroutine->amcanorderbyop = false;
      65        1382 :     amroutine->amcanbackward = true;
      66        1382 :     amroutine->amcanunique = false;
      67        1382 :     amroutine->amcanmulticol = false;
      68        1382 :     amroutine->amoptionalkey = false;
      69        1382 :     amroutine->amsearcharray = false;
      70        1382 :     amroutine->amsearchnulls = false;
      71        1382 :     amroutine->amstorage = false;
      72        1382 :     amroutine->amclusterable = false;
      73        1382 :     amroutine->ampredlocks = true;
      74        1382 :     amroutine->amcanparallel = false;
      75        1382 :     amroutine->amcaninclude = false;
      76        1382 :     amroutine->amusemaintenanceworkmem = false;
      77        1382 :     amroutine->amparallelvacuumoptions =
      78             :         VACUUM_OPTION_PARALLEL_BULKDEL;
      79        1382 :     amroutine->amkeytype = INT4OID;
      80             : 
      81        1382 :     amroutine->ambuild = hashbuild;
      82        1382 :     amroutine->ambuildempty = hashbuildempty;
      83        1382 :     amroutine->aminsert = hashinsert;
      84        1382 :     amroutine->ambulkdelete = hashbulkdelete;
      85        1382 :     amroutine->amvacuumcleanup = hashvacuumcleanup;
      86        1382 :     amroutine->amcanreturn = NULL;
      87        1382 :     amroutine->amcostestimate = hashcostestimate;
      88        1382 :     amroutine->amoptions = hashoptions;
      89        1382 :     amroutine->amproperty = NULL;
      90        1382 :     amroutine->ambuildphasename = NULL;
      91        1382 :     amroutine->amvalidate = hashvalidate;
      92        1382 :     amroutine->amadjustmembers = hashadjustmembers;
      93        1382 :     amroutine->ambeginscan = hashbeginscan;
      94        1382 :     amroutine->amrescan = hashrescan;
      95        1382 :     amroutine->amgettuple = hashgettuple;
      96        1382 :     amroutine->amgetbitmap = hashgetbitmap;
      97        1382 :     amroutine->amendscan = hashendscan;
      98        1382 :     amroutine->ammarkpos = NULL;
      99        1382 :     amroutine->amrestrpos = NULL;
     100        1382 :     amroutine->amestimateparallelscan = NULL;
     101        1382 :     amroutine->aminitparallelscan = NULL;
     102        1382 :     amroutine->amparallelrescan = NULL;
     103             : 
     104        1382 :     PG_RETURN_POINTER(amroutine);
     105             : }
     106             : 
     107             : /*
     108             :  *  hashbuild() -- build a new hash index.
     109             :  */
     110             : IndexBuildResult *
     111         180 : hashbuild(Relation heap, Relation index, IndexInfo *indexInfo)
     112             : {
     113             :     IndexBuildResult *result;
     114             :     BlockNumber relpages;
     115             :     double      reltuples;
     116             :     double      allvisfrac;
     117             :     uint32      num_buckets;
     118             :     long        sort_threshold;
     119             :     HashBuildState buildstate;
     120             : 
     121             :     /*
     122             :      * We expect to be called exactly once for any index relation. If that's
     123             :      * not the case, big trouble's what we have.
     124             :      */
     125         180 :     if (RelationGetNumberOfBlocks(index) != 0)
     126           0 :         elog(ERROR, "index \"%s\" already contains data",
     127             :              RelationGetRelationName(index));
     128             : 
     129             :     /* Estimate the number of rows currently present in the table */
     130         180 :     estimate_rel_size(heap, NULL, &relpages, &reltuples, &allvisfrac);
     131             : 
     132             :     /* Initialize the hash index metadata page and initial buckets */
     133         180 :     num_buckets = _hash_init(index, reltuples, MAIN_FORKNUM);
     134             : 
     135             :     /*
     136             :      * If we just insert the tuples into the index in scan order, then
     137             :      * (assuming their hash codes are pretty random) there will be no locality
     138             :      * of access to the index, and if the index is bigger than available RAM
     139             :      * then we'll thrash horribly.  To prevent that scenario, we can sort the
     140             :      * tuples by (expected) bucket number.  However, such a sort is useless
     141             :      * overhead when the index does fit in RAM.  We choose to sort if the
     142             :      * initial index size exceeds maintenance_work_mem, or the number of
     143             :      * buffers usable for the index, whichever is less.  (Limiting by the
     144             :      * number of buffers should reduce thrashing between PG buffers and kernel
     145             :      * buffers, which seems useful even if no physical I/O results.  Limiting
     146             :      * by maintenance_work_mem is useful to allow easy testing of the sort
     147             :      * code path, and may be useful to DBAs as an additional control knob.)
     148             :      *
     149             :      * NOTE: this test will need adjustment if a bucket is ever different from
     150             :      * one page.  Also, "initial index size" accounting does not include the
     151             :      * metapage, nor the first bitmap page.
     152             :      */
     153         180 :     sort_threshold = (maintenance_work_mem * 1024L) / BLCKSZ;
     154         180 :     if (index->rd_rel->relpersistence != RELPERSISTENCE_TEMP)
     155         176 :         sort_threshold = Min(sort_threshold, NBuffers);
     156             :     else
     157           4 :         sort_threshold = Min(sort_threshold, NLocBuffer);
     158             : 
     159         180 :     if (num_buckets >= (uint32) sort_threshold)
     160           4 :         buildstate.spool = _h_spoolinit(heap, index, num_buckets);
     161             :     else
     162         176 :         buildstate.spool = NULL;
     163             : 
     164             :     /* prepare to build the index */
     165         180 :     buildstate.indtuples = 0;
     166         180 :     buildstate.heapRel = heap;
     167             : 
     168             :     /* do the heap scan */
     169         180 :     reltuples = table_index_build_scan(heap, index, indexInfo, true, true,
     170             :                                        hashbuildCallback,
     171             :                                        (void *) &buildstate, NULL);
     172         180 :     pgstat_progress_update_param(PROGRESS_CREATEIDX_TUPLES_TOTAL,
     173         180 :                                  buildstate.indtuples);
     174             : 
     175         180 :     if (buildstate.spool)
     176             :     {
     177             :         /* sort the tuples and insert them into the index */
     178           4 :         _h_indexbuild(buildstate.spool, buildstate.heapRel);
     179           4 :         _h_spooldestroy(buildstate.spool);
     180             :     }
     181             : 
     182             :     /*
     183             :      * Return statistics
     184             :      */
     185         180 :     result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
     186             : 
     187         180 :     result->heap_tuples = reltuples;
     188         180 :     result->index_tuples = buildstate.indtuples;
     189             : 
     190         180 :     return result;
     191             : }
     192             : 
     193             : /*
     194             :  *  hashbuildempty() -- build an empty hash index in the initialization fork
     195             :  */
     196             : void
     197           4 : hashbuildempty(Relation index)
     198             : {
     199           4 :     _hash_init(index, 0, INIT_FORKNUM);
     200           4 : }
     201             : 
     202             : /*
     203             :  * Per-tuple callback for table_index_build_scan
     204             :  */
     205             : static void
     206      319174 : hashbuildCallback(Relation index,
     207             :                   ItemPointer tid,
     208             :                   Datum *values,
     209             :                   bool *isnull,
     210             :                   bool tupleIsAlive,
     211             :                   void *state)
     212             : {
     213      319174 :     HashBuildState *buildstate = (HashBuildState *) state;
     214             :     Datum       index_values[1];
     215             :     bool        index_isnull[1];
     216             :     IndexTuple  itup;
     217             : 
     218             :     /* convert data to a hash key; on failure, do not insert anything */
     219      319174 :     if (!_hash_convert_tuple(index,
     220             :                              values, isnull,
     221             :                              index_values, index_isnull))
     222           0 :         return;
     223             : 
     224             :     /* Either spool the tuple for sorting, or just put it into the index */
     225      319174 :     if (buildstate->spool)
     226       40000 :         _h_spool(buildstate->spool, tid, index_values, index_isnull);
     227             :     else
     228             :     {
     229             :         /* form an index tuple and point it at the heap tuple */
     230      279174 :         itup = index_form_tuple(RelationGetDescr(index),
     231             :                                 index_values, index_isnull);
     232      279174 :         itup->t_tid = *tid;
     233      279174 :         _hash_doinsert(index, itup, buildstate->heapRel);
     234      279174 :         pfree(itup);
     235             :     }
     236             : 
     237      319174 :     buildstate->indtuples += 1;
     238             : }
     239             : 
     240             : /*
     241             :  *  hashinsert() -- insert an index tuple into a hash table.
     242             :  *
     243             :  *  Hash on the heap tuple's key, form an index tuple with hash code.
     244             :  *  Find the appropriate location for the new tuple, and put it there.
     245             :  */
     246             : bool
     247      132562 : hashinsert(Relation rel, Datum *values, bool *isnull,
     248             :            ItemPointer ht_ctid, Relation heapRel,
     249             :            IndexUniqueCheck checkUnique,
     250             :            IndexInfo *indexInfo)
     251             : {
     252             :     Datum       index_values[1];
     253             :     bool        index_isnull[1];
     254             :     IndexTuple  itup;
     255             : 
     256             :     /* convert data to a hash key; on failure, do not insert anything */
     257      132562 :     if (!_hash_convert_tuple(rel,
     258             :                              values, isnull,
     259             :                              index_values, index_isnull))
     260           0 :         return false;
     261             : 
     262             :     /* form an index tuple and point it at the heap tuple */
     263      132562 :     itup = index_form_tuple(RelationGetDescr(rel), index_values, index_isnull);
     264      132562 :     itup->t_tid = *ht_ctid;
     265             : 
     266      132562 :     _hash_doinsert(rel, itup, heapRel);
     267             : 
     268      132550 :     pfree(itup);
     269             : 
     270      132550 :     return false;
     271             : }
     272             : 
     273             : 
     274             : /*
     275             :  *  hashgettuple() -- Get the next tuple in the scan.
     276             :  */
     277             : bool
     278       68146 : hashgettuple(IndexScanDesc scan, ScanDirection dir)
     279             : {
     280       68146 :     HashScanOpaque so = (HashScanOpaque) scan->opaque;
     281             :     bool        res;
     282             : 
     283             :     /* Hash indexes are always lossy since we store only the hash code */
     284       68146 :     scan->xs_recheck = true;
     285             : 
     286             :     /*
     287             :      * If we've already initialized this scan, we can just advance it in the
     288             :      * appropriate direction.  If we haven't done so yet, we call a routine to
     289             :      * get the first item in the scan.
     290             :      */
     291       68146 :     if (!HashScanPosIsValid(so->currPos))
     292         286 :         res = _hash_first(scan, dir);
     293             :     else
     294             :     {
     295             :         /*
     296             :          * Check to see if we should kill the previously-fetched tuple.
     297             :          */
     298       67860 :         if (scan->kill_prior_tuple)
     299             :         {
     300             :             /*
     301             :              * Yes, so remember it for later. (We'll deal with all such tuples
     302             :              * at once right after leaving the index page or at end of scan.)
     303             :              * In case if caller reverses the indexscan direction it is quite
     304             :              * possible that the same item might get entered multiple times.
     305             :              * But, we don't detect that; instead, we just forget any excess
     306             :              * entries.
     307             :              */
     308           4 :             if (so->killedItems == NULL)
     309           4 :                 so->killedItems = (int *)
     310           4 :                     palloc(MaxIndexTuplesPerPage * sizeof(int));
     311             : 
     312           4 :             if (so->numKilled < MaxIndexTuplesPerPage)
     313           4 :                 so->killedItems[so->numKilled++] = so->currPos.itemIndex;
     314             :         }
     315             : 
     316             :         /*
     317             :          * Now continue the scan.
     318             :          */
     319       67860 :         res = _hash_next(scan, dir);
     320             :     }
     321             : 
     322       68144 :     return res;
     323             : }
     324             : 
     325             : 
     326             : /*
     327             :  *  hashgetbitmap() -- get all tuples at once
     328             :  */
     329             : int64
     330          16 : hashgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
     331             : {
     332          16 :     HashScanOpaque so = (HashScanOpaque) scan->opaque;
     333             :     bool        res;
     334          16 :     int64       ntids = 0;
     335             :     HashScanPosItem *currItem;
     336             : 
     337          16 :     res = _hash_first(scan, ForwardScanDirection);
     338             : 
     339          84 :     while (res)
     340             :     {
     341          68 :         currItem = &so->currPos.items[so->currPos.itemIndex];
     342             : 
     343             :         /*
     344             :          * _hash_first and _hash_next handle eliminate dead index entries
     345             :          * whenever scan->ignore_killed_tuples is true.  Therefore, there's
     346             :          * nothing to do here except add the results to the TIDBitmap.
     347             :          */
     348          68 :         tbm_add_tuples(tbm, &(currItem->heapTid), 1, true);
     349          68 :         ntids++;
     350             : 
     351          68 :         res = _hash_next(scan, ForwardScanDirection);
     352             :     }
     353             : 
     354          16 :     return ntids;
     355             : }
     356             : 
     357             : 
     358             : /*
     359             :  *  hashbeginscan() -- start a scan on a hash index
     360             :  */
     361             : IndexScanDesc
     362         278 : hashbeginscan(Relation rel, int nkeys, int norderbys)
     363             : {
     364             :     IndexScanDesc scan;
     365             :     HashScanOpaque so;
     366             : 
     367             :     /* no order by operators allowed */
     368             :     Assert(norderbys == 0);
     369             : 
     370         278 :     scan = RelationGetIndexScan(rel, nkeys, norderbys);
     371             : 
     372         278 :     so = (HashScanOpaque) palloc(sizeof(HashScanOpaqueData));
     373         278 :     HashScanPosInvalidate(so->currPos);
     374         278 :     so->hashso_bucket_buf = InvalidBuffer;
     375         278 :     so->hashso_split_bucket_buf = InvalidBuffer;
     376             : 
     377         278 :     so->hashso_buc_populated = false;
     378         278 :     so->hashso_buc_split = false;
     379             : 
     380         278 :     so->killedItems = NULL;
     381         278 :     so->numKilled = 0;
     382             : 
     383         278 :     scan->opaque = so;
     384             : 
     385         278 :     return scan;
     386             : }
     387             : 
     388             : /*
     389             :  *  hashrescan() -- rescan an index relation
     390             :  */
     391             : void
     392         302 : hashrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
     393             :            ScanKey orderbys, int norderbys)
     394             : {
     395         302 :     HashScanOpaque so = (HashScanOpaque) scan->opaque;
     396         302 :     Relation    rel = scan->indexRelation;
     397             : 
     398         302 :     if (HashScanPosIsValid(so->currPos))
     399             :     {
     400             :         /* Before leaving current page, deal with any killed items */
     401           8 :         if (so->numKilled > 0)
     402           0 :             _hash_kill_items(scan);
     403             :     }
     404             : 
     405         302 :     _hash_dropscanbuf(rel, so);
     406             : 
     407             :     /* set position invalid (this will cause _hash_first call) */
     408         302 :     HashScanPosInvalidate(so->currPos);
     409             : 
     410             :     /* Update scan key, if a new one is given */
     411         302 :     if (scankey && scan->numberOfKeys > 0)
     412             :     {
     413         302 :         memmove(scan->keyData,
     414             :                 scankey,
     415         302 :                 scan->numberOfKeys * sizeof(ScanKeyData));
     416             :     }
     417             : 
     418         302 :     so->hashso_buc_populated = false;
     419         302 :     so->hashso_buc_split = false;
     420         302 : }
     421             : 
     422             : /*
     423             :  *  hashendscan() -- close down a scan
     424             :  */
     425             : void
     426         276 : hashendscan(IndexScanDesc scan)
     427             : {
     428         276 :     HashScanOpaque so = (HashScanOpaque) scan->opaque;
     429         276 :     Relation    rel = scan->indexRelation;
     430             : 
     431         276 :     if (HashScanPosIsValid(so->currPos))
     432             :     {
     433             :         /* Before leaving current page, deal with any killed items */
     434          24 :         if (so->numKilled > 0)
     435           0 :             _hash_kill_items(scan);
     436             :     }
     437             : 
     438         276 :     _hash_dropscanbuf(rel, so);
     439             : 
     440         276 :     if (so->killedItems != NULL)
     441           4 :         pfree(so->killedItems);
     442         276 :     pfree(so);
     443         276 :     scan->opaque = NULL;
     444         276 : }
     445             : 
     446             : /*
     447             :  * Bulk deletion of all index entries pointing to a set of heap tuples.
     448             :  * The set of target tuples is specified via a callback routine that tells
     449             :  * whether any given heap tuple (identified by ItemPointer) is being deleted.
     450             :  *
     451             :  * This function also deletes the tuples that are moved by split to other
     452             :  * bucket.
     453             :  *
     454             :  * Result: a palloc'd struct containing statistical info for VACUUM displays.
     455             :  */
     456             : IndexBulkDeleteResult *
     457           6 : hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
     458             :                IndexBulkDeleteCallback callback, void *callback_state)
     459             : {
     460           6 :     Relation    rel = info->index;
     461             :     double      tuples_removed;
     462             :     double      num_index_tuples;
     463             :     double      orig_ntuples;
     464             :     Bucket      orig_maxbucket;
     465             :     Bucket      cur_maxbucket;
     466             :     Bucket      cur_bucket;
     467           6 :     Buffer      metabuf = InvalidBuffer;
     468             :     HashMetaPage metap;
     469             :     HashMetaPage cachedmetap;
     470             : 
     471           6 :     tuples_removed = 0;
     472           6 :     num_index_tuples = 0;
     473             : 
     474             :     /*
     475             :      * We need a copy of the metapage so that we can use its hashm_spares[]
     476             :      * values to compute bucket page addresses, but a cached copy should be
     477             :      * good enough.  (If not, we'll detect that further down and refresh the
     478             :      * cache as necessary.)
     479             :      */
     480           6 :     cachedmetap = _hash_getcachedmetap(rel, &metabuf, false);
     481             :     Assert(cachedmetap != NULL);
     482             : 
     483           6 :     orig_maxbucket = cachedmetap->hashm_maxbucket;
     484           6 :     orig_ntuples = cachedmetap->hashm_ntuples;
     485             : 
     486             :     /* Scan the buckets that we know exist */
     487           6 :     cur_bucket = 0;
     488           6 :     cur_maxbucket = orig_maxbucket;
     489             : 
     490           8 : loop_top:
     491         242 :     while (cur_bucket <= cur_maxbucket)
     492             :     {
     493             :         BlockNumber bucket_blkno;
     494             :         BlockNumber blkno;
     495             :         Buffer      bucket_buf;
     496             :         Buffer      buf;
     497             :         HashPageOpaque bucket_opaque;
     498             :         Page        page;
     499         234 :         bool        split_cleanup = false;
     500             : 
     501             :         /* Get address of bucket's start page */
     502         234 :         bucket_blkno = BUCKET_TO_BLKNO(cachedmetap, cur_bucket);
     503             : 
     504         234 :         blkno = bucket_blkno;
     505             : 
     506             :         /*
     507             :          * We need to acquire a cleanup lock on the primary bucket page to out
     508             :          * wait concurrent scans before deleting the dead tuples.
     509             :          */
     510         234 :         buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, info->strategy);
     511         234 :         LockBufferForCleanup(buf);
     512         234 :         _hash_checkpage(rel, buf, LH_BUCKET_PAGE);
     513             : 
     514         234 :         page = BufferGetPage(buf);
     515         234 :         bucket_opaque = (HashPageOpaque) PageGetSpecialPointer(page);
     516             : 
     517             :         /*
     518             :          * If the bucket contains tuples that are moved by split, then we need
     519             :          * to delete such tuples.  We can't delete such tuples if the split
     520             :          * operation on bucket is not finished as those are needed by scans.
     521             :          */
     522         234 :         if (!H_BUCKET_BEING_SPLIT(bucket_opaque) &&
     523         234 :             H_NEEDS_SPLIT_CLEANUP(bucket_opaque))
     524             :         {
     525           0 :             split_cleanup = true;
     526             : 
     527             :             /*
     528             :              * This bucket might have been split since we last held a lock on
     529             :              * the metapage.  If so, hashm_maxbucket, hashm_highmask and
     530             :              * hashm_lowmask might be old enough to cause us to fail to remove
     531             :              * tuples left behind by the most recent split.  To prevent that,
     532             :              * now that the primary page of the target bucket has been locked
     533             :              * (and thus can't be further split), check whether we need to
     534             :              * update our cached metapage data.
     535             :              */
     536             :             Assert(bucket_opaque->hasho_prevblkno != InvalidBlockNumber);
     537           0 :             if (bucket_opaque->hasho_prevblkno > cachedmetap->hashm_maxbucket)
     538             :             {
     539           0 :                 cachedmetap = _hash_getcachedmetap(rel, &metabuf, true);
     540             :                 Assert(cachedmetap != NULL);
     541             :             }
     542             :         }
     543             : 
     544         234 :         bucket_buf = buf;
     545             : 
     546         234 :         hashbucketcleanup(rel, cur_bucket, bucket_buf, blkno, info->strategy,
     547             :                           cachedmetap->hashm_maxbucket,
     548             :                           cachedmetap->hashm_highmask,
     549             :                           cachedmetap->hashm_lowmask, &tuples_removed,
     550             :                           &num_index_tuples, split_cleanup,
     551             :                           callback, callback_state);
     552             : 
     553         234 :         _hash_dropbuf(rel, bucket_buf);
     554             : 
     555             :         /* Advance to next bucket */
     556         234 :         cur_bucket++;
     557             :     }
     558             : 
     559           8 :     if (BufferIsInvalid(metabuf))
     560           6 :         metabuf = _hash_getbuf(rel, HASH_METAPAGE, HASH_NOLOCK, LH_META_PAGE);
     561             : 
     562             :     /* Write-lock metapage and check for split since we started */
     563           8 :     LockBuffer(metabuf, BUFFER_LOCK_EXCLUSIVE);
     564           8 :     metap = HashPageGetMeta(BufferGetPage(metabuf));
     565             : 
     566           8 :     if (cur_maxbucket != metap->hashm_maxbucket)
     567             :     {
     568             :         /* There's been a split, so process the additional bucket(s) */
     569           2 :         LockBuffer(metabuf, BUFFER_LOCK_UNLOCK);
     570           2 :         cachedmetap = _hash_getcachedmetap(rel, &metabuf, true);
     571             :         Assert(cachedmetap != NULL);
     572           2 :         cur_maxbucket = cachedmetap->hashm_maxbucket;
     573           2 :         goto loop_top;
     574             :     }
     575             : 
     576             :     /* Okay, we're really done.  Update tuple count in metapage. */
     577           6 :     START_CRIT_SECTION();
     578             : 
     579           6 :     if (orig_maxbucket == metap->hashm_maxbucket &&
     580           4 :         orig_ntuples == metap->hashm_ntuples)
     581             :     {
     582             :         /*
     583             :          * No one has split or inserted anything since start of scan, so
     584             :          * believe our count as gospel.
     585             :          */
     586           0 :         metap->hashm_ntuples = num_index_tuples;
     587             :     }
     588             :     else
     589             :     {
     590             :         /*
     591             :          * Otherwise, our count is untrustworthy since we may have
     592             :          * double-scanned tuples in split buckets.  Proceed by dead-reckoning.
     593             :          * (Note: we still return estimated_count = false, because using this
     594             :          * count is better than not updating reltuples at all.)
     595             :          */
     596           6 :         if (metap->hashm_ntuples > tuples_removed)
     597           6 :             metap->hashm_ntuples -= tuples_removed;
     598             :         else
     599           0 :             metap->hashm_ntuples = 0;
     600           6 :         num_index_tuples = metap->hashm_ntuples;
     601             :     }
     602             : 
     603           6 :     MarkBufferDirty(metabuf);
     604             : 
     605             :     /* XLOG stuff */
     606           6 :     if (RelationNeedsWAL(rel))
     607             :     {
     608             :         xl_hash_update_meta_page xlrec;
     609             :         XLogRecPtr  recptr;
     610             : 
     611           6 :         xlrec.ntuples = metap->hashm_ntuples;
     612             : 
     613           6 :         XLogBeginInsert();
     614           6 :         XLogRegisterData((char *) &xlrec, SizeOfHashUpdateMetaPage);
     615             : 
     616           6 :         XLogRegisterBuffer(0, metabuf, REGBUF_STANDARD);
     617             : 
     618           6 :         recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_UPDATE_META_PAGE);
     619           6 :         PageSetLSN(BufferGetPage(metabuf), recptr);
     620             :     }
     621             : 
     622           6 :     END_CRIT_SECTION();
     623             : 
     624           6 :     _hash_relbuf(rel, metabuf);
     625             : 
     626             :     /* return statistics */
     627           6 :     if (stats == NULL)
     628           6 :         stats = (IndexBulkDeleteResult *) palloc0(sizeof(IndexBulkDeleteResult));
     629           6 :     stats->estimated_count = false;
     630           6 :     stats->num_index_tuples = num_index_tuples;
     631           6 :     stats->tuples_removed += tuples_removed;
     632             :     /* hashvacuumcleanup will fill in num_pages */
     633             : 
     634           6 :     return stats;
     635             : }
     636             : 
     637             : /*
     638             :  * Post-VACUUM cleanup.
     639             :  *
     640             :  * Result: a palloc'd struct containing statistical info for VACUUM displays.
     641             :  */
     642             : IndexBulkDeleteResult *
     643          34 : hashvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
     644             : {
     645          34 :     Relation    rel = info->index;
     646             :     BlockNumber num_pages;
     647             : 
     648             :     /* If hashbulkdelete wasn't called, return NULL signifying no change */
     649             :     /* Note: this covers the analyze_only case too */
     650          34 :     if (stats == NULL)
     651          28 :         return NULL;
     652             : 
     653             :     /* update statistics */
     654           6 :     num_pages = RelationGetNumberOfBlocks(rel);
     655           6 :     stats->num_pages = num_pages;
     656             : 
     657           6 :     return stats;
     658             : }
     659             : 
     660             : /*
     661             :  * Helper function to perform deletion of index entries from a bucket.
     662             :  *
     663             :  * This function expects that the caller has acquired a cleanup lock on the
     664             :  * primary bucket page, and will return with a write lock again held on the
     665             :  * primary bucket page.  The lock won't necessarily be held continuously,
     666             :  * though, because we'll release it when visiting overflow pages.
     667             :  *
     668             :  * There can't be any concurrent scans in progress when we first enter this
     669             :  * function because of the cleanup lock we hold on the primary bucket page,
     670             :  * but as soon as we release that lock, there might be.  If those scans got
     671             :  * ahead of our cleanup scan, they might see a tuple before we kill it and
     672             :  * wake up only after VACUUM has completed and the TID has been recycled for
     673             :  * an unrelated tuple.  To avoid that calamity, we prevent scans from passing
     674             :  * our cleanup scan by locking the next page in the bucket chain before
     675             :  * releasing the lock on the previous page.  (This type of lock chaining is not
     676             :  * ideal, so we might want to look for a better solution at some point.)
     677             :  *
     678             :  * We need to retain a pin on the primary bucket to ensure that no concurrent
     679             :  * split can start.
     680             :  */
     681             : void
     682         878 : hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf,
     683             :                   BlockNumber bucket_blkno, BufferAccessStrategy bstrategy,
     684             :                   uint32 maxbucket, uint32 highmask, uint32 lowmask,
     685             :                   double *tuples_removed, double *num_index_tuples,
     686             :                   bool split_cleanup,
     687             :                   IndexBulkDeleteCallback callback, void *callback_state)
     688             : {
     689             :     BlockNumber blkno;
     690             :     Buffer      buf;
     691         878 :     Bucket      new_bucket PG_USED_FOR_ASSERTS_ONLY = InvalidBucket;
     692         878 :     bool        bucket_dirty = false;
     693             : 
     694         878 :     blkno = bucket_blkno;
     695         878 :     buf = bucket_buf;
     696             : 
     697         878 :     if (split_cleanup)
     698         644 :         new_bucket = _hash_get_newbucket_from_oldbucket(rel, cur_bucket,
     699             :                                                         lowmask, maxbucket);
     700             : 
     701             :     /* Scan each page in bucket */
     702             :     for (;;)
     703         266 :     {
     704             :         HashPageOpaque opaque;
     705             :         OffsetNumber offno;
     706             :         OffsetNumber maxoffno;
     707             :         Buffer      next_buf;
     708             :         Page        page;
     709             :         OffsetNumber deletable[MaxOffsetNumber];
     710        1144 :         int         ndeletable = 0;
     711        1144 :         bool        retain_pin = false;
     712        1144 :         bool        clear_dead_marking = false;
     713             : 
     714        1144 :         vacuum_delay_point();
     715             : 
     716        1144 :         page = BufferGetPage(buf);
     717        1144 :         opaque = (HashPageOpaque) PageGetSpecialPointer(page);
     718             : 
     719             :         /* Scan each tuple in page */
     720        1144 :         maxoffno = PageGetMaxOffsetNumber(page);
     721      264544 :         for (offno = FirstOffsetNumber;
     722             :              offno <= maxoffno;
     723      263400 :              offno = OffsetNumberNext(offno))
     724             :         {
     725             :             ItemPointer htup;
     726             :             IndexTuple  itup;
     727             :             Bucket      bucket;
     728      263400 :             bool        kill_tuple = false;
     729             : 
     730      263400 :             itup = (IndexTuple) PageGetItem(page,
     731             :                                             PageGetItemId(page, offno));
     732      263400 :             htup = &(itup->t_tid);
     733             : 
     734             :             /*
     735             :              * To remove the dead tuples, we strictly want to rely on results
     736             :              * of callback function.  refer btvacuumpage for detailed reason.
     737             :              */
     738      263400 :             if (callback && callback(htup, callback_state))
     739             :             {
     740       16940 :                 kill_tuple = true;
     741       33880 :                 if (tuples_removed)
     742       16940 :                     *tuples_removed += 1;
     743             :             }
     744      246460 :             else if (split_cleanup)
     745             :             {
     746             :                 /* delete the tuples that are moved by split. */
     747      192460 :                 bucket = _hash_hashkey2bucket(_hash_get_indextuple_hashkey(itup),
     748             :                                               maxbucket,
     749             :                                               highmask,
     750             :                                               lowmask);
     751             :                 /* mark the item for deletion */
     752      192460 :                 if (bucket != cur_bucket)
     753             :                 {
     754             :                     /*
     755             :                      * We expect tuples to either belong to current bucket or
     756             :                      * new_bucket.  This is ensured because we don't allow
     757             :                      * further splits from bucket that contains garbage. See
     758             :                      * comments in _hash_expandtable.
     759             :                      */
     760             :                     Assert(bucket == new_bucket);
     761       77684 :                     kill_tuple = true;
     762             :                 }
     763             :             }
     764             : 
     765      263400 :             if (kill_tuple)
     766             :             {
     767             :                 /* mark the item for deletion */
     768       94624 :                 deletable[ndeletable++] = offno;
     769             :             }
     770             :             else
     771             :             {
     772             :                 /* we're keeping it, so count it */
     773      168776 :                 if (num_index_tuples)
     774       54000 :                     *num_index_tuples += 1;
     775             :             }
     776             :         }
     777             : 
     778             :         /* retain the pin on primary bucket page till end of bucket scan */
     779        1144 :         if (blkno == bucket_blkno)
     780         878 :             retain_pin = true;
     781             :         else
     782         266 :             retain_pin = false;
     783             : 
     784        1144 :         blkno = opaque->hasho_nextblkno;
     785             : 
     786             :         /*
     787             :          * Apply deletions, advance to next page and write page if needed.
     788             :          */
     789        1144 :         if (ndeletable > 0)
     790             :         {
     791             :             /* No ereport(ERROR) until changes are logged */
     792         744 :             START_CRIT_SECTION();
     793             : 
     794         744 :             PageIndexMultiDelete(page, deletable, ndeletable);
     795         744 :             bucket_dirty = true;
     796             : 
     797             :             /*
     798             :              * Let us mark the page as clean if vacuum removes the DEAD tuples
     799             :              * from an index page. We do this by clearing
     800             :              * LH_PAGE_HAS_DEAD_TUPLES flag.
     801             :              */
     802         744 :             if (tuples_removed && *tuples_removed > 0 &&
     803          62 :                 H_HAS_DEAD_TUPLES(opaque))
     804             :             {
     805           0 :                 opaque->hasho_flag &= ~LH_PAGE_HAS_DEAD_TUPLES;
     806           0 :                 clear_dead_marking = true;
     807             :             }
     808             : 
     809         744 :             MarkBufferDirty(buf);
     810             : 
     811             :             /* XLOG stuff */
     812         744 :             if (RelationNeedsWAL(rel))
     813             :             {
     814             :                 xl_hash_delete xlrec;
     815             :                 XLogRecPtr  recptr;
     816             : 
     817         744 :                 xlrec.clear_dead_marking = clear_dead_marking;
     818         744 :                 xlrec.is_primary_bucket_page = (buf == bucket_buf) ? true : false;
     819             : 
     820         744 :                 XLogBeginInsert();
     821         744 :                 XLogRegisterData((char *) &xlrec, SizeOfHashDelete);
     822             : 
     823             :                 /*
     824             :                  * bucket buffer needs to be registered to ensure that we can
     825             :                  * acquire a cleanup lock on it during replay.
     826             :                  */
     827         744 :                 if (!xlrec.is_primary_bucket_page)
     828         116 :                     XLogRegisterBuffer(0, bucket_buf, REGBUF_STANDARD | REGBUF_NO_IMAGE);
     829             : 
     830         744 :                 XLogRegisterBuffer(1, buf, REGBUF_STANDARD);
     831         744 :                 XLogRegisterBufData(1, (char *) deletable,
     832             :                                     ndeletable * sizeof(OffsetNumber));
     833             : 
     834         744 :                 recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_DELETE);
     835         744 :                 PageSetLSN(BufferGetPage(buf), recptr);
     836             :             }
     837             : 
     838         744 :             END_CRIT_SECTION();
     839             :         }
     840             : 
     841             :         /* bail out if there are no more pages to scan. */
     842        1144 :         if (!BlockNumberIsValid(blkno))
     843         878 :             break;
     844             : 
     845         266 :         next_buf = _hash_getbuf_with_strategy(rel, blkno, HASH_WRITE,
     846             :                                               LH_OVERFLOW_PAGE,
     847             :                                               bstrategy);
     848             : 
     849             :         /*
     850             :          * release the lock on previous page after acquiring the lock on next
     851             :          * page
     852             :          */
     853         266 :         if (retain_pin)
     854          54 :             LockBuffer(buf, BUFFER_LOCK_UNLOCK);
     855             :         else
     856         212 :             _hash_relbuf(rel, buf);
     857             : 
     858         266 :         buf = next_buf;
     859             :     }
     860             : 
     861             :     /*
     862             :      * lock the bucket page to clear the garbage flag and squeeze the bucket.
     863             :      * if the current buffer is same as bucket buffer, then we already have
     864             :      * lock on bucket page.
     865             :      */
     866         878 :     if (buf != bucket_buf)
     867             :     {
     868          54 :         _hash_relbuf(rel, buf);
     869          54 :         LockBuffer(bucket_buf, BUFFER_LOCK_EXCLUSIVE);
     870             :     }
     871             : 
     872             :     /*
     873             :      * Clear the garbage flag from bucket after deleting the tuples that are
     874             :      * moved by split.  We purposefully clear the flag before squeeze bucket,
     875             :      * so that after restart, vacuum shouldn't again try to delete the moved
     876             :      * by split tuples.
     877             :      */
     878         878 :     if (split_cleanup)
     879             :     {
     880             :         HashPageOpaque bucket_opaque;
     881             :         Page        page;
     882             : 
     883         644 :         page = BufferGetPage(bucket_buf);
     884         644 :         bucket_opaque = (HashPageOpaque) PageGetSpecialPointer(page);
     885             : 
     886             :         /* No ereport(ERROR) until changes are logged */
     887         644 :         START_CRIT_SECTION();
     888             : 
     889         644 :         bucket_opaque->hasho_flag &= ~LH_BUCKET_NEEDS_SPLIT_CLEANUP;
     890         644 :         MarkBufferDirty(bucket_buf);
     891             : 
     892             :         /* XLOG stuff */
     893         644 :         if (RelationNeedsWAL(rel))
     894             :         {
     895             :             XLogRecPtr  recptr;
     896             : 
     897         644 :             XLogBeginInsert();
     898         644 :             XLogRegisterBuffer(0, bucket_buf, REGBUF_STANDARD);
     899             : 
     900         644 :             recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_SPLIT_CLEANUP);
     901         644 :             PageSetLSN(page, recptr);
     902             :         }
     903             : 
     904         644 :         END_CRIT_SECTION();
     905             :     }
     906             : 
     907             :     /*
     908             :      * If we have deleted anything, try to compact free space.  For squeezing
     909             :      * the bucket, we must have a cleanup lock, else it can impact the
     910             :      * ordering of tuples for a scan that has started before it.
     911             :      */
     912         878 :     if (bucket_dirty && IsBufferCleanupOK(bucket_buf))
     913         636 :         _hash_squeezebucket(rel, cur_bucket, bucket_blkno, bucket_buf,
     914             :                             bstrategy);
     915             :     else
     916         242 :         LockBuffer(bucket_buf, BUFFER_LOCK_UNLOCK);
     917         878 : }

Generated by: LCOV version 1.13