LCOV - differential code coverage report
Current view: top level - contrib/pgstattuple - pgstattuple.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage HEAD vs 15 Lines: 30.6 % 183 56 127 56
Current Date: 2023-04-08 17:13:01 Functions: 58.8 % 17 10 7 10
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 30.6 % 183 56 127 56
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 58.8 % 17 10 7 10

 Age         Owner                  TLA  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                 : 
 6158 tgl                        42 CBC           1 : PG_MODULE_MAGIC;
                                 43                 : 
 7860 ishii                      44               1 : PG_FUNCTION_INFO_V1(pgstattuple);
 2383 sfrost                     45               2 : PG_FUNCTION_INFO_V1(pgstattuple_v1_5);
 7241 bruce                      46               1 : PG_FUNCTION_INFO_V1(pgstattuplebyid);
 2383 sfrost                     47               2 : 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
 5050 bruce                      90               8 : 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 */
 6121                           106               8 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
 6121 bruce                     107 UBC           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                 :      */
 6121 bruce                     113 CBC           8 :     attinmeta = TupleDescGetAttInMetadata(tupdesc);
                                114                 : 
                                115               8 :     if (stat->table_len == 0)
                                116                 :     {
                                117               8 :         tuple_percent = 0.0;
                                118               8 :         dead_tuple_percent = 0.0;
                                119               8 :         free_percent = 0.0;
                                120                 :     }
                                121                 :     else
                                122                 :     {
 6121 bruce                     123 UBC           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                 :      */
 6121 bruce                     133 CBC          80 :     for (i = 0; i < NCOLUMNS; i++)
                                134              72 :         values[i] = values_buf[i];
                                135               8 :     i = 0;
                                136               8 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->table_len);
                                137               8 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_count);
                                138               8 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->tuple_len);
                                139               8 :     snprintf(values[i++], NCHARS, "%.2f", tuple_percent);
                                140               8 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_count);
                                141               8 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->dead_tuple_len);
                                142               8 :     snprintf(values[i++], NCHARS, "%.2f", dead_tuple_percent);
                                143               8 :     snprintf(values[i++], NCHARS, INT64_FORMAT, stat->free_space);
                                144               8 :     snprintf(values[i++], NCHARS, "%.2f", free_percent);
                                145                 : 
                                146                 :     /* build a tuple */
                                147               8 :     tuple = BuildTupleFromCStrings(attinmeta, values);
                                148                 : 
                                149                 :     /* make the tuple into a datum */
                                150               8 :     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
 7860 ishii                     167 UBC           0 : pgstattuple(PG_FUNCTION_ARGS)
                                168                 : {
 2219 noah                      169               0 :     text       *relname = PG_GETARG_TEXT_PP(0);
                                170                 :     RangeVar   *relrv;
                                171                 :     Relation    rel;
                                172                 : 
 5705 tgl                       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 */
 6526 neilc                     179               0 :     relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
 6121 bruce                     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
 2383 sfrost                    193 CBC           9 : pgstattuple_v1_5(PG_FUNCTION_ARGS)
                                194                 : {
 2219 noah                      195               9 :     text       *relname = PG_GETARG_TEXT_PP(0);
                                196                 :     RangeVar   *relrv;
                                197                 :     Relation    rel;
                                198                 : 
                                199                 :     /* open relation */
 2383 sfrost                    200               9 :     relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
                                201               9 :     rel = relation_openrv(relrv, AccessShareLock);
                                202                 : 
                                203               9 :     PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
                                204                 : }
                                205                 : 
                                206                 : /* Must keep superuser() check, see above. */
                                207                 : Datum
 7241 bruce                     208 UBC           0 : pgstattuplebyid(PG_FUNCTION_ARGS)
                                209                 : {
                                210               0 :     Oid         relid = PG_GETARG_OID(0);
                                211                 :     Relation    rel;
                                212                 : 
 5705 tgl                       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 */
 6121 bruce                     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
 2383 sfrost                    226 CBC           3 : pgstattuplebyid_v1_5(PG_FUNCTION_ARGS)
                                227                 : {
                                228               3 :     Oid         relid = PG_GETARG_OID(0);
                                229                 :     Relation    rel;
                                230                 : 
                                231                 :     /* open relation */
                                232               3 :     rel = relation_open(relid, AccessShareLock);
                                233                 : 
                                234               3 :     PG_RETURN_DATUM(pgstat_relation(rel, fcinfo));
                                235                 : }
                                236                 : 
                                237                 : /*
                                238                 :  * pgstat_relation
                                239                 :  */
                                240                 : static Datum
 6121 bruce                     241              12 : 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                 :      */
 5122 tgl                       250              12 :     if (RELATION_IS_OTHER_TEMP(rel))
 5122 tgl                       251 UBC           0 :         ereport(ERROR,
                                252                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                253                 :                  errmsg("cannot access temporary tables of other sessions")));
                                254                 : 
  492 peter                     255 CBC          12 :     if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind) ||
                                256               4 :         rel->rd_rel->relkind == RELKIND_SEQUENCE)
                                257                 :     {
  332 tgl                       258               8 :         return pgstat_heap(rel, fcinfo);
                                259                 :     }
  492 peter                     260               4 :     else if (rel->rd_rel->relkind == RELKIND_INDEX)
                                261                 :     {
  332 tgl                       262 UBC           0 :         switch (rel->rd_rel->relam)
                                263                 :         {
                                264               0 :             case BTREE_AM_OID:
                                265               0 :                 return pgstat_index(rel, BTREE_METAPAGE + 1,
                                266                 :                                     pgstat_btree_page, fcinfo);
                                267               0 :             case HASH_AM_OID:
                                268               0 :                 return pgstat_index(rel, HASH_METAPAGE + 1,
                                269                 :                                     pgstat_hash_page, fcinfo);
                                270               0 :             case GIST_AM_OID:
                                271               0 :                 return pgstat_index(rel, GIST_ROOT_BLKNO + 1,
                                272                 :                                     pgstat_gist_page, fcinfo);
                                273               0 :             case GIN_AM_OID:
                                274               0 :                 err = "gin index";
                                275               0 :                 break;
                                276               0 :             case SPGIST_AM_OID:
                                277               0 :                 err = "spgist index";
                                278               0 :                 break;
                                279               0 :             case BRIN_AM_OID:
                                280               0 :                 err = "brin index";
                                281               0 :                 break;
                                282               0 :             default:
                                283               0 :                 err = "unknown index";
                                284               0 :                 break;
                                285                 :         }
                                286               0 :         ereport(ERROR,
                                287                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                288                 :                  errmsg("index \"%s\" (%s) is not supported",
                                289                 :                         RelationGetRelationName(rel), err)));
                                290                 :     }
                                291                 :     else
                                292                 :     {
  332 tgl                       293 CBC           4 :         ereport(ERROR,
                                294                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                295                 :                  errmsg("cannot get tuple-level statistics for relation \"%s\"",
                                296                 :                         RelationGetRelationName(rel)),
                                297                 :                  errdetail_relkind_not_supported(rel->rd_rel->relkind)));
                                298                 :     }
                                299                 : 
                                300                 :     return 0;                   /* should not happen */
                                301                 : }
                                302                 : 
                                303                 : /*
                                304                 :  * pgstat_heap -- returns live/dead tuples info in a heap
                                305                 :  */
                                306                 : static Datum
 6121 bruce                     307               8 : pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
                                308                 : {
                                309                 :     TableScanDesc scan;
                                310                 :     HeapScanDesc hscan;
                                311                 :     HeapTuple   tuple;
                                312                 :     BlockNumber nblocks;
 6031                           313               8 :     BlockNumber block = 0;      /* next block to count free space in */
                                314                 :     BlockNumber tupblock;
                                315                 :     Buffer      buffer;
                                316               8 :     pgstattuple_type stat = {0};
                                317                 :     SnapshotData SnapshotDirty;
                                318                 : 
 1469 andres                    319               8 :     if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
 1469 andres                    320 UBC           0 :         ereport(ERROR,
                                321                 :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                322                 :                  errmsg("only heap AM is supported")));
                                323                 : 
                                324                 :     /* Disable syncscan because we assume we scan from block zero upwards */
 1490 andres                    325 CBC           8 :     scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false);
                                326               8 :     hscan = (HeapScanDesc) scan;
                                327                 : 
 3545 rhaas                     328               8 :     InitDirtySnapshot(SnapshotDirty);
                                329                 : 
 1418 tgl                       330               8 :     nblocks = hscan->rs_nblocks; /* # blocks to be scanned */
                                331                 : 
                                332                 :     /* scan the relation */
 7629                           333               8 :     while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
                                334                 :     {
 4755 tgl                       335 UBC           0 :         CHECK_FOR_INTERRUPTS();
                                336                 : 
                                337                 :         /* must hold a buffer lock to call HeapTupleSatisfiesVisibility */
 1490 andres                    338               0 :         LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
                                339                 : 
                                340               0 :         if (HeapTupleSatisfiesVisibility(tuple, &SnapshotDirty, hscan->rs_cbuf))
                                341                 :         {
 6121 bruce                     342               0 :             stat.tuple_len += tuple->t_len;
                                343               0 :             stat.tuple_count++;
                                344                 :         }
                                345                 :         else
                                346                 :         {
                                347               0 :             stat.dead_tuple_len += tuple->t_len;
                                348               0 :             stat.dead_tuple_count++;
                                349                 :         }
                                350                 : 
 1490 andres                    351               0 :         LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
                                352                 : 
                                353                 :         /*
                                354                 :          * To avoid physically reading the table twice, try to do the
                                355                 :          * free-space scan in parallel with the heap scan.  However,
                                356                 :          * heap_getnext may find no tuples on a given page, so we cannot
                                357                 :          * simply examine the pages returned by the heap scan.
                                358                 :          */
 2203 alvherre                  359               0 :         tupblock = ItemPointerGetBlockNumber(&tuple->t_self);
                                360                 : 
 7781 tgl                       361               0 :         while (block <= tupblock)
                                362                 :         {
 4755                           363               0 :             CHECK_FOR_INTERRUPTS();
                                364                 : 
 3205 noah                      365               0 :             buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
                                366                 :                                         RBM_NORMAL, hscan->rs_strategy);
 6750 tgl                       367               0 :             LockBuffer(buffer, BUFFER_LOCK_SHARE);
 2545 kgrittn                   368               0 :             stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
 5648 tgl                       369               0 :             UnlockReleaseBuffer(buffer);
 7781                           370               0 :             block++;
                                371                 :         }
                                372                 :     }
                                373                 : 
 7781 tgl                       374 CBC           8 :     while (block < nblocks)
                                375                 :     {
 4755 tgl                       376 UBC           0 :         CHECK_FOR_INTERRUPTS();
                                377                 : 
 3205 noah                      378               0 :         buffer = ReadBufferExtended(rel, MAIN_FORKNUM, block,
                                379                 :                                     RBM_NORMAL, hscan->rs_strategy);
 5648 tgl                       380               0 :         LockBuffer(buffer, BUFFER_LOCK_SHARE);
 2545 kgrittn                   381               0 :         stat.free_space += PageGetHeapFreeSpace((Page) BufferGetPage(buffer));
 5648 tgl                       382               0 :         UnlockReleaseBuffer(buffer);
 7781                           383               0 :         block++;
                                384                 :     }
                                385                 : 
 1490 andres                    386 CBC           8 :     table_endscan(scan);
 6121 bruce                     387               8 :     relation_close(rel, AccessShareLock);
                                388                 : 
 2118 tgl                       389               8 :     stat.table_len = (uint64) nblocks * BLCKSZ;
                                390                 : 
 6121 bruce                     391               8 :     return build_pgstattuple_type(&stat, fcinfo);
                                392                 : }
                                393                 : 
                                394                 : /*
                                395                 :  * pgstat_btree_page -- check tuples in a btree page
                                396                 :  */
                                397                 : static void
 4044 rhaas                     398 UBC           0 : pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno,
                                399                 :                   BufferAccessStrategy bstrategy)
                                400                 : {
                                401                 :     Buffer      buf;
                                402                 :     Page        page;
                                403                 : 
                                404               0 :     buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
 6121 bruce                     405               0 :     LockBuffer(buf, BT_READ);
 2545 kgrittn                   406               0 :     page = BufferGetPage(buf);
                                407                 : 
                                408                 :     /* Page is valid, see what to do with it */
 6121 bruce                     409               0 :     if (PageIsNew(page))
                                410                 :     {
                                411                 :         /* fully empty page */
                                412               0 :         stat->free_space += BLCKSZ;
                                413                 :     }
                                414                 :     else
                                415                 :     {
                                416                 :         BTPageOpaque opaque;
                                417                 : 
  373 michael                   418               0 :         opaque = BTPageGetOpaque(page);
 2029 tgl                       419               0 :         if (P_IGNORE(opaque))
                                420                 :         {
                                421                 :             /* deleted or half-dead page */
 6121 bruce                     422               0 :             stat->free_space += BLCKSZ;
                                423                 :         }
                                424               0 :         else if (P_ISLEAF(opaque))
                                425                 :         {
                                426               0 :             pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
 6031                           427               0 :                               PageGetMaxOffsetNumber(page));
                                428                 :         }
                                429                 :         else
                                430                 :         {
                                431                 :             /* internal page */
                                432                 :         }
                                433                 :     }
                                434                 : 
 6121                           435               0 :     _bt_relbuf(rel, buf);
                                436               0 : }
                                437                 : 
                                438                 : /*
                                439                 :  * pgstat_hash_page -- check tuples in a hash page
                                440                 :  */
                                441                 : static void
 4044 rhaas                     442               0 : pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno,
                                443                 :                  BufferAccessStrategy bstrategy)
                                444                 : {
                                445                 :     Buffer      buf;
                                446                 :     Page        page;
                                447                 : 
                                448               0 :     buf = _hash_getbuf_with_strategy(rel, blkno, HASH_READ, 0, bstrategy);
 2545 kgrittn                   449               0 :     page = BufferGetPage(buf);
                                450                 : 
 6121 bruce                     451               0 :     if (PageGetSpecialSize(page) == MAXALIGN(sizeof(HashPageOpaqueData)))
                                452                 :     {
                                453                 :         HashPageOpaque opaque;
                                454                 : 
  373 michael                   455               0 :         opaque = HashPageGetOpaque(page);
 2186 tgl                       456               0 :         switch (opaque->hasho_flag & LH_PAGE_TYPE)
                                457                 :         {
 6031 bruce                     458               0 :             case LH_UNUSED_PAGE:
                                459               0 :                 stat->free_space += BLCKSZ;
                                460               0 :                 break;
                                461               0 :             case LH_BUCKET_PAGE:
                                462                 :             case LH_OVERFLOW_PAGE:
                                463               0 :                 pgstat_index_page(stat, page, FirstOffsetNumber,
                                464               0 :                                   PageGetMaxOffsetNumber(page));
                                465               0 :                 break;
                                466               0 :             case LH_BITMAP_PAGE:
                                467                 :             case LH_META_PAGE:
                                468                 :             default:
                                469               0 :                 break;
                                470                 :         }
                                471                 :     }
                                472                 :     else
                                473                 :     {
                                474                 :         /* maybe corrupted */
                                475                 :     }
                                476                 : 
 6121                           477               0 :     _hash_relbuf(rel, buf);
                                478               0 : }
                                479                 : 
                                480                 : /*
                                481                 :  * pgstat_gist_page -- check tuples in a gist page
                                482                 :  */
                                483                 : static void
 4044 rhaas                     484               0 : pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno,
                                485                 :                  BufferAccessStrategy bstrategy)
                                486                 : {
                                487                 :     Buffer      buf;
                                488                 :     Page        page;
                                489                 : 
                                490               0 :     buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, bstrategy);
 6121 bruce                     491               0 :     LockBuffer(buf, GIST_SHARE);
                                492               0 :     gistcheckpage(rel, buf);
 2545 kgrittn                   493               0 :     page = BufferGetPage(buf);
                                494                 : 
 6121 bruce                     495               0 :     if (GistPageIsLeaf(page))
                                496                 :     {
                                497               0 :         pgstat_index_page(stat, page, FirstOffsetNumber,
 6031                           498               0 :                           PageGetMaxOffsetNumber(page));
                                499                 :     }
                                500                 :     else
                                501                 :     {
                                502                 :         /* root or node */
                                503                 :     }
                                504                 : 
 6121                           505               0 :     UnlockReleaseBuffer(buf);
                                506               0 : }
                                507                 : 
                                508                 : /*
                                509                 :  * pgstat_index -- returns live/dead tuples info in a generic index
                                510                 :  */
                                511                 : static Datum
 6061 tgl                       512               0 : pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn,
                                513                 :              FunctionCallInfo fcinfo)
                                514                 : {
                                515                 :     BlockNumber nblocks;
                                516                 :     BlockNumber blkno;
                                517                 :     BufferAccessStrategy bstrategy;
 6031 bruce                     518               0 :     pgstattuple_type stat = {0};
                                519                 : 
                                520                 :     /* prepare access strategy for this index */
 4044 rhaas                     521               0 :     bstrategy = GetAccessStrategy(BAS_BULKREAD);
                                522                 : 
 6121 bruce                     523               0 :     blkno = start;
                                524                 :     for (;;)
                                525                 :     {
                                526                 :         /* Get the current relation length */
                                527               0 :         LockRelationForExtension(rel, ExclusiveLock);
                                528               0 :         nblocks = RelationGetNumberOfBlocks(rel);
                                529               0 :         UnlockRelationForExtension(rel, ExclusiveLock);
                                530                 : 
                                531                 :         /* Quit if we've scanned the whole relation */
                                532               0 :         if (blkno >= nblocks)
                                533                 :         {
 2118 tgl                       534               0 :             stat.table_len = (uint64) nblocks * BLCKSZ;
                                535                 : 
 6121 bruce                     536               0 :             break;
                                537                 :         }
                                538                 : 
                                539               0 :         for (; blkno < nblocks; blkno++)
                                540                 :         {
 4755 tgl                       541               0 :             CHECK_FOR_INTERRUPTS();
                                542                 : 
 4044 rhaas                     543               0 :             pagefn(&stat, rel, blkno, bstrategy);
                                544                 :         }
                                545                 :     }
                                546                 : 
 6121 bruce                     547               0 :     relation_close(rel, AccessShareLock);
                                548                 : 
 6061 tgl                       549               0 :     return build_pgstattuple_type(&stat, fcinfo);
                                550                 : }
                                551                 : 
                                552                 : /*
                                553                 :  * pgstat_index_page -- for generic index page
                                554                 :  */
                                555                 : static void
 5050 bruce                     556               0 : pgstat_index_page(pgstattuple_type *stat, Page page,
                                557                 :                   OffsetNumber minoff, OffsetNumber maxoff)
                                558                 : {
                                559                 :     OffsetNumber i;
                                560                 : 
 6121                           561               0 :     stat->free_space += PageGetFreeSpace(page);
                                562                 : 
                                563               0 :     for (i = minoff; i <= maxoff; i = OffsetNumberNext(i))
                                564                 :     {
 6031                           565               0 :         ItemId      itemid = PageGetItemId(page, i);
                                566                 : 
 5688 tgl                       567               0 :         if (ItemIdIsDead(itemid))
                                568                 :         {
 6121 bruce                     569               0 :             stat->dead_tuple_count++;
                                570               0 :             stat->dead_tuple_len += ItemIdGetLength(itemid);
                                571                 :         }
                                572                 :         else
                                573                 :         {
                                574               0 :             stat->tuple_count++;
                                575               0 :             stat->tuple_len += ItemIdGetLength(itemid);
                                576                 :         }
                                577                 :     }
 7860 ishii                     578               0 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a