|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * pg_variable.c |
| 4 | + * session variables |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * IDENTIFICATION |
| 10 | + * src/backend/catalog/pg_variable.c |
| 11 | + * |
| 12 | + *------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | + |
| 15 | +#include "postgres.h" |
| 16 | + |
| 17 | +#include "access/heapam.h" |
| 18 | +#include "catalog/dependency.h" |
| 19 | +#include "catalog/indexing.h" |
| 20 | +#include "catalog/namespace.h" |
| 21 | +#include "catalog/objectaccess.h" |
| 22 | +#include "catalog/pg_collation.h" |
| 23 | +#include "catalog/pg_namespace.h" |
| 24 | +#include "catalog/pg_variable.h" |
| 25 | +#include "utils/builtins.h" |
| 26 | +#include "utils/pg_lsn.h" |
| 27 | +#include "utils/syscache.h" |
| 28 | + |
| 29 | +/* |
| 30 | + * Creates entry in pg_variable table |
| 31 | + */ |
| 32 | +ObjectAddress |
| 33 | +create_variable(const char *varName, |
| 34 | + Oid varNamespace, |
| 35 | + Oid varType, |
| 36 | + int32 varTypmod, |
| 37 | + Oid varOwner, |
| 38 | + Oid varCollation, |
| 39 | + bool if_not_exists) |
| 40 | +{ |
| 41 | + NameData varname; |
| 42 | + bool nulls[Natts_pg_variable]; |
| 43 | + Datum values[Natts_pg_variable]; |
| 44 | + Relation rel; |
| 45 | + HeapTuple tup; |
| 46 | + TupleDesc tupdesc; |
| 47 | + ObjectAddress myself, |
| 48 | + referenced; |
| 49 | + ObjectAddresses *addrs; |
| 50 | + Oid varid; |
| 51 | + |
| 52 | + Assert(varName); |
| 53 | + Assert(OidIsValid(varNamespace)); |
| 54 | + Assert(OidIsValid(varType)); |
| 55 | + Assert(OidIsValid(varOwner)); |
| 56 | + |
| 57 | + rel = table_open(VariableRelationId, RowExclusiveLock); |
| 58 | + |
| 59 | + /* |
| 60 | + * Check for duplicates. Note that this does not really prevent |
| 61 | + * duplicates, it's here just to provide nicer error message in common |
| 62 | + * case. The real protection is the unique key on the catalog. |
| 63 | + */ |
| 64 | + if (SearchSysCacheExists2(VARIABLENAMENSP, |
| 65 | + PointerGetDatum(varName), |
| 66 | + ObjectIdGetDatum(varNamespace))) |
| 67 | + { |
| 68 | + if (if_not_exists) |
| 69 | + ereport(NOTICE, |
| 70 | + (errcode(ERRCODE_DUPLICATE_OBJECT), |
| 71 | + errmsg("session variable \"%s\" already exists, skipping", |
| 72 | + varName))); |
| 73 | + else |
| 74 | + ereport(ERROR, |
| 75 | + (errcode(ERRCODE_DUPLICATE_OBJECT), |
| 76 | + errmsg("session variable \"%s\" already exists", |
| 77 | + varName))); |
| 78 | + |
| 79 | + table_close(rel, RowExclusiveLock); |
| 80 | + |
| 81 | + return InvalidObjectAddress; |
| 82 | + } |
| 83 | + |
| 84 | + memset(values, 0, sizeof(values)); |
| 85 | + memset(nulls, false, sizeof(nulls)); |
| 86 | + |
| 87 | + namestrcpy(&varname, varName); |
| 88 | + |
| 89 | + varid = GetNewOidWithIndex(rel, VariableOidIndexId, Anum_pg_variable_oid); |
| 90 | + |
| 91 | + values[Anum_pg_variable_oid - 1] = ObjectIdGetDatum(varid); |
| 92 | + values[Anum_pg_variable_varcreate_lsn - 1] = LSNGetDatum(GetXLogInsertRecPtr()); |
| 93 | + values[Anum_pg_variable_varname - 1] = NameGetDatum(&varname); |
| 94 | + values[Anum_pg_variable_varnamespace - 1] = ObjectIdGetDatum(varNamespace); |
| 95 | + values[Anum_pg_variable_vartype - 1] = ObjectIdGetDatum(varType); |
| 96 | + values[Anum_pg_variable_vartypmod - 1] = Int32GetDatum(varTypmod); |
| 97 | + values[Anum_pg_variable_varowner - 1] = ObjectIdGetDatum(varOwner); |
| 98 | + values[Anum_pg_variable_varcollation - 1] = ObjectIdGetDatum(varCollation); |
| 99 | + |
| 100 | + nulls[Anum_pg_variable_varacl - 1] = true; |
| 101 | + |
| 102 | + tupdesc = RelationGetDescr(rel); |
| 103 | + |
| 104 | + tup = heap_form_tuple(tupdesc, values, nulls); |
| 105 | + CatalogTupleInsert(rel, tup); |
| 106 | + Assert(OidIsValid(varid)); |
| 107 | + |
| 108 | + addrs = new_object_addresses(); |
| 109 | + |
| 110 | + ObjectAddressSet(myself, VariableRelationId, varid); |
| 111 | + |
| 112 | + /* dependency on namespace */ |
| 113 | + ObjectAddressSet(referenced, NamespaceRelationId, varNamespace); |
| 114 | + add_exact_object_address(&referenced, addrs); |
| 115 | + |
| 116 | + /* dependency on used type */ |
| 117 | + ObjectAddressSet(referenced, TypeRelationId, varType); |
| 118 | + add_exact_object_address(&referenced, addrs); |
| 119 | + |
| 120 | + /* dependency on collation */ |
| 121 | + if (OidIsValid(varCollation) && |
| 122 | + varCollation != DEFAULT_COLLATION_OID) |
| 123 | + { |
| 124 | + ObjectAddressSet(referenced, CollationRelationId, varCollation); |
| 125 | + add_exact_object_address(&referenced, addrs); |
| 126 | + } |
| 127 | + |
| 128 | + record_object_address_dependencies(&myself, addrs, DEPENDENCY_NORMAL); |
| 129 | + free_object_addresses(addrs); |
| 130 | + |
| 131 | + /* dependency on owner */ |
| 132 | + recordDependencyOnOwner(VariableRelationId, varid, varOwner); |
| 133 | + |
| 134 | + /* dependency on extension */ |
| 135 | + recordDependencyOnCurrentExtension(&myself, false); |
| 136 | + |
| 137 | + heap_freetuple(tup); |
| 138 | + |
| 139 | + /* post creation hook for new function */ |
| 140 | + InvokeObjectPostCreateHook(VariableRelationId, varid, 0); |
| 141 | + |
| 142 | + table_close(rel, RowExclusiveLock); |
| 143 | + |
| 144 | + return myself; |
| 145 | +} |
| 146 | + |
| 147 | +/* |
| 148 | + * Drop variable by OID |
| 149 | + */ |
| 150 | +void |
| 151 | +DropVariableById(Oid varid) |
| 152 | +{ |
| 153 | + Relation rel; |
| 154 | + HeapTuple tup; |
| 155 | + |
| 156 | + rel = table_open(VariableRelationId, RowExclusiveLock); |
| 157 | + |
| 158 | + tup = SearchSysCache1(VARIABLEOID, ObjectIdGetDatum(varid)); |
| 159 | + |
| 160 | + if (!HeapTupleIsValid(tup)) |
| 161 | + elog(ERROR, "cache lookup failed for variable %u", varid); |
| 162 | + |
| 163 | + CatalogTupleDelete(rel, &tup->t_self); |
| 164 | + |
| 165 | + ReleaseSysCache(tup); |
| 166 | + |
| 167 | + table_close(rel, RowExclusiveLock); |
| 168 | +} |
0 commit comments