Skip to content

Commit a22f537

Browse files
okbob@github.comCommitfest Bot
authored andcommitted
CREATE, DROP, ALTER VARIABLE
Implementation of commands: CREATE VARIABLE varname AS type DROP VARIABLE varname ALTER VARIABLE varname OWNER TO ALTER VARIABLE varname RENAME TO ALTER VARIABLE varname SET SCHEMA ALTER command uses already prepared infrastructure based on ObjectAddress API, so this patch implements ObjectAddress related functionality too. Event triggers for DDL over session variables are supported.
1 parent 0fe23db commit a22f537

40 files changed

+1571
-8
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5362,6 +5362,27 @@ EXPLAIN SELECT count(*) FROM measurement WHERE logdate >= DATE '2008-01-01';
53625362
</para>
53635363
</sect1>
53645364

5365+
<sect1 id="ddl-session-variables">
5366+
<title>Session Variables</title>
5367+
5368+
<indexterm zone="ddl-session-variables">
5369+
<primary>Session variables</primary>
5370+
</indexterm>
5371+
5372+
<indexterm>
5373+
<primary>session variable</primary>
5374+
</indexterm>
5375+
5376+
<para>
5377+
Session variables are database objects that can hold a value.
5378+
</para>
5379+
5380+
<para>
5381+
The session variable holds value in session memory. This value is private
5382+
to each session and is released when the session ends.
5383+
</para>
5384+
</sect1>
5385+
53655386
<sect1 id="ddl-others">
53665387
<title>Other Database Objects</title>
53675388

doc/src/sgml/glossary.sgml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,21 @@
17081708
</glossdef>
17091709
</glossentry>
17101710

1711+
<glossentry id="glossary-session-variable">
1712+
<glossterm>Session variable</glossterm>
1713+
<glossdef>
1714+
<para>
1715+
A persistent database object that holds a value in session memory. This
1716+
value is private to each session and is released when the session ends.
1717+
Read or write access to session variables is controlled by privileges,
1718+
similar to other database objects.
1719+
</para>
1720+
<para>
1721+
For more information, see <xref linkend="ddl-session-variables"/>.
1722+
</para>
1723+
</glossdef>
1724+
</glossentry>
1725+
17111726
<glossentry id="glossary-shared-memory">
17121727
<glossterm>Shared memory</glossterm>
17131728
<glossdef>

doc/src/sgml/plpgsql.sgml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6036,6 +6036,20 @@ $$ LANGUAGE plpgsql STRICT IMMUTABLE;
60366036
</programlisting>
60376037
</para>
60386038
</sect3>
6039+
6040+
<sect3 id="plpgsql-porting-package-variables">
6041+
<title><command>Packages and package variables</command></title>
6042+
6043+
<para>
6044+
The <application>PL/pgSQL</application> language has no packages, and
6045+
therefore no package variables or package constants.
6046+
You can consider translating an Oracle package into a schema in
6047+
<productname>PostgreSQL</productname>. Package functions and procedures
6048+
would then become functions and procedures in that schema, and package
6049+
variables could be translated to session variables in that schema.
6050+
(see <xref linkend="ddl-session-variables"/>).
6051+
</para>
6052+
</sect3>
60396053
</sect2>
60406054

60416055
<sect2 id="plpgsql-porting-appendix">

doc/src/sgml/ref/allfiles.sgml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Complete list of usable sgml source files in this directory.
4747
<!ENTITY alterType SYSTEM "alter_type.sgml">
4848
<!ENTITY alterUser SYSTEM "alter_user.sgml">
4949
<!ENTITY alterUserMapping SYSTEM "alter_user_mapping.sgml">
50+
<!ENTITY alterVariable SYSTEM "alter_variable.sgml">
5051
<!ENTITY alterView SYSTEM "alter_view.sgml">
5152
<!ENTITY analyze SYSTEM "analyze.sgml">
5253
<!ENTITY begin SYSTEM "begin.sgml">
@@ -99,6 +100,7 @@ Complete list of usable sgml source files in this directory.
99100
<!ENTITY createType SYSTEM "create_type.sgml">
100101
<!ENTITY createUser SYSTEM "create_user.sgml">
101102
<!ENTITY createUserMapping SYSTEM "create_user_mapping.sgml">
103+
<!ENTITY createVariable SYSTEM "create_variable.sgml">
102104
<!ENTITY createView SYSTEM "create_view.sgml">
103105
<!ENTITY deallocate SYSTEM "deallocate.sgml">
104106
<!ENTITY declare SYSTEM "declare.sgml">
@@ -147,6 +149,7 @@ Complete list of usable sgml source files in this directory.
147149
<!ENTITY dropType SYSTEM "drop_type.sgml">
148150
<!ENTITY dropUser SYSTEM "drop_user.sgml">
149151
<!ENTITY dropUserMapping SYSTEM "drop_user_mapping.sgml">
152+
<!ENTITY dropVariable SYSTEM "drop_variable.sgml">
150153
<!ENTITY dropView SYSTEM "drop_view.sgml">
151154
<!ENTITY end SYSTEM "end.sgml">
152155
<!ENTITY execute SYSTEM "execute.sgml">

