Skip to content

Commit 5a02377

Browse files
okbob@github.comCommitfest Bot
authored andcommitted
introduce new class (catalog) pg_variable
This table holds metadata about session variables created by command CREATE VARIABLE, and dropped by command DROP VARIABLE.
1 parent 1213cb4 commit 5a02377

File tree

9 files changed

+408
-1
lines changed

9 files changed

+408
-1
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,11 @@
369369
<entry><link linkend="catalog-pg-user-mapping"><structname>pg_user_mapping</structname></link></entry>
370370
<entry>mappings of users to foreign servers</entry>
371371
</row>
372+
373+
<row>
374+
<entry><link linkend="catalog-pg-variable"><structname>pg_variable</structname></link></entry>
375+
<entry>session variables</entry>
376+
</row>
372377
</tbody>
373378
</tgroup>
374379
</table>
@@ -9834,4 +9839,132 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
98349839
</table>
98359840
</sect1>
98369841

9842+
<sect1 id="catalog-pg-variable">
9843+
<title><structname>pg_variable</structname></title>
9844+
9845+
<indexterm zone="catalog-pg-variable">
9846+
<primary>pg_variable</primary>
9847+
</indexterm>
9848+
9849+
<para>
9850+
The catalog <structname>pg_variable</structname> stores information about
9851+
session variables.
9852+
</para>
9853+
9854+
<table>
9855+
<title><structname>pg_variable</structname> Columns</title>
9856+
<tgroup cols="1">
9857+
<thead>
9858+
<row>
9859+
<entry role="catalog_table_entry"><para role="column_definition">
9860+
Column Type
9861+
</para>
9862+
<para>
9863+
Description
9864+
</para></entry>
9865+
</row>
9866+
</thead>
9867+
9868+
<tbody>
9869+
<row>
9870+
<entry role="catalog_table_entry"><para role="column_definition">
9871+
<structfield>oid</structfield> <type>oid</type>
9872+
</para>
9873+
<para>
9874+
Row identifier
9875+
</para></entry>
9876+
</row>
9877+
9878+
<row>
9879+
<entry role="catalog_table_entry"><para role="column_definition">
9880+
<structfield>vartype</structfield> <type>oid</type>
9881+
(references <link linkend="catalog-pg-type"><structname>pg_type</structname></link>.<structfield>oid</structfield>)
9882+
</para>
9883+
<para>
9884+
The OID of the variable's data type
9885+
</para></entry>
9886+
</row>
9887+
9888+
<row>
9889+
<entry role="catalog_table_entry"><para role="column_definition">
9890+
<structfield>varcreate_lsn</structfield> <type>pg_lsn</type>
9891+
</para>
9892+
<para>
9893+
LSN of the transaction where the variable was created.
9894+
<structfield>varcreate_lsn</structfield> and
9895+
<structfield>oid</structfield> together form the all-time unique
9896+
identifier (<structfield>oid</structfield> alone is not enough, since
9897+
object identifiers can get reused).
9898+
</para></entry>
9899+
</row>
9900+
9901+
<row>
9902+
<entry role="catalog_table_entry"><para role="column_definition">
9903+
<structfield>varname</structfield> <type>name</type>
9904+
</para>
9905+
<para>
9906+
Name of the session variable
9907+
</para></entry>
9908+
</row>
9909+
9910+
<row>
9911+
<entry role="catalog_table_entry"><para role="column_definition">
9912+
<structfield>varnamespace</structfield> <type>oid</type>
9913+
(references <link linkend="catalog-pg-namespace"><structname>pg_namespace</structname></link>.<structfield>oid</structfield>)
9914+
</para>
9915+
<para>
9916+
The OID of the namespace that contains this variable
9917+
</para></entry>
9918+
</row>
9919+
9920+
<row>
9921+
<entry role="catalog_table_entry"><para role="column_definition">
9922+
<structfield>varowner</structfield> <type>oid</type>
9923+
(references <link linkend="catalog-pg-authid"><structname>pg_authid</structname></link>.<structfield>oid</structfield>)
9924+
</para>
9925+
<para>
9926+
Owner of the variable
9927+
</para></entry>
9928+
</row>
9929+
9930+
<row>
9931+
<entry role="catalog_table_entry"><para role="column_definition">
9932+
<structfield>vartypmod</structfield> <type>int4</type>
9933+
</para>
9934+
<para>
9935+
<structfield>vartypmod</structfield> records type-specific data
9936+
supplied at variable creation time (for example, the maximum
9937+
length of a <type>varchar</type> column). It is passed to
9938+
type-specific input functions and length coercion functions.
9939+
The value will generally be -1 for types that do not need <structfield>vartypmod</structfield>.
9940+
</para></entry>
9941+
</row>
9942+
9943+
<row>
9944+
<entry role="catalog_table_entry"><para role="column_definition">
9945+
<structfield>varcollation</structfield> <type>oid</type>
9946+
(references <link linkend="catalog-pg-collation"><structname>pg_collation</structname></link>.<structfield>oid</structfield>)
9947+
</para>
9948+
<para>
9949+
The defined collation of the variable, or zero if the variable is
9950+
not of a collatable data type.
9951+
</para></entry>
9952+
</row>
9953+
9954+
<row>
9955+
<entry role="catalog_table_entry"><para role="column_definition">
9956+
<structfield>varacl</structfield> <type>aclitem[]</type>
9957+
</para>
9958+
<para>
9959+
Access privileges; see
9960+
<xref linkend="sql-grant"/> and
9961+
<xref linkend="sql-revoke"/>
9962+
for details
9963+
</para></entry>
9964+
</row>
9965+
9966+
</tbody>
9967+
</tgroup>
9968+
</table>
9969+
</sect1>
98379970
</chapter>

src/backend/catalog/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ OBJS = \
4545
pg_shdepend.o \
4646
pg_subscription.o \
4747
pg_type.o \
48+
pg_variable.o \
4849
storage.o \
4950
toasting.o
5051

src/backend/catalog/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ backend_sources += files(
3232
'pg_shdepend.c',
3333
'pg_subscription.c',
3434
'pg_type.c',
35+
'pg_variable.c',
3536
'storage.c',
3637
'toasting.c',
3738
)

src/backend/catalog/pg_variable.c

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
}

src/include/catalog/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ CATALOG_HEADERS := \
8181
pg_publication_namespace.h \
8282
pg_publication_rel.h \
8383
pg_subscription.h \
84-
pg_subscription_rel.h
84+
pg_subscription_rel.h \
85+
pg_variable.h
8586

8687
GENERATED_HEADERS := $(CATALOG_HEADERS:%.h=%_d.h)
8788

src/include/catalog/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ catalog_headers = [
6969
'pg_publication_rel.h',
7070
'pg_subscription.h',
7171
'pg_subscription_rel.h',
72+
'pg_variable.h',
7273
]
7374

7475
# The .dat files we need can just be listed alphabetically.

0 commit comments

Comments
 (0)