PostgreSQL Source Code  git master
pg_parameter_acl.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_parameter_acl.c
4  * routines to support manipulation of the pg_parameter_acl relation
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/catalog/pg_parameter_acl.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include "access/table.h"
18 #include "catalog/catalog.h"
19 #include "catalog/indexing.h"
20 #include "catalog/objectaccess.h"
21 #include "catalog/pg_namespace.h"
23 #include "utils/builtins.h"
24 #include "utils/pg_locale.h"
25 #include "utils/rel.h"
26 #include "utils/syscache.h"
27 
28 
29 /*
30  * ParameterAclLookup - Given a configuration parameter name,
31  * look up the associated configuration parameter ACL's OID.
32  *
33  * If missing_ok is false, throw an error if ACL entry not found. If
34  * true, just return InvalidOid.
35  */
36 Oid
37 ParameterAclLookup(const char *parameter, bool missing_ok)
38 {
39  Oid oid;
40  char *parname;
41 
42  /* Convert name to the form it should have in pg_parameter_acl... */
43  parname = convert_GUC_name_for_parameter_acl(parameter);
44 
45  /* ... and look it up */
46  oid = GetSysCacheOid1(PARAMETERACLNAME, Anum_pg_parameter_acl_oid,
48 
49  if (!OidIsValid(oid) && !missing_ok)
50  ereport(ERROR,
51  (errcode(ERRCODE_UNDEFINED_OBJECT),
52  errmsg("parameter ACL \"%s\" does not exist", parameter)));
53 
54  pfree(parname);
55 
56  return oid;
57 }
58 
59 /*
60  * ParameterAclCreate
61  *
62  * Add a new tuple to pg_parameter_acl.
63  *
64  * parameter: the parameter name to create an entry for.
65  * Caller should have verified that there's no such entry already.
66  *
67  * Returns the new entry's OID.
68  */
69 Oid
70 ParameterAclCreate(const char *parameter)
71 {
72  Oid parameterId;
73  char *parname;
74  Relation rel;
75  TupleDesc tupDesc;
76  HeapTuple tuple;
77  Datum values[Natts_pg_parameter_acl];
78  bool nulls[Natts_pg_parameter_acl];
79 
80  /*
81  * To prevent cluttering pg_parameter_acl with useless entries, insist
82  * that the name be valid.
83  */
84  if (!check_GUC_name_for_parameter_acl(parameter))
85  ereport(ERROR,
86  (errcode(ERRCODE_INVALID_NAME),
87  errmsg("invalid parameter name \"%s\"",
88  parameter)));
89 
90  /* Convert name to the form it should have in pg_parameter_acl. */
91  parname = convert_GUC_name_for_parameter_acl(parameter);
92 
93  /*
94  * Create and insert a new record containing a null ACL.
95  *
96  * We don't take a strong enough lock to prevent concurrent insertions,
97  * relying instead on the unique index.
98  */
99  rel = table_open(ParameterAclRelationId, RowExclusiveLock);
100  tupDesc = RelationGetDescr(rel);
101  MemSet(values, 0, sizeof(values));
102  MemSet(nulls, false, sizeof(nulls));
103  parameterId = GetNewOidWithIndex(rel,
104  ParameterAclOidIndexId,
105  Anum_pg_parameter_acl_oid);
106  values[Anum_pg_parameter_acl_oid - 1] = ObjectIdGetDatum(parameterId);
107  values[Anum_pg_parameter_acl_parname - 1] =
109  nulls[Anum_pg_parameter_acl_paracl - 1] = true;
110  tuple = heap_form_tuple(tupDesc, values, nulls);
111  CatalogTupleInsert(rel, tuple);
112 
113  /* Close pg_parameter_acl, but keep lock till commit. */
114  heap_freetuple(tuple);
115  table_close(rel, NoLock);
116 
117  return parameterId;
118 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define MemSet(start, val, len)
Definition: c.h:1008
#define OidIsValid(objectId)
Definition: c.h:710
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:391
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
bool check_GUC_name_for_parameter_acl(const char *name)
Definition: guc.c:5817
char * convert_GUC_name_for_parameter_acl(const char *name)
Definition: guc.c:5781
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:221
#define NoLock
Definition: lockdefs.h:34
#define RowExclusiveLock
Definition: lockdefs.h:38
void pfree(void *pointer)
Definition: mcxt.c:1175
Oid ParameterAclLookup(const char *parameter, bool missing_ok)
Oid ParameterAclCreate(const char *parameter)
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define PointerGetDatum(X)
Definition: postgres.h:600
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:514
@ PARAMETERACLNAME
Definition: syscache.h:75
#define GetSysCacheOid1(cacheId, oidcol, key1)
Definition: syscache.h:197
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
text * cstring_to_text(const char *s)
Definition: varlena.c:188