doc/src/sgml/ref/alter_variable.sgml

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<!--
2+
doc/src/sgml/ref/alter_variable.sgml
3+
PostgreSQL documentation
4+
-->
5+
6+
<refentry id="sql-altervariable">
7+
<indexterm zone="sql-altervariable">
8+
<primary>ALTER VARIABLE</primary>
9+
</indexterm>
10+
11+
<indexterm>
12+
<primary>session variable</primary>
13+
<secondary>altering</secondary>
14+
</indexterm>
15+
16+
<refmeta>
17+
<refentrytitle>ALTER VARIABLE</refentrytitle>
18+
<manvolnum>7</manvolnum>
19+
<refmiscinfo>SQL - Language Statements</refmiscinfo>
20+
</refmeta>
21+
22+
<refnamediv>
23+
<refname>ALTER VARIABLE</refname>
24+
<refpurpose>
25+
change the definition of a session variable
26+
</refpurpose>
27+
</refnamediv>
28+
29+
<refsynopsisdiv>
30+
<synopsis>
31+
ALTER VARIABLE <replaceable class="parameter">name</replaceable> OWNER TO { <replaceable class="parameter">new_owner</replaceable> | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
32+
ALTER VARIABLE <replaceable class="parameter">name</replaceable> RENAME TO <replaceable class="parameter">new_name</replaceable>
33+
ALTER VARIABLE <replaceable class="parameter">name</replaceable> SET SCHEMA <replaceable class="parameter">new_schema</replaceable>
34+
</synopsis>
35+
</refsynopsisdiv>
36+
37+
<refsect1>
38+
<title>Description</title>
39+
40+
<para>
41+
The <command>ALTER VARIABLE</command> command changes the definition of an
42+
existing session variable. There are several subforms:
43+
44+
<variablelist>
45+
<varlistentry>
46+
<term><literal>OWNER</literal></term>
47+
<listitem>
48+
<para>
49+
This form changes the owner of the session variable.
50+
</para>
51+
</listitem>
52+
</varlistentry>
53+
54+
<varlistentry>
55+
<term><literal>RENAME</literal></term>
56+
<listitem>
57+
<para>
58+
This form changes the name of the session variable.
59+
</para>
60+
</listitem>
61+
</varlistentry>
62+
63+
<varlistentry>
64+
<term><literal>SET SCHEMA</literal></term>
65+
<listitem>
66+
<para>
67+
This form moves the session variable into another schema.
68+
</para>
69+
</listitem>
70+
</varlistentry>
71+
72+
</variablelist>
73+
</para>
74+
75+
<para>
76+
Only the owner or a superuser is allowed to alter a session variable.
77+
In order to move a session variable from one schema to another, the user
78+
must also have the <literal>CREATE</literal> privilege on the new schema (or
79+
be a superuser).
80+
81+
In order to move the session variable ownership from one role to another,
82+
the user must also be a direct or indirect member of the new
83+
owning role, and that role must have the <literal>CREATE</literal> privilege
84+
on the session variable's schema (or be a superuser). These restrictions
85+
enforce that altering the owner doesn't do anything you couldn't do by
86+
dropping and recreating the session variable.
87+
</para>
88+
</refsect1>
89+
90+
<refsect1>
91+
<title>Parameters</title>
92+
93+
<para>
94+
<variablelist>
95+
<varlistentry>
96+
<term><replaceable class="parameter">name</replaceable></term>
97+
<listitem>
98+
<para>
99+
The name (possibly schema-qualified) of the existing session variable
100+
to alter.
101+
</para>
102+
</listitem>
103+
</varlistentry>
104+
105+
<varlistentry>
106+
<term><replaceable class="parameter">new_owner</replaceable></term>
107+
<listitem>
108+
<para>
109+
The user name of the new owner of the session variable.
110+
</para>
111+
</listitem>
112+
</varlistentry>
113+
114+
<varlistentry>
115+
<term><replaceable class="parameter">new_name</replaceable></term>
116+
<listitem>
117+
<para>
118+
The new name for the session variable.
119+
</para>
120+
</listitem>
121+
</varlistentry>
122+
123+
<varlistentry>
124+
<term><replaceable class="parameter">new_schema</replaceable></term>
125+
<listitem>
126+
<para>
127+
The new schema for the session variable.
128+
</para>
129+
</listitem>
130+
</varlistentry>
131+
</variablelist>
132+
</para>
133+
</refsect1>
134+
135+
<refsect1>
136+
<title>Examples</title>
137+
138+
<para>
139+
To rename a session variable:
140+
<programlisting>
141+
ALTER VARIABLE foo RENAME TO boo;
142+
</programlisting>
143+
</para>
144+
145+
<para>
146+
To change the owner of the session variable <literal>boo</literal> to
147+
<literal>joe</literal>:
148+
<programlisting>
149+
ALTER VARIABLE boo OWNER TO joe;
150+
</programlisting>
151+
</para>
152+
153+
<para>
154+
To change the schema of the session variable <literal>boo</literal> to
155+
<literal>private</literal>:
156+
<programlisting>
157+
ALTER VARIABLE boo SET SCHEMA private;
158+
</programlisting>
159+
</para>
160+
</refsect1>
161+
162+
<refsect1>
163+
<title>Compatibility</title>
164+
165+
<para>
166+
Session variables and this command in particular are a PostgreSQL extension.
167+
</para>
168+
</refsect1>
169+
170+
<refsect1 id="sql-altervariable-see-also">
171+
<title>See Also</title>
172+
173+
<simplelist type="inline">
174+
<member><xref linkend="sql-createvariable"/></member>
175+
<member><xref linkend="sql-dropvariable"/></member>
176+
</simplelist>
177+
</refsect1>
178+
</refentry>

doc/src/sgml/ref/comment.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ COMMENT ON
6565
TRANSFORM FOR <replaceable>type_name</replaceable> LANGUAGE <replaceable>lang_name</replaceable> |
6666
TRIGGER <replaceable class="parameter">trigger_name</replaceable> ON <replaceable class="parameter">table_name</replaceable> |
6767
TYPE <replaceable class="parameter">object_name</replaceable> |
68+
VARIABLE <replaceable class="parameter">object_name</replaceable> |
6869
VIEW <replaceable class="parameter">object_name</replaceable>
6970
} IS { <replaceable class="parameter">string_literal</replaceable> | NULL }
7071

