LCOV - differential code coverage report
Current view: top level - contrib/pgstattuple - pgstattuple.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 39.2 % 199 78 3 118 5 73 1
Current Date: 2026-11-28 16:48:44 +0900 Functions: 61.1 % 18 11 7 2 9 1
Baseline: lcov-20261128-baseline Branches: 28.2 % 110 31 7 72 2 3 26
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 62.5 % 8 5 3 5
(360..) days: 38.2 % 191 73 118 73
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(360..) days: 56.2 % 16 9 7 9
Branch coverage date bins:
(7,30] days: 30.0 % 10 3 7 3
(360..) days: 28.0 % 100 28 72 2 26

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

Generated by: LCOV version 2.0-1