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

Generated by: LCOV version 1.13