LCOV - code coverage report
Current view: top level - src/backend/catalog - pg_db_role_setting.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 78.6 % 131 103
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 3 3
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 56.7 % 30 17

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * pg_db_role_setting.c
       3                 :             :  *              Routines to support manipulation of the pg_db_role_setting relation
       4                 :             :  *
       5                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       6                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       7                 :             :  *
       8                 :             :  * IDENTIFICATION
       9                 :             :  *              src/backend/catalog/pg_db_role_setting.c
      10                 :             :  */
      11                 :             : #include "postgres.h"
      12                 :             : 
      13                 :             : #include "access/genam.h"
      14                 :             : #include "access/heapam.h"
      15                 :             : #include "access/htup_details.h"
      16                 :             : #include "access/tableam.h"
      17                 :             : #include "catalog/indexing.h"
      18                 :             : #include "catalog/objectaccess.h"
      19                 :             : #include "catalog/pg_db_role_setting.h"
      20                 :             : #include "utils/fmgroids.h"
      21                 :             : #include "utils/rel.h"
      22                 :             : 
      23                 :             : void
      24                 :          13 : AlterSetting(Oid databaseid, Oid roleid, VariableSetStmt *setstmt)
      25                 :             : {
      26                 :          13 :         char       *valuestr;
      27                 :          13 :         HeapTuple       tuple;
      28                 :          13 :         Relation        rel;
      29                 :          13 :         ScanKeyData scankey[2];
      30                 :          13 :         SysScanDesc scan;
      31                 :             : 
      32                 :          13 :         valuestr = ExtractSetVariableArgs(setstmt);
      33                 :             : 
      34                 :             :         /* Get the old tuple, if any. */
      35                 :             : 
      36                 :          13 :         rel = table_open(DbRoleSettingRelationId, RowExclusiveLock);
      37                 :          26 :         ScanKeyInit(&scankey[0],
      38                 :             :                                 Anum_pg_db_role_setting_setdatabase,
      39                 :             :                                 BTEqualStrategyNumber, F_OIDEQ,
      40                 :          13 :                                 ObjectIdGetDatum(databaseid));
      41                 :          26 :         ScanKeyInit(&scankey[1],
      42                 :             :                                 Anum_pg_db_role_setting_setrole,
      43                 :             :                                 BTEqualStrategyNumber, F_OIDEQ,
      44                 :          13 :                                 ObjectIdGetDatum(roleid));
      45                 :          26 :         scan = systable_beginscan(rel, DbRoleSettingDatidRolidIndexId, true,
      46                 :          13 :                                                           NULL, 2, scankey);
      47                 :          13 :         tuple = systable_getnext(scan);
      48                 :             : 
      49                 :             :         /*
      50                 :             :          * There are three cases:
      51                 :             :          *
      52                 :             :          * - in RESET ALL, request GUC to reset the settings array and update the
      53                 :             :          * catalog if there's anything left, delete it otherwise
      54                 :             :          *
      55                 :             :          * - in other commands, if there's a tuple in pg_db_role_setting, update
      56                 :             :          * it; if it ends up empty, delete it
      57                 :             :          *
      58                 :             :          * - otherwise, insert a new pg_db_role_setting tuple, but only if the
      59                 :             :          * command is not RESET
      60                 :             :          */
      61         [ -  + ]:          13 :         if (setstmt->kind == VAR_RESET_ALL)
      62                 :             :         {
      63         [ #  # ]:           0 :                 if (HeapTupleIsValid(tuple))
      64                 :             :                 {
      65                 :           0 :                         ArrayType  *new = NULL;
      66                 :           0 :                         Datum           datum;
      67                 :           0 :                         bool            isnull;
      68                 :             : 
      69                 :           0 :                         datum = heap_getattr(tuple, Anum_pg_db_role_setting_setconfig,
      70                 :           0 :                                                                  RelationGetDescr(rel), &isnull);
      71                 :             : 
      72         [ #  # ]:           0 :                         if (!isnull)
      73                 :           0 :                                 new = GUCArrayReset(DatumGetArrayTypeP(datum));
      74                 :             : 
      75         [ #  # ]:           0 :                         if (new)
      76                 :             :                         {
      77                 :           0 :                                 Datum           repl_val[Natts_pg_db_role_setting];
      78                 :           0 :                                 bool            repl_null[Natts_pg_db_role_setting];
      79                 :           0 :                                 bool            repl_repl[Natts_pg_db_role_setting];
      80                 :           0 :                                 HeapTuple       newtuple;
      81                 :             : 
      82                 :           0 :                                 memset(repl_repl, false, sizeof(repl_repl));
      83                 :             : 
      84                 :           0 :                                 repl_val[Anum_pg_db_role_setting_setconfig - 1] =
      85                 :           0 :                                         PointerGetDatum(new);
      86                 :           0 :                                 repl_repl[Anum_pg_db_role_setting_setconfig - 1] = true;
      87                 :           0 :                                 repl_null[Anum_pg_db_role_setting_setconfig - 1] = false;
      88                 :             : 
      89                 :           0 :                                 newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel),
      90                 :           0 :                                                                                          repl_val, repl_null, repl_repl);
      91                 :           0 :                                 CatalogTupleUpdate(rel, &tuple->t_self, newtuple);
      92                 :           0 :                         }
      93                 :             :                         else
      94                 :           0 :                                 CatalogTupleDelete(rel, &tuple->t_self);
      95                 :           0 :                 }
      96                 :           0 :         }
      97         [ +  + ]:          13 :         else if (HeapTupleIsValid(tuple))
      98                 :             :         {
      99                 :          10 :                 Datum           repl_val[Natts_pg_db_role_setting];
     100                 :          10 :                 bool            repl_null[Natts_pg_db_role_setting];
     101                 :          10 :                 bool            repl_repl[Natts_pg_db_role_setting];
     102                 :          10 :                 HeapTuple       newtuple;
     103                 :          10 :                 Datum           datum;
     104                 :          10 :                 bool            isnull;
     105                 :          10 :                 ArrayType  *a;
     106                 :             : 
     107                 :          10 :                 memset(repl_repl, false, sizeof(repl_repl));
     108                 :          10 :                 repl_repl[Anum_pg_db_role_setting_setconfig - 1] = true;
     109                 :          10 :                 repl_null[Anum_pg_db_role_setting_setconfig - 1] = false;
     110                 :             : 
     111                 :             :                 /* Extract old value of setconfig */
     112                 :          20 :                 datum = heap_getattr(tuple, Anum_pg_db_role_setting_setconfig,
     113                 :          10 :                                                          RelationGetDescr(rel), &isnull);
     114         [ -  + ]:          10 :                 a = isnull ? NULL : DatumGetArrayTypeP(datum);
     115                 :             : 
     116                 :             :                 /* Update (valuestr is NULL in RESET cases) */
     117         [ +  - ]:          10 :                 if (valuestr)
     118                 :          10 :                         a = GUCArrayAdd(a, setstmt->name, valuestr);
     119                 :             :                 else
     120                 :           0 :                         a = GUCArrayDelete(a, setstmt->name);
     121                 :             : 
     122         [ +  - ]:          10 :                 if (a)
     123                 :             :                 {
     124                 :          10 :                         repl_val[Anum_pg_db_role_setting_setconfig - 1] =
     125                 :          10 :                                 PointerGetDatum(a);
     126                 :             : 
     127                 :          20 :                         newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel),
     128                 :          10 :                                                                                  repl_val, repl_null, repl_repl);
     129                 :          10 :                         CatalogTupleUpdate(rel, &tuple->t_self, newtuple);
     130                 :          10 :                 }
     131                 :             :                 else
     132                 :           0 :                         CatalogTupleDelete(rel, &tuple->t_self);
     133                 :          10 :         }
     134         [ +  - ]:           3 :         else if (valuestr)
     135                 :             :         {
     136                 :             :                 /* non-null valuestr means it's not RESET, so insert a new tuple */
     137                 :           3 :                 HeapTuple       newtuple;
     138                 :           3 :                 Datum           values[Natts_pg_db_role_setting];
     139                 :           3 :                 bool            nulls[Natts_pg_db_role_setting];
     140                 :           3 :                 ArrayType  *a;
     141                 :             : 
     142                 :           3 :                 memset(nulls, false, sizeof(nulls));
     143                 :             : 
     144                 :           3 :                 a = GUCArrayAdd(NULL, setstmt->name, valuestr);
     145                 :             : 
     146                 :           3 :                 values[Anum_pg_db_role_setting_setdatabase - 1] =
     147                 :           3 :                         ObjectIdGetDatum(databaseid);
     148                 :           3 :                 values[Anum_pg_db_role_setting_setrole - 1] = ObjectIdGetDatum(roleid);
     149                 :           3 :                 values[Anum_pg_db_role_setting_setconfig - 1] = PointerGetDatum(a);
     150                 :           3 :                 newtuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
     151                 :             : 
     152                 :           3 :                 CatalogTupleInsert(rel, newtuple);
     153                 :           3 :         }
     154                 :             :         else
     155                 :             :         {
     156                 :             :                 /*
     157                 :             :                  * RESET doesn't need to change any state if there's no pre-existing
     158                 :             :                  * pg_db_role_setting entry, but for consistency we should still check
     159                 :             :                  * that the option is valid and we're allowed to set it.
     160                 :             :                  */
     161                 :           0 :                 (void) GUCArrayDelete(NULL, setstmt->name);
     162                 :             :         }
     163                 :             : 
     164         [ +  - ]:          13 :         InvokeObjectPostAlterHookArg(DbRoleSettingRelationId,
     165                 :             :                                                                  databaseid, 0, roleid, false);
     166                 :             : 
     167                 :          13 :         systable_endscan(scan);
     168                 :             : 
     169                 :             :         /* Close pg_db_role_setting, but keep lock till commit */
     170                 :          13 :         table_close(rel, NoLock);
     171                 :          13 : }
     172                 :             : 
     173                 :             : /*
     174                 :             :  * Drop some settings from the catalog.  These can be for a particular
     175                 :             :  * database, or for a particular role.  (It is of course possible to do both
     176                 :             :  * too, but it doesn't make sense for current uses.)
     177                 :             :  */
     178                 :             : void
     179                 :         207 : DropSetting(Oid databaseid, Oid roleid)
     180                 :             : {
     181                 :         207 :         Relation        relsetting;
     182                 :         207 :         TableScanDesc scan;
     183                 :         207 :         ScanKeyData keys[2];
     184                 :         207 :         HeapTuple       tup;
     185                 :         207 :         int                     numkeys = 0;
     186                 :             : 
     187                 :         207 :         relsetting = table_open(DbRoleSettingRelationId, RowExclusiveLock);
     188                 :             : 
     189         [ +  + ]:         207 :         if (OidIsValid(databaseid))
     190                 :             :         {
     191                 :           2 :                 ScanKeyInit(&keys[numkeys],
     192                 :             :                                         Anum_pg_db_role_setting_setdatabase,
     193                 :             :                                         BTEqualStrategyNumber,
     194                 :             :                                         F_OIDEQ,
     195                 :           1 :                                         ObjectIdGetDatum(databaseid));
     196                 :           1 :                 numkeys++;
     197                 :           1 :         }
     198         [ +  + ]:         207 :         if (OidIsValid(roleid))
     199                 :             :         {
     200                 :         412 :                 ScanKeyInit(&keys[numkeys],
     201                 :             :                                         Anum_pg_db_role_setting_setrole,
     202                 :             :                                         BTEqualStrategyNumber,
     203                 :             :                                         F_OIDEQ,
     204                 :         206 :                                         ObjectIdGetDatum(roleid));
     205                 :         206 :                 numkeys++;
     206                 :         206 :         }
     207                 :             : 
     208                 :         207 :         scan = table_beginscan_catalog(relsetting, numkeys, keys);
     209         [ +  + ]:         208 :         while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
     210                 :             :         {
     211                 :           1 :                 CatalogTupleDelete(relsetting, &tup->t_self);
     212                 :             :         }
     213                 :         207 :         table_endscan(scan);
     214                 :             : 
     215                 :         207 :         table_close(relsetting, RowExclusiveLock);
     216                 :         207 : }
     217                 :             : 
     218                 :             : /*
     219                 :             :  * Scan pg_db_role_setting looking for applicable settings, and load them on
     220                 :             :  * the current process.
     221                 :             :  *
     222                 :             :  * relsetting is pg_db_role_setting, already opened and locked.
     223                 :             :  *
     224                 :             :  * Note: we only consider setting for the exact databaseid/roleid combination.
     225                 :             :  * This probably needs to be called more than once, with InvalidOid passed as
     226                 :             :  * databaseid/roleid.
     227                 :             :  */
     228                 :             : void
     229                 :        3180 : ApplySetting(Snapshot snapshot, Oid databaseid, Oid roleid,
     230                 :             :                          Relation relsetting, GucSource source)
     231                 :             : {
     232                 :        3180 :         SysScanDesc scan;
     233                 :        3180 :         ScanKeyData keys[2];
     234                 :        3180 :         HeapTuple       tup;
     235                 :             : 
     236                 :        6360 :         ScanKeyInit(&keys[0],
     237                 :             :                                 Anum_pg_db_role_setting_setdatabase,
     238                 :             :                                 BTEqualStrategyNumber,
     239                 :             :                                 F_OIDEQ,
     240                 :        3180 :                                 ObjectIdGetDatum(databaseid));
     241                 :        6360 :         ScanKeyInit(&keys[1],
     242                 :             :                                 Anum_pg_db_role_setting_setrole,
     243                 :             :                                 BTEqualStrategyNumber,
     244                 :             :                                 F_OIDEQ,
     245                 :        3180 :                                 ObjectIdGetDatum(roleid));
     246                 :             : 
     247                 :        6360 :         scan = systable_beginscan(relsetting, DbRoleSettingDatidRolidIndexId, true,
     248                 :        3180 :                                                           snapshot, 2, keys);
     249         [ +  + ]:        3947 :         while (HeapTupleIsValid(tup = systable_getnext(scan)))
     250                 :             :         {
     251                 :         767 :                 bool            isnull;
     252                 :         767 :                 Datum           datum;
     253                 :             : 
     254                 :        1534 :                 datum = heap_getattr(tup, Anum_pg_db_role_setting_setconfig,
     255                 :         767 :                                                          RelationGetDescr(relsetting), &isnull);
     256         [ -  + ]:         767 :                 if (!isnull)
     257                 :             :                 {
     258                 :         767 :                         ArrayType  *a = DatumGetArrayTypeP(datum);
     259                 :             : 
     260                 :             :                         /*
     261                 :             :                          * We process all the options at SUSET level.  We assume that the
     262                 :             :                          * right to insert an option into pg_db_role_setting was checked
     263                 :             :                          * when it was inserted.
     264                 :             :                          */
     265                 :         767 :                         ProcessGUCArray(a, PGC_SUSET, source, GUC_ACTION_SET);
     266                 :         767 :                 }
     267                 :         767 :         }
     268                 :             : 
     269                 :        3180 :         systable_endscan(scan);
     270                 :        3180 : }
        

Generated by: LCOV version 2.3.2-1