LCOV - differential code coverage report
Current view: top level - contrib/pgrowlocks - pgrowlocks.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 90.0 % 110 99 1 10 5 94 4
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 3 3 1 2
Baseline: lcov-20261128-baseline Branches: 64.2 % 53 34 5 14 1 33
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: 50.0 % 2 1 1 1
(30,360] days: 100.0 % 5 5 4 1
(360..) days: 90.3 % 103 93 10 93
Function coverage date bins:
(360..) days: 100.0 % 3 3 1 2
Branch coverage date bins:
(7,30] days: 16.7 % 6 1 5 1
(360..) days: 70.2 % 47 33 14 33

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * contrib/pgrowlocks/pgrowlocks.c
                                  3                 :                :  *
                                  4                 :                :  * Copyright (c) 2005-2006  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/heapam.h"
                                 28                 :                : #include "access/multixact.h"
                                 29                 :                : #include "access/relscan.h"
                                 30                 :                : #include "access/tableam.h"
                                 31                 :                : #include "access/xact.h"
                                 32                 :                : #include "access/xlog.h"
                                 33                 :                : #include "catalog/namespace.h"
                                 34                 :                : #include "catalog/pg_am_d.h"
                                 35                 :                : #include "catalog/pg_authid.h"
                                 36                 :                : #include "funcapi.h"
                                 37                 :                : #include "miscadmin.h"
                                 38                 :                : #include "storage/bufmgr.h"
                                 39                 :                : #include "storage/procarray.h"
                                 40                 :                : #include "utils/acl.h"
                                 41                 :                : #include "utils/fmgrprotos.h"
                                 42                 :                : #include "utils/rel.h"
                                 43                 :                : #include "utils/snapmgr.h"
                                 44                 :                : #include "utils/tuplestore.h"
                                 45                 :                : #include "utils/varlena.h"
                                 46                 :                : 
  612 tgl@sss.pgh.pa.us          47                 :CBC           2 : PG_MODULE_MAGIC_EXT(
                                 48                 :                :                     .name = "pgrowlocks",
                                 49                 :                :                     .version = PG_VERSION
                                 50                 :                : );
                                 51                 :                : 
 7524 ishii@postgresql.org       52                 :              2 : PG_FUNCTION_INFO_V1(pgrowlocks);
                                 53                 :                : 
                                 54                 :                : /* ----------
                                 55                 :                :  * pgrowlocks:
                                 56                 :                :  * returns tids of rows being locked
                                 57                 :                :  * ----------
                                 58                 :                :  */
                                 59                 :                : 
                                 60                 :                : #define NCHARS 32
                                 61                 :                : 
                                 62                 :                : #define     Atnum_tid       0
                                 63                 :                : #define     Atnum_xmax      1
                                 64                 :                : #define     Atnum_ismulti   2
                                 65                 :                : #define     Atnum_xids      3
                                 66                 :                : #define     Atnum_modes     4
                                 67                 :                : #define     Atnum_pids      5
                                 68                 :                : 
                                 69                 :                : Datum
                                 70                 :             12 : pgrowlocks(PG_FUNCTION_ARGS)
                                 71                 :                : {
 2448 tgl@sss.pgh.pa.us          72                 :             12 :     text       *relname = PG_GETARG_TEXT_PP(0);
                                 73                 :             12 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
                                 74                 :                :     AttInMetadata *attinmeta;
                                 75                 :                :     Relation    rel;
                                 76                 :                :     RangeVar   *relrv;
                                 77                 :                :     TableScanDesc scan;
                                 78                 :                :     HeapScanDesc hscan;
                                 79                 :                :     HeapTuple   tuple;
                                 80                 :                :     AclResult   aclresult;
                                 81                 :                :     char      **values;
                                 82                 :                : 
 1502 michael@paquier.xyz        83                 :             12 :     InitMaterializedSRF(fcinfo, 0);
                                 84                 :                : 
                                 85                 :                :     /* Access the table */
 2448 tgl@sss.pgh.pa.us          86                 :             12 :     relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
                                 87                 :             12 :     rel = relation_openrv(relrv, AccessShareLock);
                                 88                 :                : 
                                 89         [ -  + ]:             12 :     if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
 2448 tgl@sss.pgh.pa.us          90         [ #  # ]:UBC           0 :         ereport(ERROR,
                                 91                 :                :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                 92                 :                :                  errmsg("\"%s\" is a partitioned table",
                                 93                 :                :                         RelationGetRelationName(rel)),
                                 94                 :                :                  errdetail("Partitioned tables do not contain rows.")));
 2448 tgl@sss.pgh.pa.us          95         [ -  + ]:CBC          12 :     else if (rel->rd_rel->relkind != RELKIND_RELATION)
 2448 tgl@sss.pgh.pa.us          96         [ #  # ]:UBC           0 :         ereport(ERROR,
                                 97                 :                :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                                 98                 :                :                  errmsg("\"%s\" is not a table",
                                 99                 :                :                         RelationGetRelationName(rel))));
 1124 drowley@postgresql.o      100         [ -  + ]:CBC          12 :     else if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
 1124 drowley@postgresql.o      101         [ #  # ]:UBC           0 :         ereport(ERROR,
                                102                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                103                 :                :                  errmsg("only heap AM is supported")));
                                104                 :                : 
                                105                 :                :     /*
                                106                 :                :      * check permissions: must have SELECT on table or be in
                                107                 :                :      * pg_stat_scan_tables
                                108                 :                :      */
 2448 tgl@sss.pgh.pa.us         109                 :CBC          12 :     aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
                                110                 :                :                                   ACL_SELECT);
                                111         [ -  + ]:             12 :     if (aclresult != ACLCHECK_OK)
 1706 mail@joeconway.com        112                 :UBC           0 :         aclresult = has_privs_of_role(GetUserId(), ROLE_PG_STAT_SCAN_TABLES) ? ACLCHECK_OK : ACLCHECK_NO_PRIV;
                                113                 :                : 
 2448 tgl@sss.pgh.pa.us         114         [ -  + ]:CBC          12 :     if (aclresult != ACLCHECK_OK)
 2448 tgl@sss.pgh.pa.us         115                 :UBC           0 :         aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
                                116                 :              0 :                        RelationGetRelationName(rel));
                                117                 :                : 
   22 michael@paquier.xyz       118   [ -  +  -  - ]:GNC          12 :     if (!RelationIsPermanent(rel) && RecoveryInProgress())
   22 michael@paquier.xyz       119         [ #  # ]:UNC           0 :         ereport(ERROR,
                                120                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                121                 :                :                  errmsg("cannot access temporary or unlogged relations during recovery")));
                                122                 :                : 
                                123                 :                :     /* Scan the relation */
  243 melanieplageman@gmai      124                 :CBC          12 :     scan = table_beginscan(rel, GetActiveSnapshot(), 0, NULL, SO_NONE);
 2448 tgl@sss.pgh.pa.us         125                 :             12 :     hscan = (HeapScanDesc) scan;
                                126                 :                : 
 1726 michael@paquier.xyz       127                 :             12 :     attinmeta = TupleDescGetAttInMetadata(rsinfo->setDesc);
                                128                 :                : 
  103 michael@paquier.xyz       129                 :GNC          12 :     values = palloc_array(char *, rsinfo->setDesc->natts);
                                130                 :                : 
 7524 ishii@postgresql.org      131         [ +  + ]:CBC          36 :     while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
                                132                 :                :     {
                                133                 :                :         TM_Result   htsu;
                                134                 :                :         TransactionId xmax;
                                135                 :                :         uint16      infomask;
                                136                 :                : 
                                137                 :                :         /* must hold a buffer lock to call HeapTupleSatisfiesUpdate */
 2819 andres@anarazel.de        138                 :             24 :         LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE);
                                139                 :                : 
 4877 rhaas@postgresql.org      140                 :             24 :         htsu = HeapTupleSatisfiesUpdate(tuple,
                                141                 :                :                                         GetCurrentCommandId(false),
                                142                 :                :                                         hscan->rs_cbuf);
 5057 alvherre@alvh.no-ip.      143                 :             24 :         xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
                                144                 :             24 :         infomask = tuple->t_data->t_infomask;
                                145                 :                : 
                                146                 :                :         /*
                                147                 :                :          * A tuple is locked if HTSU returns BeingModified.
                                148                 :                :          */
 2807 andres@anarazel.de        149         [ +  + ]:             24 :         if (htsu == TM_BeingModified)
                                150                 :                :         {
  477 peter@eisentraut.org      151                 :             22 :             values[Atnum_tid] = DatumGetCString(DirectFunctionCall1(tidout,
                                152                 :                :                                                                     PointerGetDatum(&tuple->t_self)));
                                153                 :                : 
  103 michael@paquier.xyz       154                 :GNC          22 :             values[Atnum_xmax] = palloc_array(char, NCHARS);
 1816 peter@eisentraut.org      155                 :CBC          22 :             snprintf(values[Atnum_xmax], NCHARS, "%u", xmax);
 5057 alvherre@alvh.no-ip.      156         [ +  + ]:             22 :             if (infomask & HEAP_XMAX_IS_MULTI)
                                157                 :                :             {
                                158                 :                :                 MultiXactMember *members;
                                159                 :                :                 int         nmembers;
                                160                 :              8 :                 bool        first = true;
                                161                 :                :                 bool        allow_old;
                                162                 :                : 
                                163                 :              8 :                 values[Atnum_ismulti] = pstrdup("true");
                                164                 :                : 
 3809                           165                 :              8 :                 allow_old = HEAP_LOCKED_UPGRADED(infomask);
 4505                           166                 :              8 :                 nmembers = GetMultiXactIdMembers(xmax, &members, allow_old,
                                167                 :                :                                                  false);
 5057                           168         [ -  + ]:              8 :                 if (nmembers == -1)
                                169                 :                :                 {
 5057 alvherre@alvh.no-ip.      170                 :UBC           0 :                     values[Atnum_xids] = "{0}";
                                171                 :              0 :                     values[Atnum_modes] = "{transient upgrade status}";
                                172                 :              0 :                     values[Atnum_pids] = "{0}";
                                173                 :                :                 }
                                174                 :                :                 else
                                175                 :                :                 {
                                176                 :                :                     int         j;
                                177                 :                : 
 5057 alvherre@alvh.no-ip.      178                 :CBC           8 :                     values[Atnum_xids] = palloc(NCHARS * nmembers);
                                179                 :              8 :                     values[Atnum_modes] = palloc(NCHARS * nmembers);
                                180                 :              8 :                     values[Atnum_pids] = palloc(NCHARS * nmembers);
                                181                 :                : 
                                182                 :              8 :                     strcpy(values[Atnum_xids], "{");
                                183                 :              8 :                     strcpy(values[Atnum_modes], "{");
                                184                 :              8 :                     strcpy(values[Atnum_pids], "{");
                                185                 :                : 
                                186         [ +  + ]:             24 :                     for (j = 0; j < nmembers; j++)
                                187                 :                :                     {
                                188                 :                :                         char        buf[NCHARS];
                                189                 :                : 
                                190         [ +  + ]:             16 :                         if (!first)
                                191                 :                :                         {
                                192                 :              8 :                             strcat(values[Atnum_xids], ",");
                                193                 :              8 :                             strcat(values[Atnum_modes], ",");
                                194                 :              8 :                             strcat(values[Atnum_pids], ",");
                                195                 :                :                         }
 1816 peter@eisentraut.org      196                 :             16 :                         snprintf(buf, NCHARS, "%u", members[j].xid);
 5057 alvherre@alvh.no-ip.      197                 :             16 :                         strcat(values[Atnum_xids], buf);
                                198   [ +  +  +  +  :             16 :                         switch (members[j].status)
                                           +  +  - ]
                                199                 :                :                         {
                                200                 :              1 :                             case MultiXactStatusUpdate:
                                201                 :              1 :                                 snprintf(buf, NCHARS, "Update");
                                202                 :              1 :                                 break;
                                203                 :              1 :                             case MultiXactStatusNoKeyUpdate:
                                204                 :              1 :                                 snprintf(buf, NCHARS, "No Key Update");
                                205                 :              1 :                                 break;
                                206                 :              2 :                             case MultiXactStatusForUpdate:
                                207                 :              2 :                                 snprintf(buf, NCHARS, "For Update");
                                208                 :              2 :                                 break;
                                209                 :              2 :                             case MultiXactStatusForNoKeyUpdate:
                                210                 :              2 :                                 snprintf(buf, NCHARS, "For No Key Update");
                                211                 :              2 :                                 break;
                                212                 :              2 :                             case MultiXactStatusForShare:
 1159 bruce@momjian.us          213                 :              2 :                                 snprintf(buf, NCHARS, "For Share");
 5057 alvherre@alvh.no-ip.      214                 :              2 :                                 break;
                                215                 :              8 :                             case MultiXactStatusForKeyShare:
 1159 bruce@momjian.us          216                 :              8 :                                 snprintf(buf, NCHARS, "For Key Share");
 5057 alvherre@alvh.no-ip.      217                 :              8 :                                 break;
                                218                 :                :                         }
                                219                 :             16 :                         strcat(values[Atnum_modes], buf);
                                220                 :             16 :                         snprintf(buf, NCHARS, "%d",
                                221                 :             16 :                                  BackendXidGetPid(members[j].xid));
                                222                 :             16 :                         strcat(values[Atnum_pids], buf);
                                223                 :                : 
                                224                 :             16 :                         first = false;
                                225                 :                :                     }
                                226                 :                : 
                                227                 :              8 :                     strcat(values[Atnum_xids], "}");
                                228                 :              8 :                     strcat(values[Atnum_modes], "}");
                                229                 :              8 :                     strcat(values[Atnum_pids], "}");
                                230                 :                :                 }
                                231                 :                :             }
                                232                 :                :             else
                                233                 :                :             {
                                234                 :             14 :                 values[Atnum_ismulti] = pstrdup("false");
                                235                 :                : 
  103 michael@paquier.xyz       236                 :GNC          14 :                 values[Atnum_xids] = palloc_array(char, NCHARS);
 1816 peter@eisentraut.org      237                 :CBC          14 :                 snprintf(values[Atnum_xids], NCHARS, "{%u}", xmax);
                                238                 :                : 
 5057 alvherre@alvh.no-ip.      239                 :             14 :                 values[Atnum_modes] = palloc(NCHARS);
                                240         [ +  + ]:             14 :                 if (infomask & HEAP_XMAX_LOCK_ONLY)
                                241                 :                :                 {
                                242         [ +  + ]:             12 :                     if (HEAP_XMAX_IS_SHR_LOCKED(infomask))
                                243                 :              2 :                         snprintf(values[Atnum_modes], NCHARS, "{For Share}");
                                244         [ +  + ]:             10 :                     else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
                                245                 :              6 :                         snprintf(values[Atnum_modes], NCHARS, "{For Key Share}");
                                246         [ +  - ]:              4 :                     else if (HEAP_XMAX_IS_EXCL_LOCKED(infomask))
                                247                 :                :                     {
 5049                           248         [ +  + ]:              4 :                         if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
                                249                 :              2 :                             snprintf(values[Atnum_modes], NCHARS, "{For Update}");
                                250                 :                :                         else
                                251                 :              2 :                             snprintf(values[Atnum_modes], NCHARS, "{For No Key Update}");
                                252                 :                :                     }
                                253                 :                :                     else
                                254                 :                :                         /* neither keyshare nor exclusive bit it set */
 5057 alvherre@alvh.no-ip.      255                 :UBC           0 :                         snprintf(values[Atnum_modes], NCHARS,
                                256                 :                :                                  "{transient upgrade status}");
                                257                 :                :                 }
                                258                 :                :                 else
                                259                 :                :                 {
 5057 alvherre@alvh.no-ip.      260         [ +  + ]:CBC           2 :                     if (tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED)
                                261                 :              1 :                         snprintf(values[Atnum_modes], NCHARS, "{Update}");
                                262                 :                :                     else
 5049                           263                 :              1 :                         snprintf(values[Atnum_modes], NCHARS, "{No Key Update}");
                                264                 :                :                 }
                                265                 :                : 
  103 michael@paquier.xyz       266                 :GNC          14 :                 values[Atnum_pids] = palloc_array(char, NCHARS);
 5057 alvherre@alvh.no-ip.      267                 :CBC          14 :                 snprintf(values[Atnum_pids], NCHARS, "{%d}",
                                268                 :                :                          BackendXidGetPid(xmax));
                                269                 :                :             }
                                270                 :                : 
 2819 andres@anarazel.de        271                 :             22 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
                                272                 :                : 
                                273                 :                :             /* build a tuple */
 7524 ishii@postgresql.org      274                 :             22 :             tuple = BuildTupleFromCStrings(attinmeta, values);
 1726 michael@paquier.xyz       275                 :             22 :             tuplestore_puttuple(rsinfo->setResult, tuple);
                                276                 :                :         }
                                277                 :                :         else
                                278                 :                :         {
 2819 andres@anarazel.de        279                 :              2 :             LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK);
                                280                 :                :         }
                                281                 :                :     }
                                282                 :                : 
                                283                 :             12 :     table_endscan(scan);
 2448 tgl@sss.pgh.pa.us         284                 :             12 :     table_close(rel, AccessShareLock);
                                285                 :             12 :     return (Datum) 0;
                                286                 :                : }
        

Generated by: LCOV version 2.0-1