LCOV - differential code coverage report
Current view: top level - contrib/pgstattuple - pgstattuple.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 39.8 % 186 74 1 111 1 73 1 1
Current Date: 2025-09-06 07:49:51 +0900 Functions: 58.8 % 17 10 7 1 9
Baseline: lcov-20250906-005545-baseline Branches: 28.3 % 92 26 66 26
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 50.0 % 2 1 1 1
(30,360] days: 100.0 % 3 3 3
(360..) days: 38.7 % 181 70 111 70
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 56.2 % 16 9 7 1 8
Branch coverage date bins:
(30,360] days: 75.0 % 4 3 1 3
(360..) days: 26.1 % 88 23 65 23

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

Generated by: LCOV version 2.4-beta