PostgreSQL Source Code git master
Loading...
Searching...
No Matches
superuser.c
Go to the documentation of this file.
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 */
35static Oid last_roleid = InvalidOid; /* InvalidOid == cache not valid */
36static bool last_roleid_is_super = false;
37static bool roleid_callback_registered = false;
38
40 uint32 hashvalue);
41
42
43/*
44 * The Postgres user running this command has Postgres superuser privileges
45 */
46bool
48{
49 return superuser_arg(GetUserId());
50}
51
52
53/*
54 * The specified role has Postgres superuser privileges
55 */
56bool
58{
59 bool result;
61
62 /* Quick out for cache hit */
63 if (OidIsValid(last_roleid) && last_roleid == roleid)
65
66 /* Special escape path in case you deleted all your users. */
68 return true;
69
70 /* OK, look up the information in pg_authid */
73 {
76 }
77 else
78 {
79 /* Report "not superuser" for invalid roleids */
80 result = false;
81 }
82
83 /* If first time through, set up callback for cache flushes */
85 {
88 (Datum) 0);
90 }
91
92 /* Cache the result for next time */
93 last_roleid = roleid;
94 last_roleid_is_super = result;
95
96 return result;
97}
98
99/*
100 * RoleidCallback
101 * Syscache inval callback function
102 */
103static void
105{
106 /* Invalidate our local cache in case role's superuserness changed */
108}
uint32_t uint32
Definition c.h:558
#define OidIsValid(objectId)
Definition c.h:800
Datum arg
Definition elog.c:1322
bool IsUnderPostmaster
Definition globals.c:120
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
void CacheRegisterSyscacheCallback(SysCacheIdentifier cacheid, SyscacheCallbackFunction func, Datum arg)
Definition inval.c:1816
Oid GetUserId(void)
Definition miscinit.c:469
bool rolsuper
Definition pg_authid.h:37
END_CATALOG_STRUCT typedef FormData_pg_authid * Form_pg_authid
Definition pg_authid.h:60
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
static int fb(int x)
static Oid last_roleid
Definition superuser.c:35
static bool roleid_callback_registered
Definition superuser.c:37
static void RoleidCallback(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
Definition superuser.c:104
bool superuser_arg(Oid roleid)
Definition superuser.c:57
static bool last_roleid_is_super
Definition superuser.c:36
bool superuser(void)
Definition superuser.c:47
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220