LCOV - code coverage report
Current view: top level - contrib/pgstattuple - pgstattuple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 74 186 39.8 %
Date: 2024-12-12 16:15:21 Functions: 10 17 58.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/pgstattuple/pgstattuple.c
       3             :  *
       4             :  * Copyright (c) 2001,2002  Tatsuo Ishii
       5             :  *
       6             :  * Permission to use, copy, modify, and distribute this software and
       7             :  * its documentation for any purpose, without fee, and without a
       8             :  * written agreement is hereby granted, provided that the above
       9             :  * copyright notice and this paragraph and the following two
      10             :  * paragraphs appear in all copies.
      11             :  *
      12             :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
      13             :  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
      14             :  * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
      15             :  * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
      16             :  * OF THE POSSIBILITY OF SUCH DAMAGE.
      17             :  *
      18             :  * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
      19             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      20             :  * A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
      21             :  * IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
      22             :  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
      23             :  */
      24             : 
      25             : #include "postgres.h"
      26             : 
      27             : #include "access/gist_private.h"
      28             : #include "access/hash.h"
      29             : #include "access/heapam.h"
      30             : #include "access/nbtree.h"
      31             : #include "access/relscan.h"
      32             : #include "access/tableam.h"
      33             : #include "catalog/namespace.h"
      34             : #include "catalog/pg_am_d.h"
      35             : #include "funcapi.h"
      36             : #include "miscadmin.h"
      37             : #include "storage/bufmgr.h"
      38             : #include "storage/lmgr.h"
      39             : #include "utils/varlena.h"
      40             : 
      41           2 : PG_MODULE_MAGIC;
      42             : 
      43           2 : PG_FUNCTION_INFO_V1(pgstattuple);
      44           4 : PG_FUNCTION_INFO_V1(pgstattuple_v1_5);
      45           2 : PG_FUNCTION_INFO_V1(pgstattuplebyid);
      46           4 : PG_FUNCTION_INFO_V1(pgstattuplebyid_v1_5);
      47             : 
      48             : /*
      49             :  * struct pgstattuple_type
      50             :  *
      51             :  * tuple_percent, dead_tuple_percent and free_percent are computable,
      52             :  * so not defined here.
      53             :  */
      54             : typedef struct pgstattuple_type
      55             : {
      56             :     uint64      table_len;
      57             :     uint64      tuple_count;
      58             :     uint64      tuple_len;
      59             :     uint64      dead_tuple_count;
      60             :     uint64      dead_tuple_len;
      61             :     uint64      free_space;     /* free/reusable space in bytes */
      62             : } pgstattuple_type;
      63             : 
      64             : typedef void (*pgstat_page) (pgstattuple_type *, Relation, BlockNumber,
      65             :                              BufferAccessStrategy);
      66             : 
      67             : static Datum build_pgstattuple_type(pgstattuple_type *stat,
      68             :                                     FunctionCallInfo fcinfo);
      69             : static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo);
      70             : static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo);
      71             : static void pgstat_btree_page(pgstattuple_type *stat,
      72             :                               Relation rel, BlockNumber blkno,
      73             :                               BufferAccessStrategy bstrategy);
      74             : static void pgstat_hash_page(pgstattuple_type *stat,
      75             :                              Relation rel, BlockNumber blkno,
      76             :                              BufferAccessStrategy bstrategy);
      77             : static void pgstat_gist_page(pgstattuple_type *stat,
      78             :                              Relation rel, BlockNumber blkno,
      79             :                              BufferAccessStrategy bstrategy);
      80             : static Datum pgstat_index(Relation rel, BlockNumber start,
      81             :                           pgstat_page pagefn, FunctionCallInfo fcinfo);
      82             : static void pgstat_index_page(pgstattuple_type *stat, Page page,
      83             :                               OffsetNumber minoff, OffsetNumber maxoff);
      84             : 
      85             : /*
      86             :  * build_pgstattuple_type -- build a pgstattuple_type tuple
      87             :  */
      88             : static Datum
      89          18 : build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
      90             : {
      91             : #define NCOLUMNS    9
      92             : #define NCHARS      314
      93             : 
      94             :     HeapTuple   tuple;
      95             :     char       *values[NCOLUMNS];
      96             :     char        values_buf[NCOLUMNS][NCHARS];
      97             :     int         i;
      98             :     double      tuple_percent;
      99             :     double      dead_tuple_percent;
     100             :     double      free_percent;   /* free/reusable space in % */
     101             :     TupleDesc   tupdesc;
     102             :     AttInMetadata *attinmeta;
     103             : 
     104             :     /* Build a tuple descriptor for our result type */
     105          18 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     106           0 :         elog(ERROR, "return type must be a row type");
     107             : 
     108             :     /*
     109             :      * Generate attribute metadata needed later to produce tuples from raw C
     110             :      * strings
     111             :      */
     112          18 :     attinmeta = TupleDescGetAttInMetadata(tupdesc);
     113             : 
     114          18 :     if (stat->table_len == 0)
     115             :     {
     116          16 :         tuple_percent = 0.0;
     117          16 :         dead_tuple_percent = 0.0;
     118          16 :         free_percent = 0.0;
     119             :     }
     120             :     else
     121             :     {
     122           2 :         tuple_percent = 100.0 * stat->tuple_len / stat->table_len;
     123           2 :         dead_tuple_percent = 100.0 * stat->dead_tuple_len / stat->table_len;
     124           2 :         free_percent = 100.0 * stat->free_space / stat->table_len;
     125             :     }
     126             : 
     127             :     /*
     128             :      * Prepare a values array for constructing the tuple. This should be an
     129             :      * array of C strings which will be processed later by the appropriate
     130             :      * "in" functions.
     131             :      */
     132         180 :     for (i = 0; i < NCOLUMNS; i++)
     133         162 :         values[i] = values_buf[i];
     134          18 :     i = 0;
     135          18 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->table_len);
     136          18 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_count);
     137          18 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_len);
     138          18 :     snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
     139          18 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_count);
     140          18 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_len);
     141          18 :     snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
     142          18 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->free_space);
     143          18 :     snprintf(values[i++], NCHARS, "%.2f", free_percent);
     144             : 
     145             :     /* build a tuple */
     146          18 :     tuple = BuildTupleFromCStrings(attinmeta, values);
     147             : 
     148             :     /* make the tuple into a datum */
     149          18 :     return HeapTupleGetDatum(tuple);
     150             : }
     151             : 
     152             : /* ----------
     153             :  * pgstattuple:
     154             :  * returns live/dead tuples info
     155             :  *
     156             :  * C FUNCTION definition
     157             :  * pgstattuple(text) returns pgstattuple_type
     158             :  *
     159             :  * The superuser() check here must be kept as the library might be upgraded
     160             :  * without the extension being upgraded, meaning that in pre-1.5 installations
     161             :  * these functions could be called by any user.
     162             :  * ----------
     163             :  */
     164             : 
     165             : Datum
     166           0 : pgstattuple(PG_FUNCTION_ARGS)
     167             : {
     168           0 :     text       *relname = PG_GETARG_TEXT_PP(0);
     169             :     RangeVar   *relrv;
     170             :     Relation    rel;
     171             : 
     172           0 :     if (!superuser())
     173           0 :         ereport(ERROR,
     174             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     175             :                  errmsg("must be superuser to use pgstattuple functions")));
     176             : 
     177             :     /* open relation */
     178           0 :     relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
     179           0 :     rel = relation_openrv(relrv, AccessShareLock);
     180             : 
     181           0 :     PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
     182             : }
     183             : 
     184             : /*
     185             :  * As of pgstattuple version 1.5, we no longer need to check if the user
     186             :  * is a superuser because we REVOKE EXECUTE on the function from PUBLIC.
     187             :  * Users can then grant access to it based on their policies.
     188             :  *
     189             :  * Otherwise identical to pgstattuple (above).
     190             :  */
     191             : Datum
     192          20 : pgstattuple_v1_5(PG_FUNCTION_ARGS)
     193             : {
     194          20 :     text       *relname = PG_GETARG_TEXT_PP(0);
     195             :     RangeVar   *relrv;
     196             :     Relation    rel;
     197             : 
     198             :     /* open relation */
     199          20 :     relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
     200          20 :     rel = relation_openrv(relrv, AccessShareLock);
     201             : 
     202          20 :     PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
     203             : }
     204             : 
     205             : /* Must keep superuser() check, see above. */
     206             : Datum
     207           0 : pgstattuplebyid(PG_FUNCTION_ARGS)
     208             : {
     209           0 :     Oid         relid = PG_GETARG_OID(0);
     210             :     Relation    rel;
     211             : 
     212           0 :     if (!superuser())
     213           0 :         ereport(ERROR,
     214             :                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
     215             :                  errmsg("must be superuser to use pgstattuple functions")));
     216             : 
     217             :     /* open relation */
     218           0 :     rel = relation_open(relid, AccessShareLock);
     219             : 
     220           0 :     PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
     221             : }
     222             : 
     223             : /* Remove superuser() check for 1.5 version, see above */
     224             : Datum
     225           6 : pgstattuplebyid_v1_5(PG_FUNCTION_ARGS)
     226             : {
     227           6 :     Oid         relid = PG_GETARG_OID(0);
     228             :     Relation    rel;
     229             : 
     230             :     /* open relation */
     231           6 :     rel = relation_open(relid, AccessShareLock);
     232             : 
     233           6 :     PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
     234             : }
     235             : 
     236             : /*
     237             :  * pgstat_relation
     238             :  */
     239             : static Datum
     240          26 : pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
     241             : {
     242             :     const char *err;
     243             : 
     244             :     /*
     245             :      * Reject attempts to read non-local temporary relations; we would be
     246             :      * likely to get wrong data since we have no visibility into the owning
     247             :      * session's local buffers.
     248             :      */
     249          26 :     if (RELATION_IS_OTHER_TEMP(rel))
     250           0 :         ereport(ERROR,
     251             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     252             :                  errmsg("cannot access temporary tables of other sessions")));
     253             : 
     254          26 :     if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind) ||
     255          10 :         rel->rd_rel->relkind == RELKIND_SEQUENCE)
     256             :     {
     257          18 :         return pgstat_heap(rel, fcinfo);
     258             :     }
     259           8 :     else if (rel->rd_rel->relkind == RELKIND_INDEX)
     260             :     {
     261             :         /* see pgstatindex_impl */
     262           0 :         if (!rel->rd_index->indisvalid)
     263           0 :             ereport(ERROR,
     264             :                     (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     265             :                      errmsg("index \"%s\" is not valid",
     266             :                             RelationGetRelationName(rel))));
     267             : 
     268           0 :         switch (rel->rd_rel->relam)
     269             :         {
     270           0 :             case BTREE_AM_OID:
     271           0 :                 return pgstat_index(rel, BTREE_METAPAGE + 1,
     272             :                                     pgstat_btree_page, fcinfo);
     273           0 :             case HASH_AM_OID:
     274           0 :                 return pgstat_index(rel, HASH_METAPAGE + 1,
     275             :                                     pgstat_hash_page, fcinfo);
     276           0 :             case GIST_AM_OID:
     277           0 :                 return pgstat_index(rel, GIST_ROOT_BLKNO + 1,
     278             :                                     pgstat_gist_page, fcinfo);
     279           0 :             case GIN_AM_OID:
     280           0 :                 err = "gin index";
     281           0 :                 break;
     282           0 :             case SPGIST_AM_OID:
     283           0 :                 err = "spgist index";
     284           0 :                 break;
     285           0 :             case BRIN_AM_OID:
     286           0 :                 err = "brin index";
     287           0 :                 break;
     288           0 :             default:
     289           0 :                 err = "unknown index";
     290           0 :                 break;
     291             :         }
     292           0 :         ereport(ERROR,
     293             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     294             :                  errmsg("index \"%s\" (%s) is not supported",
     295             :                         RelationGetRelationName(rel), err)));
     296             :     }
     297             :     else
     298             :     {
     299           8 :         ereport(ERROR,
     300             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     301             :                  errmsg("cannot get tuple-level statistics for relation \"%s\"",
     302             :                         RelationGetRelationName(rel)),
     303             :                  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
     304             :     }
     305             : 
     306             :     return 0;                   /* should not happen */
     307             : }
     308             : 
     309             : /*
     310             :  * pgstat_heap -- returns live/dead tuples info in a heap
     311             :  */
     312             : static Datum
     313          18 : pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
     314             : {
     315             :     TableScanDesc scan;
     316             :     HeapScanDesc hscan;
     317             :     HeapTuple   tuple;
     318             :     BlockNumber nblocks;
     319          18 :     BlockNumber block = 0;      /* next block to count free space in */
     320             :     BlockNumber tupblock;
     321             :     Buffer      buffer;
     322          18 :     pgstattuple_type stat = {0};
     323             :     SnapshotData SnapshotDirty;
     324             : 
     325             :     /*
     326             :      * Sequences always use heap AM, but they don't show that in the catalogs.
     327             :      */
     328          18 :     if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
     329          16 :         rel->rd_rel->relam != HEAP_TABLE_AM_OID)
     330           0 :         ereport(ERROR,
     331             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     332             :                  errmsg("only heap AM is supported")));
     333             : 
     334             :     /* Disable syncscan because we assume we scan from block zero upwards */
     335          18 :     scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
     336          18 :     hscan = (HeapScanDesc) scan;
     337             : 
     338          18 :     InitDirtySnapshot(SnapshotDirty);
     339             : 
     340          18 :     nblocks = hscan->rs_nblocks; /* # blocks to be scanned */
     341             : 
     342             :     /* scan the relation */
     343          20 :     while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
     344             :     {
     345           2 :         CHECK_FOR_INTERRUPTS();
     346             : 
     347             :         /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
     348           2 :         LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
     349             : 
     350           2 :         if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, hscan->rs_cbuf))
     351             :         {
     352           2 :             stat.tuple_len += tuple->t_len;
     353           2 :             stat.tuple_count++;
     354             :         }
     355             :         else
     356             :         {
     357           0 :             stat.dead_tuple_len += tuple->t_len;
     358           0 :             stat.dead_tuple_count++;
     359             :         }
     360             : 
     361           2 :         LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
     362             : 
     363             :         /*
     364             :          * To avoid physically reading the table twice, try to do the
     365             :          * free-space scan in parallel with the heap scan.  However,
     366             :          * heap_getnext may find no tuples on a given page, so we cannot
     367             :          * simply examine the pages returned by the heap scan.
     368             :          */
     369           2 :         tupblock = ItemPointerGetBlockNumber(&tuple->t_self);
     370             : 
     371           4 :         while (block <= tupblock)
     372             :         {
     373           2 :             CHECK_FOR_INTERRUPTS();
     374             : 
     375           2 :             buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
     376             :                                         RBM_NORMAL, hscan->rs_strategy);
     377           2 :             LockBuffer(buffer, BUFFER_LOCK_SHARE);
     378           2 :             stat.free_space += PageGetExactFreeSpace((Page) BufferGetPage(buffer));
     379           2 :             UnlockReleaseBuffer(buffer);
     380           2 :             block++;
     381             :         }
     382             :     }
     383             : 
     384          18 :     while (block < nblocks)
     385             :     {
     386           0 :         CHECK_FOR_INTERRUPTS();
     387             : 
     388           0 :         buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
     389             :                                     RBM_NORMAL, hscan->rs_strategy);
     390           0 :         LockBuffer(buffer, BUFFER_LOCK_SHARE);
     391           0 :         stat.free_space += PageGetExactFreeSpace((Page) BufferGetPage(buffer));
     392           0 :         UnlockReleaseBuffer(buffer);
     393           0 :         block++;
     394             :     }
     395             : 
     396          18 :     table_endscan(scan);
     397          18 :     relation_close(rel, AccessShareLock);
     398             : 
     399          18 :     stat.table_len = (uint64) nblocks * BLCKSZ;
     400             : 
     401          18 :     return build_pgstattuple_type(&stat, fcinfo);
     402             : }
     403             : 
     404             : /*
     405             :  * pgstat_btree_page -- check tuples in a btree page
     406             :  */
     407             : static void
     408           0 : pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno,
     409             :                   BufferAccessStrategy bstrategy)
     410             : {
     411             :     Buffer      buf;
     412             :     Page        page;
     413             : 
     414           0 :     buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
     415           0 :     LockBuffer(buf, BT_READ);
     416           0 :     page = BufferGetPage(buf);
     417             : 
     418             :     /* Page is valid, see what to do with it */
     419           0 :     if (PageIsNew(page))
     420             :     {
     421             :         /* fully empty page */
     422           0 :         stat->free_space += BLCKSZ;
     423             :     }
     424             :     else
     425             :     {
     426             :         BTPageOpaque opaque;
     427             : 
     428           0 :         opaque = BTPageGetOpaque(page);
     429           0 :         if (P_IGNORE(opaque))
     430             :         {
     431             :             /* deleted or half-dead page */
     432           0 :             stat->free_space += BLCKSZ;
     433             :         }
     434           0 :         else if (P_ISLEAF(opaque))
     435             :         {
     436           0 :             pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
     437           0 :                               PageGetMaxOffsetNumber(page));
     438             :         }
     439             :         else
     440             :         {
     441             :             /* internal page */
     442             :         }
     443             :     }
     444             : 
     445           0 :     _bt_relbuf(rel, buf);
     446           0 : }
     447             : 
     448             : /*
     449             :  * pgstat_hash_page -- check tuples in a hash page
     450             :  */
     451             : static void
     452           0 : pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno,
     453             :                  BufferAccessStrategy bstrategy)
     454             : {
     455             :     Buffer      buf;
     456             :     Page        page;
     457             : 
     458           0 :     buf = _hash_getbuf_with_strategy(rel, blkno, HASH_READ, 0, bstrategy);
     459           0 :     page = BufferGetPage(buf);
     460             : 
     461           0 :     if (PageGetSpecialSize(page) == MAXALIGN(sizeof(HashPageOpaqueData)))
     462             :     {
     463             :         HashPageOpaque opaque;
     464             : 
     465           0 :         opaque = HashPageGetOpaque(page);
     466           0 :         switch (opaque->hasho_flag & LH_PAGE_TYPE)
     467             :         {
     468           0 :             case LH_UNUSED_PAGE:
     469           0 :                 stat->free_space += BLCKSZ;
     470           0 :                 break;
     471           0 :             case LH_BUCKET_PAGE:
     472             :             case LH_OVERFLOW_PAGE:
     473           0 :                 pgstat_index_page(stat, page, FirstOffsetNumber,
     474           0 :                                   PageGetMaxOffsetNumber(page));
     475           0 :                 break;
     476           0 :             case LH_BITMAP_PAGE:
     477             :             case LH_META_PAGE:
     478             :             default:
     479           0 :                 break;
     480             :         }
     481             :     }
     482             :     else
     483             :     {
     484             :         /* maybe corrupted */
     485             :     }
     486             : 
     487           0 :     _hash_relbuf(rel, buf);
     488           0 : }
     489             : 
     490             : /*
     491             :  * pgstat_gist_page -- check tuples in a gist page
     492             :  */
     493             : static void
     494           0 : pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno,
     495             :                  BufferAccessStrategy bstrategy)
     496             : {
     497             :     Buffer      buf;
     498             :     Page        page;
     499             : 
     500           0 :     buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
     501           0 :     LockBuffer(buf, GIST_SHARE);
     502           0 :     gistcheckpage(rel, buf);
     503           0 :     page = BufferGetPage(buf);
     504             : 
     505           0 :     if (GistPageIsLeaf(page))
     506             :     {
     507           0 :         pgstat_index_page(stat, page, FirstOffsetNumber,
     508           0 :                           PageGetMaxOffsetNumber(page));
     509             :     }
     510             :     else
     511             :     {
     512             :         /* root or node */
     513             :     }
     514             : 
     515           0 :     UnlockReleaseBuffer(buf);
     516           0 : }
     517             : 
     518             : /*
     519             :  * pgstat_index -- returns live/dead tuples info in a generic index
     520             :  */
     521             : static Datum
     522           0 : pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn,
     523             :              FunctionCallInfo fcinfo)
     524             : {
     525             :     BlockNumber nblocks;
     526             :     BlockNumber blkno;
     527             :     BufferAccessStrategy bstrategy;
     528           0 :     pgstattuple_type stat = {0};
     529             : 
     530             :     /* prepare access strategy for this index */
     531           0 :     bstrategy = GetAccessStrategy(BAS_BULKREAD);
     532             : 
     533           0 :     blkno = start;
     534             :     for (;;)
     535             :     {
     536             :         /* Get the current relation length */
     537           0 :         LockRelationForExtension(rel, ExclusiveLock);
     538           0 :         nblocks = RelationGetNumberOfBlocks(rel);
     539           0 :         UnlockRelationForExtension(rel, ExclusiveLock);
     540             : 
     541             :         /* Quit if we've scanned the whole relation */
     542           0 :         if (blkno >= nblocks)
     543             :         {
     544           0 :             stat.table_len = (uint64) nblocks * BLCKSZ;
     545             : 
     546           0 :             break;
     547             :         }
     548             : 
     549           0 :         for (; blkno < nblocks; blkno++)
     550             :         {
     551           0 :             CHECK_FOR_INTERRUPTS();
     552             : 
     553           0 :             pagefn(&stat, rel, blkno, bstrategy);
     554             :         }
     555             :     }
     556             : 
     557           0 :     relation_close(rel, AccessShareLock);
     558             : 
     559           0 :     return build_pgstattuple_type(&stat, fcinfo);
     560             : }
     561             : 
     562             : /*
     563             :  * pgstat_index_page -- for generic index page
     564             :  */
     565             : static void
     566           0 : pgstat_index_page(pgstattuple_type *stat, Page page,
     567             :                   OffsetNumber minoff, OffsetNumber maxoff)
     568             : {
     569             :     OffsetNumber i;
     570             : 
     571           0 :     stat->free_space += PageGetExactFreeSpace(page);
     572             : 
     573           0 :     for (i = minoff; i <= maxoff; i = OffsetNumberNext(i))
     574             :     {
     575           0 :         ItemId      itemid = PageGetItemId(page, i);
     576             : 
     577           0 :         if (ItemIdIsDead(itemid))
     578             :         {
     579           0 :             stat->dead_tuple_count++;
     580           0 :             stat->dead_tuple_len += ItemIdGetLength(itemid);
     581             :         }
     582             :         else
     583             :         {
     584           0 :             stat->tuple_count++;
     585           0 :             stat->tuple_len += ItemIdGetLength(itemid);
     586             :         }
     587             :     }
     588           0 : }

Generated by: LCOV version 1.14