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

Generated by: LCOV version 1.13