doc/src/sgml/ref/create_schema.sgml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ CREATE SCHEMA IF NOT EXISTS AUTHORIZATION <replaceable class="parameter">role_sp
103103
schema. Currently, only <command>CREATE
104104
TABLE</command>, <command>CREATE VIEW</command>, <command>CREATE
105105
INDEX</command>, <command>CREATE SEQUENCE</command>, <command>CREATE
106-
TRIGGER</command> and <command>GRANT</command> are accepted as clauses
107-
within <command>CREATE SCHEMA</command>. Other kinds of objects may
108-
be created in separate commands after the schema is created.
106+
TRIGGER</command>, <command>GRANT</command> and <command>CREATE
107+
VARIABLE</command> are accepted as clauses within <command>CREATE
108+
SCHEMA</command>. Other kinds of objects may be created in separate
109+
commands after the schema is created.
109110
</para>
110111
</listitem>
111112
</varlistentry>
@@ -214,6 +215,11 @@ CREATE VIEW hollywood.winners AS
214215
The <literal>IF NOT EXISTS</literal> option is a
215216
<productname>PostgreSQL</productname> extension.
216217
</para>
218+
219+
<para>
220+
The <command>CREATE VARIABLE</command> command is a
221+
<productname>PostgreSQL</productname> extension.
222+
</para>
217223
</refsect1>
218224

219225
<refsect1>

0 commit comments

Comments
 (0)