LCOV - differential code coverage report
Current view: top level - src/backend/utils/misc - superuser.c (source / functions) Coverage Total Hit LBC GNC CBC DCB
Current: 380a8b2ea024c33a35e7abc8628e7c4f52f9f9f9 vs db5ed03217b9c238703df8b4b286115d6e940488 Lines: 100.0 % 21 21 1 20 1
Current Date: 2026-05-29 21:51:00 -0400 Functions: 100.0 % 3 3 1 2 1
Baseline: lcov-20260530-034037-baseline Branches: 91.7 % 12 11 1 11
Baseline Date: 2026-05-29 14:39:03 -0700 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 20 20 20
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 2 2 2
Branch coverage date bins:
(360..) days: 91.7 % 12 11 1 11

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * superuser.c
                                  4                 :                :  *    The superuser() function.  Determines if user has superuser privilege.
                                  5                 :                :  *
                                  6                 :                :  * All code should use either of these two functions to find out
                                  7                 :                :  * whether a given user is a superuser, rather than examining
                                  8                 :                :  * pg_authid.rolsuper directly, so that the escape hatch built in for
                                  9                 :                :  * the single-user case works.
                                 10                 :                :  *
                                 11                 :                :  *
                                 12                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 13                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 14                 :                :  *
                                 15                 :                :  *
                                 16                 :                :  * IDENTIFICATION
                                 17                 :                :  *    src/backend/utils/misc/superuser.c
                                 18                 :                :  *
                                 19                 :                :  *-------------------------------------------------------------------------
                                 20                 :                :  */
                                 21                 :                : #include "postgres.h"
                                 22                 :                : 
                                 23                 :                : #include "access/htup_details.h"
                                 24                 :                : #include "catalog/pg_authid.h"
                                 25                 :                : #include "miscadmin.h"
                                 26                 :                : #include "utils/inval.h"
                                 27                 :                : #include "utils/syscache.h"
                                 28                 :                : 
                                 29                 :                : /*
                                 30                 :                :  * In common cases the same roleid (ie, the session or current ID) will
                                 31                 :                :  * be queried repeatedly.  So we maintain a simple one-entry cache for
                                 32                 :                :  * the status of the last requested roleid.  The cache can be flushed
                                 33                 :                :  * at need by watching for cache update events on pg_authid.
                                 34                 :                :  */
                                 35                 :                : static Oid  last_roleid = InvalidOid;   /* InvalidOid == cache not valid */
                                 36                 :                : static bool last_roleid_is_super = false;
                                 37                 :                : static bool roleid_callback_registered = false;
                                 38                 :                : 
                                 39                 :                : static void RoleidCallback(Datum arg, SysCacheIdentifier cacheid,
                                 40                 :                :                            uint32 hashvalue);
                                 41                 :                : 
                                 42                 :                : 
                                 43                 :                : /*
                                 44                 :                :  * The Postgres user running this command has Postgres superuser privileges
                                 45                 :                :  */
                                 46                 :                : bool
10492 bruce@momjian.us           47                 :CBC       76102 : superuser(void)
                                 48                 :                : {
 8867 peter_e@gmx.net            49                 :          76102 :     return superuser_arg(GetUserId());
                                 50                 :                : }
                                 51                 :                : 
                                 52                 :                : 
                                 53                 :                : /*
                                 54                 :                :  * The specified role has Postgres superuser privileges
                                 55                 :                :  */
                                 56                 :                : bool
 7641 tgl@sss.pgh.pa.us          57                 :        5050429 : superuser_arg(Oid roleid)
                                 58                 :                : {
                                 59                 :                :     bool        result;
                                 60                 :                :     HeapTuple   rtup;
                                 61                 :                : 
                                 62                 :                :     /* Quick out for cache hit */
                                 63   [ +  +  +  + ]:        5050429 :     if (OidIsValid(last_roleid) && last_roleid == roleid)
                                 64                 :        4789310 :         return last_roleid_is_super;
                                 65                 :                : 
                                 66                 :                :     /* Special escape path in case you deleted all your users. */
                                 67   [ +  +  +  - ]:         261119 :     if (!IsUnderPostmaster && roleid == BOOTSTRAP_SUPERUSERID)
 9030 peter_e@gmx.net            68                 :         218937 :         return true;
                                 69                 :                : 
                                 70                 :                :     /* OK, look up the information in pg_authid */
 5949 rhaas@postgresql.org       71                 :          42182 :     rtup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
 7641 tgl@sss.pgh.pa.us          72         [ +  + ]:          42182 :     if (HeapTupleIsValid(rtup))
                                 73                 :                :     {
 4176 alvherre@alvh.no-ip.       74                 :          42174 :         result = ((Form_pg_authid) GETSTRUCT(rtup))->rolsuper;
 7641 tgl@sss.pgh.pa.us          75                 :          42174 :         ReleaseSysCache(rtup);
                                 76                 :                :     }
                                 77                 :                :     else
                                 78                 :                :     {
                                 79                 :                :         /* Report "not superuser" for invalid roleids */
 7671                            80                 :              8 :         result = false;
                                 81                 :                :     }
                                 82                 :                : 
                                 83                 :                :     /* If first time through, set up callback for cache flushes */
 7641                            84         [ +  + ]:          42182 :     if (!roleid_callback_registered)
                                 85                 :                :     {
                                 86                 :          17411 :         CacheRegisterSyscacheCallback(AUTHOID,
                                 87                 :                :                                       RoleidCallback,
                                 88                 :                :                                       (Datum) 0);
                                 89                 :          17411 :         roleid_callback_registered = true;
                                 90                 :                :     }
                                 91                 :                : 
                                 92                 :                :     /* Cache the result for next time */
                                 93                 :          42182 :     last_roleid = roleid;
                                 94                 :          42182 :     last_roleid_is_super = result;
                                 95                 :                : 
 9117                            96                 :          42182 :     return result;
                                 97                 :                : }
                                 98                 :                : 
                                 99                 :                : /*
                                100                 :                :  * RoleidCallback
                                101                 :                :  *      Syscache inval callback function
                                102                 :                :  */
                                103                 :                : static void
  101 michael@paquier.xyz       104                 :GNC       23851 : RoleidCallback(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
                                105                 :                : {
                                106                 :                :     /* Invalidate our local cache in case role's superuserness changed */
 7641 tgl@sss.pgh.pa.us         107                 :CBC       23851 :     last_roleid = InvalidOid;
 7671                           108                 :          23851 : }
        

Generated by: LCOV version 2.5.0-beta