LCOV - code coverage report
Current view: top level - contrib/pgstattuple - pgstatapprox.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta1 Lines: 44 95 46.3 %
Date: 2019-06-16 15:06:48 Functions: 5 6 83.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pgstatapprox.c
       4             :  *        Bloat estimation functions
       5             :  *
       6             :  * Copyright (c) 2014-2019, PostgreSQL Global Development Group
       7             :  *
       8             :  * IDENTIFICATION
       9             :  *        contrib/pgstattuple/pgstatapprox.c
      10             :  *
      11             :  *-------------------------------------------------------------------------
      12             :  */
      13             : #include "postgres.h"
      14             : 
      15             : #include "access/heapam.h"
      16             : #include "access/relation.h"
      17             : #include "access/transam.h"
      18             : #include "access/visibilitymap.h"
      19             : #include "access/xact.h"
      20             : #include "access/multixact.h"
      21             : #include "access/htup_details.h"
      22             : #include "catalog/namespace.h"
      23             : #include "catalog/pg_am_d.h"
      24             : #include "commands/vacuum.h"
      25             : #include "funcapi.h"
      26             : #include "miscadmin.h"
      27             : #include "storage/bufmgr.h"
      28             : #include "storage/freespace.h"
      29             : #include "storage/procarray.h"
      30             : #include "storage/lmgr.h"
      31             : #include "utils/builtins.h"
      32             : 
      33           2 : PG_FUNCTION_INFO_V1(pgstattuple_approx);
      34           4 : PG_FUNCTION_INFO_V1(pgstattuple_approx_v1_5);
      35             : 
      36             : Datum       pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo);
      37             : 
      38             : typedef struct output_type
      39             : {
      40             :     uint64      table_len;
      41             :     uint64      scanned_percent;
      42             :     uint64      tuple_count;
      43             :     uint64      tuple_len;
      44             :     double      tuple_percent;
      45             :     uint64      dead_tuple_count;
      46             :     uint64      dead_tuple_len;
      47             :     double      dead_tuple_percent;
      48             :     uint64      free_space;
      49             :     double      free_percent;
      50             : } output_type;
      51             : 
      52             : #define NUM_OUTPUT_COLUMNS 10
      53             : 
      54             : /*
      55             :  * This function takes an already open relation and scans its pages,
      56             :  * skipping those that have the corresponding visibility map bit set.
      57             :  * For pages we skip, we find the free space from the free space map
      58             :  * and approximate tuple_len on that basis. For the others, we count
      59             :  * the exact number of dead tuples etc.
      60             :  *
      61             :  * This scan is loosely based on vacuumlazy.c:lazy_scan_heap(), but
      62             :  * we do not try to avoid skipping single pages.
      63             :  */
      64             : static void
      65           2 : statapprox_heap(Relation rel, output_type *stat)
      66             : {
      67             :     BlockNumber scanned,
      68             :                 nblocks,
      69             :                 blkno;
      70           2 :     Buffer      vmbuffer = InvalidBuffer;
      71             :     BufferAccessStrategy bstrategy;
      72             :     TransactionId OldestXmin;
      73             : 
      74           2 :     OldestXmin = GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM);
      75           2 :     bstrategy = GetAccessStrategy(BAS_BULKREAD);
      76             : 
      77           2 :     nblocks = RelationGetNumberOfBlocks(rel);
      78           2 :     scanned = 0;
      79             : 
      80           2 :     for (blkno = 0; blkno < nblocks; blkno++)
      81             :     {
      82             :         Buffer      buf;
      83             :         Page        page;
      84             :         OffsetNumber offnum,
      85             :                     maxoff;
      86             :         Size        freespace;
      87             : 
      88           0 :         CHECK_FOR_INTERRUPTS();
      89             : 
      90             :         /*
      91             :          * If the page has only visible tuples, then we can find out the free
      92             :          * space from the FSM and move on.
      93             :          */
      94           0 :         if (VM_ALL_VISIBLE(rel, blkno, &vmbuffer))
      95             :         {
      96           0 :             freespace = GetRecordedFreeSpace(rel, blkno);
      97           0 :             stat->tuple_len += BLCKSZ - freespace;
      98           0 :             stat->free_space += freespace;
      99           0 :             continue;
     100             :         }
     101             : 
     102           0 :         buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno,
     103             :                                  RBM_NORMAL, bstrategy);
     104             : 
     105           0 :         LockBuffer(buf, BUFFER_LOCK_SHARE);
     106             : 
     107           0 :         page = BufferGetPage(buf);
     108             : 
     109             :         /*
     110             :          * It's not safe to call PageGetHeapFreeSpace() on new pages, so we
     111             :          * treat them as being free space for our purposes.
     112             :          */
     113           0 :         if (!PageIsNew(page))
     114           0 :             stat->free_space += PageGetHeapFreeSpace(page);
     115             :         else
     116           0 :             stat->free_space += BLCKSZ - SizeOfPageHeaderData;
     117             : 
     118             :         /* We may count the page as scanned even if it's new/empty */
     119           0 :         scanned++;
     120             : 
     121           0 :         if (PageIsNew(page) || PageIsEmpty(page))
     122             :         {
     123           0 :             UnlockReleaseBuffer(buf);
     124           0 :             continue;
     125             :         }
     126             : 
     127             :         /*
     128             :          * Look at each tuple on the page and decide whether it's live or
     129             :          * dead, then count it and its size. Unlike lazy_scan_heap, we can
     130             :          * afford to ignore problems and special cases.
     131             :          */
     132           0 :         maxoff = PageGetMaxOffsetNumber(page);
     133             : 
     134           0 :         for (offnum = FirstOffsetNumber;
     135             :              offnum <= maxoff;
     136           0 :              offnum = OffsetNumberNext(offnum))
     137             :         {
     138             :             ItemId      itemid;
     139             :             HeapTupleData tuple;
     140             : 
     141           0 :             itemid = PageGetItemId(page, offnum);
     142             : 
     143           0 :             if (!ItemIdIsUsed(itemid) || ItemIdIsRedirected(itemid) ||
     144           0 :                 ItemIdIsDead(itemid))
     145             :             {
     146           0 :                 continue;
     147             :             }
     148             : 
     149             :             Assert(ItemIdIsNormal(itemid));
     150             : 
     151           0 :             ItemPointerSet(&(tuple.t_self), blkno, offnum);
     152             : 
     153           0 :             tuple.t_data = (HeapTupleHeader) PageGetItem(page, itemid);
     154           0 :             tuple.t_len = ItemIdGetLength(itemid);
     155           0 :             tuple.t_tableOid = RelationGetRelid(rel);
     156             : 
     157             :             /*
     158             :              * We follow VACUUM's lead in counting INSERT_IN_PROGRESS tuples
     159             :              * as "dead" while DELETE_IN_PROGRESS tuples are "live".  We don't
     160             :              * bother distinguishing tuples inserted/deleted by our own
     161             :              * transaction.
     162             :              */
     163           0 :             switch (HeapTupleSatisfiesVacuum(&tuple, OldestXmin, buf))
     164             :             {
     165             :                 case HEAPTUPLE_LIVE:
     166             :                 case HEAPTUPLE_DELETE_IN_PROGRESS:
     167           0 :                     stat->tuple_len += tuple.t_len;
     168           0 :                     stat->tuple_count++;
     169           0 :                     break;
     170             :                 case HEAPTUPLE_DEAD:
     171             :                 case HEAPTUPLE_RECENTLY_DEAD:
     172             :                 case HEAPTUPLE_INSERT_IN_PROGRESS:
     173           0 :                     stat->dead_tuple_len += tuple.t_len;
     174           0 :                     stat->dead_tuple_count++;
     175           0 :                     break;
     176             :                 default:
     177           0 :                     elog(ERROR, "unexpected HeapTupleSatisfiesVacuum result");
     178             :                     break;
     179             :             }
     180             :         }
     181             : 
     182           0 :         UnlockReleaseBuffer(buf);
     183             :     }
     184             : 
     185           2 :     stat->table_len = (uint64) nblocks * BLCKSZ;
     186             : 
     187             :     /*
     188             :      * We don't know how many tuples are in the pages we didn't scan, so
     189             :      * extrapolate the live-tuple count to the whole table in the same way
     190             :      * that VACUUM does.  (Like VACUUM, we're not taking a random sample, so
     191             :      * just extrapolating linearly seems unsafe.)  There should be no dead
     192             :      * tuples in all-visible pages, so no correction is needed for that, and
     193             :      * we already accounted for the space in those pages, too.
     194             :      */
     195           2 :     stat->tuple_count = vac_estimate_reltuples(rel, nblocks, scanned,
     196           2 :                                                stat->tuple_count);
     197             : 
     198             :     /*
     199             :      * Calculate percentages if the relation has one or more pages.
     200             :      */
     201           2 :     if (nblocks != 0)
     202             :     {
     203           0 :         stat->scanned_percent = 100 * scanned / nblocks;
     204           0 :         stat->tuple_percent = 100.0 * stat->tuple_len / stat->table_len;
     205           0 :         stat->dead_tuple_percent = 100.0 * stat->dead_tuple_len / stat->table_len;
     206           0 :         stat->free_percent = 100.0 * stat->free_space / stat->table_len;
     207             :     }
     208             : 
     209           2 :     if (BufferIsValid(vmbuffer))
     210             :     {
     211           0 :         ReleaseBuffer(vmbuffer);
     212           0 :         vmbuffer = InvalidBuffer;
     213             :     }
     214           2 : }
     215             : 
     216             : /*
     217             :  * Returns estimated live/dead tuple statistics for the given relid.
     218             :  *
     219             :  * The superuser() check here must be kept as the library might be upgraded
     220             :  * without the extension being upgraded, meaning that in pre-1.5 installations
     221             :  * these functions could be called by any user.
     222             :  */
     223             : Datum
     224           0 : pgstattuple_approx(PG_FUNCTION_ARGS)
     225             : {
     226           0 :     Oid         relid = PG_GETARG_OID(0);
     227             : 
     228           0 :     if (!superuser())
     229           0 :         ereport(ERROR,
     230             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     231             :                  (errmsg("must be superuser to use pgstattuple functions"))));
     232             : 
     233           0 :     PG_RETURN_DATUM(pgstattuple_approx_internal(relid, fcinfo));
     234             : }
     235             : 
     236             : /*
     237             :  * As of pgstattuple version 1.5, we no longer need to check if the user
     238             :  * is a superuser because we REVOKE EXECUTE on the SQL function from PUBLIC.
     239             :  * Users can then grant access to it based on their policies.
     240             :  *
     241             :  * Otherwise identical to pgstattuple_approx (above).
     242             :  */
     243             : Datum
     244           8 : pgstattuple_approx_v1_5(PG_FUNCTION_ARGS)
     245             : {
     246           8 :     Oid         relid = PG_GETARG_OID(0);
     247             : 
     248           8 :     PG_RETURN_DATUM(pgstattuple_approx_internal(relid, fcinfo));
     249             : }
     250             : 
     251             : Datum
     252           8 : pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
     253             : {
     254             :     Relation    rel;
     255           8 :     output_type stat = {0};
     256             :     TupleDesc   tupdesc;
     257             :     bool        nulls[NUM_OUTPUT_COLUMNS];
     258             :     Datum       values[NUM_OUTPUT_COLUMNS];
     259             :     HeapTuple   ret;
     260           8 :     int         i = 0;
     261             : 
     262           8 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     263           0 :         elog(ERROR, "return type must be a row type");
     264             : 
     265           8 :     if (tupdesc->natts != NUM_OUTPUT_COLUMNS)
     266           0 :         elog(ERROR, "incorrect number of output arguments");
     267             : 
     268           8 :     rel = relation_open(relid, AccessShareLock);
     269             : 
     270             :     /*
     271             :      * Reject attempts to read non-local temporary relations; we would be
     272             :      * likely to get wrong data since we have no visibility into the owning
     273             :      * session's local buffers.
     274             :      */
     275           8 :     if (RELATION_IS_OTHER_TEMP(rel))
     276           0 :         ereport(ERROR,
     277             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     278             :                  errmsg("cannot access temporary tables of other sessions")));
     279             : 
     280             :     /*
     281             :      * We support only ordinary relations and materialised views, because we
     282             :      * depend on the visibility map and free space map for our estimates about
     283             :      * unscanned pages.
     284             :      */
     285          14 :     if (!(rel->rd_rel->relkind == RELKIND_RELATION ||
     286           6 :           rel->rd_rel->relkind == RELKIND_MATVIEW))
     287           6 :         ereport(ERROR,
     288             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     289             :                  errmsg("\"%s\" is not a table or materialized view",
     290             :                         RelationGetRelationName(rel))));
     291             : 
     292           2 :     if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
     293           0 :         ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     294             :                         errmsg("only heap AM is supported")));
     295             : 
     296           2 :     statapprox_heap(rel, &stat);
     297             : 
     298           2 :     relation_close(rel, AccessShareLock);
     299             : 
     300           2 :     memset(nulls, 0, sizeof(nulls));
     301             : 
     302           2 :     values[i++] = Int64GetDatum(stat.table_len);
     303           2 :     values[i++] = Float8GetDatum(stat.scanned_percent);
     304           2 :     values[i++] = Int64GetDatum(stat.tuple_count);
     305           2 :     values[i++] = Int64GetDatum(stat.tuple_len);
     306           2 :     values[i++] = Float8GetDatum(stat.tuple_percent);
     307           2 :     values[i++] = Int64GetDatum(stat.dead_tuple_count);
     308           2 :     values[i++] = Int64GetDatum(stat.dead_tuple_len);
     309           2 :     values[i++] = Float8GetDatum(stat.dead_tuple_percent);
     310           2 :     values[i++] = Int64GetDatum(stat.free_space);
     311           2 :     values[i++] = Float8GetDatum(stat.free_percent);
     312             : 
     313           2 :     ret = heap_form_tuple(tupdesc, values, nulls);
     314           2 :     return HeapTupleGetDatum(ret);
     315             : }

Generated by: LCOV version 1.13